Hello,

you can also use the omitns transformer at the end of your pipeline :
add this to the components/transformer section of the sitemap
<map:transformer logger="sitemap.transformer.omitns" name="omitns" pool-grow="2" pool-max="16" pool-min="2" src="com.gitus.cocoon.transform.OmitNsTransformer"/>

then in your pipe finish with
       <map:transform type="omitns"/>
       <map:serialize type="html"/>

I attached the source file of the transformer file containing the source of the transformer, as i don't think it's part of the distrib.
Just compile the class and put the jar in WEB-INF/lib folder.

Best regards,

Marc



Geert Josten a écrit :

If there is no XSL, then why not add it? It a relatively small slowdown compared to cinclude..

My pipeline looks the following:

  <map:match pattern="...">
    ...
    <map:transform type="cinclude"/>

    <map:transform type="xslt" src="stripUnusedNamespaces.xsl" />

    <map:serialize type="xhtml"/>
  </map:pattern>


Cheers,
G.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




/*
 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================

 Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.

 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:

 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

 4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    [EMAIL PROTECTED]

 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
 Software Foundation, please see <http://www.apache.org/>.
 
 * OmitNsTransformer.java
 * (C)2004 Gitus, s.r.o., http://www.gitus.com
 * Henryk Paluch, [EMAIL PROTECTED]
 */
package com.gitus.cocoon.transform;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.avalon.excalibur.pool.Recyclable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.AbstractTransformer;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * This transformer is supposed to omit selected or all namespace declaration.
 * <h2>Usage</h2>
 * Declare this transformer in <code>map:transformers</code> section:
 * <pre>
 *  &lt;map:components&gt;
 *    ...
 *  &lt;map:transformes&gt;
 *    ...
 *  &lt;map:transformer
 *      logger="sitemap.transformer.omitns"
 *      name="omitns"
 *          pool-grow="2" pool-max="16" pool-min="2"
 *          src="com.gitus.cocoon.transform.OmitNsTransformer"/&gt;
 *   &lt;/map:transformer&gt;
 *   ...
 * </pre>
 * 
 * Insert <code>omitns</code> transformer with <code>omit-namespaces</code>
 * into your pipeline. For example, to omit common logicsheet tags from xsp use 
something like:
 * 
 * <pre>
 * &lt;map:match pattern="*_xsp"&gt;
 *  &lt;map:generate type="serverpages" src="xsp/{1}.xsp"/&gt;
 *  &lt;map:transform type="omitns" label="xml"&gt;
 *   &lt;map:parameter name="omit-namespaces" value="xsp jpath xspdoc esql 
xsp-request"/&gt;
 *  &lt;/map:transform&gt;
 *  &lt;map:serialize type="xml"/&gt;
 * &lt;/map:match&gt;                            
 * </pre>
 *
 * <p>If you want to omit all ns declaration - then leave 
<code>omit-namespaces</code> empty.</p>
 * <p>If you want to omit plain <code>xmlns=http://....</code> attribute 
(usefull for HTMLSerializer) then specify</p>
 * <pre>
 *   &lt;map:parameter name="omit-plain-xmlns" value="true"/&gt;
 * </pre>
 * @author <a href="mailto:[EMAIL PROTECTED]">Henryk Paluch</a>
 * @version CVS $Id:$ 2004-01-16
 * 
 * 
 */
public class OmitNsTransformer extends AbstractTransformer
 implements Recyclable
  {
        /**
         * Parameter - specifies whitespace or comma separated list of 
namespaces
         * to omit (remove) from output.
         */
        public static final String OMITNS_OMIT_NAMESPACES="omit-namespaces";

        /**
         * Parameter - specifies whether to omit main 
<code>xmlns=http://...</code>
         * declaration.
         * Allowed values are "true" or "false". Default is false;
         */
        public static final String OMITNS_OMIT_PLAIN_XMLNS="omit-plain-xmlns";


        /**
         * Set of Strings containing namespace prefixes to omit from output.
         */
        protected Set omittedNs = null;

        /**
         * We need to make runtime set of URIs to omit too...
         */
        protected Set omittedUris = null;

        
        /**
         * Whether to omit plain <code>xmlns=http://abcde</code>.
         * It is usefull when you like to specify <code>xmlns</code> for html
         * (it is accurate for XHTML or XML Serialization),
         * but it should be filtered out for HTML serialization
         */
        protected boolean omitPlainXmlns = false;

        /** 
         * Setup - prepare transformer.
         * setup list of omitted namespaces
         * @see 
org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
 java.util.Map, java.lang.String, 
org.apache.avalon.framework.parameters.Parameters)
         */
        public void setup(
                SourceResolver resolver,
                Map objectModel,
                String src,
                Parameters parameters)
                throws ProcessingException, SAXException, IOException {
                        omitPlainXmlns = 
parameters.getParameterAsBoolean(OMITNS_OMIT_PLAIN_XMLNS,false);                
       
                        omittedNs = new HashSet();
                        omittedUris = new HashSet();
                        myAddOmittedNamespaces(parameters);
                        if ( getLogger().isDebugEnabled()){
                                getLogger().debug("setup: List of ns prefixes 
to omit: "+showSet(omittedNs));
                                getLogger().debug("setup: 
omitPlainXmlns="+omitPlainXmlns);
                        }
                        
        }
        
        /**
         * Release Set on recycle event.
         */
        public void recycle() {
                super.recycle();
                omittedNs = null;
                omittedUris = null;
        }

        /**
         * Hook for startPrefixMapping - if prefix is found in our omittedNs 
set - then ignore.
         */
        public void startPrefixMapping(String prefix, String uri)
                throws SAXException {

                if ( ( omitPlainXmlns && prefix.length()==0) 
                      || shouldOmitNs(prefix)){
                        if ( getLogger().isDebugEnabled()){
                                getLogger().debug("startPrefixMapping: omiting 
prefix="+prefix+", uri="+uri);
                        }
                        // add uri to omit list
                        omittedUris.add(uri);
                } else {
                        super.startPrefixMapping(prefix, uri);
                }
        }

        /**
         * Hook for endPrefixMapping - if prefix is found in our omittedNs set 
- then ignore.
         */

        public void endPrefixMapping(String prefix) throws SAXException {
                if (  ( omitPlainXmlns && prefix.length()==0) 
                      || shouldOmitNs(prefix)){
                        if ( getLogger().isDebugEnabled()){
                                getLogger().debug("endPrefixMapping: omiting 
prefix="+prefix);
                        }
                } else {
                        super.endPrefixMapping(prefix);
                }
        }

        /**
         * Catch and filter out namespace in attributes.
         * It is supposed to filter out unwanted <code>xmlns:xyz</code>
         * This attribute is "inverted" (it is in <code>xmlns</code> but no 
<code>xyz</code>space
         * - therefore special catch needed...)
         * attribute.
         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, 
java.lang.String, java.lang.String, org.xml.sax.Attributes)
         */
        public void startElement(
                String uri,
                String name,
                String raw,
                Attributes attr)
                throws SAXException {
                
                Attributes realAttr = attr; // by default pass unmodified 
attribute list        
                if ( attr.getLength()>0){                       
                        AttributesImpl myAttr = new AttributesImpl(attr);
                        // walk attributes backward to
                        // avoid unnecessary index change
                        for(int i=myAttr.getLength()-1;i>=0;i--){
                                String local = myAttr.getLocalName(i);
                                String qName = myAttr.getQName(i);
                                // handle omit-plain-xmlns
                                if ( omitPlainXmlns && qName.equals("xmlns")){
                                        if ( getLogger().isDebugEnabled()){
                                                
getLogger().debug("startElement: omitting main xmlns attribute: "
                                                           
+",qname="+qName+",value="+myAttr.getValue(i));
                                        }
                                        myAttr.removeAttribute(i);
                                        continue;
                                }
                                // handle omit-namespaces                       
        
                                if ( qName.startsWith("xmlns:") && 
local.length()>0
                                         && shouldOmitNs(local) ){
                                                if ( 
getLogger().isDebugEnabled()){
                                                        
getLogger().debug("startElement: omitting xmlns attribute: local="+local
                                                                   
+",qname="+qName+",value="+myAttr.getValue(i));
                                                }
                                                myAttr.removeAttribute(i);
                                }
                        }
                        realAttr = myAttr;
                }                       
                if ( omittedUris.contains(uri)){
                        uri = ""; // clear uri
                }
                super.startElement(uri, name, raw, realAttr);
        }

        /**
         * Omit uris if needed.
         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, 
java.lang.String, java.lang.String)
         */
        public void endElement(String uri, String loc, String qname)
                throws SAXException {
                        
                if ( omittedUris.contains(uri)){
                                uri = ""; // clear uri
                }               
                super.endElement(uri, loc, qname);
        }


        /**
         * Returns true if we should omit all namespace prefixes.
         * @return
         */
        private boolean shouldOmitNs(String nsName)
        {
                return omittedNs.isEmpty() || omittedNs.contains(nsName);
        }

        /**
         * Parses list of namespace names in form of <code>ns1 ns2 ns3</code>
         * and adds them to <code>omitNsStr</code>.
         * @param parameters
         */
        private void myAddOmittedNamespaces(Parameters parameters) {
                String omitNsStr = 
parameters.getParameter(OMITNS_OMIT_NAMESPACES,null);
                if ( omitNsStr != null){
                        StringTokenizer t = new StringTokenizer(omitNsStr," 
\t\r\n\f,:");
                        while ( t.hasMoreTokens()){
                                String ns = t.nextToken();
                                ns = ns.trim();
                                if ( ns.length() > 0){
                                        omittedNs.add(ns); 
                                }
                        }
                }               
        }
        
        /**
         * Returns String of set items (using their toString() method).
         * Used for debug purposes.
         * @param s
         * @return
         */
        private String showSet(Set s)
        {
                StringBuffer b = new StringBuffer();
                Iterator i = s.iterator();
                while (i.hasNext()){
                        String str = (String)i.next();
                        if ( b.length()>0){
                                b.append(",");
                        }
                        b.append(str);
                }
                return b.toString();            
        }



}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to