haul        2003/01/31 08:28:31

  Modified:    src/java/org/apache/cocoon/transformation
                        AbstractExtractionTransformer.java
                        SimpleFormInstanceExtractionTransformer.java
  Log:
  make more versatile. derived class can override root node creation
  
  Revision  Changes    Path
  1.3       +71 -38    
xml-cocoon2/src/java/org/apache/cocoon/transformation/AbstractExtractionTransformer.java
  
  Index: AbstractExtractionTransformer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/AbstractExtractionTransformer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractExtractionTransformer.java        7 Jan 2003 23:56:12 -0000       1.2
  +++ AbstractExtractionTransformer.java        31 Jan 2003 16:28:31 -0000      1.3
  @@ -76,11 +76,11 @@
    */
   abstract public class AbstractExtractionTransformer extends AbstractTransformer {
   
  -    private DOMBuilder currentBuilder;
  +    protected DOMBuilder currentBuilder;
   
       private Map prefixMap;
   
  -    private int extractLevel;
  +    protected int extractLevel;
   
   
       /** Setup the transformer. */
  @@ -161,37 +161,42 @@
        *          attributes, it shall be an empty Attributes object.
        */
       public void startElement(String uri, String loc, String raw, Attributes a) 
throws SAXException {
  -        if (startExtracting(uri, loc, raw, a)) {
  +        if (!startExtracting(uri, loc, raw, a)) {
  +
  +            if (extractLevel == 0) {
  +                super.startElement(uri,loc,raw,a);
  +            } else {
  +                this.currentBuilder.startElement(uri,loc,raw,a);
  +            }
  +
  +        } else {
  +
               extractLevel++;
  -            if (extractLevel == 1) {
  -                startExtractedDocument(uri, loc, raw, a);
  -                if (this.getLogger().isDebugEnabled()) {
  -                    getLogger().debug("extractLevel now " + extractLevel + ".");
  -                }
  +            if (this.getLogger().isDebugEnabled()) {
  +                getLogger().debug("extractLevel now " + extractLevel + ".");
  +            }
  +
  +            if (extractLevel != 1) {
  +                this.currentBuilder.startElement(uri,loc,raw,a);
               } else {
  -                if (this.getLogger().isDebugEnabled()) {
  -                    getLogger().debug("extractLevel now " + extractLevel + ".");
  +
  +                // setup new document
  +                this.currentBuilder = new DOMBuilder();
  +                this.currentBuilder.startDocument();
  +                // setup namespaces
  +                Iterator itt = prefixMap.entrySet().iterator();
  +                while (itt.hasNext()) {
  +                    Map.Entry entry = (Map.Entry)itt.next();
  +                    this.currentBuilder.startPrefixMapping(
  +                        (String)entry.getKey(),
  +                        (String)entry.getValue()
  +                    );
                   }
  -            }
  +                // start root node
  +                startExtractingDocument(uri, loc, raw, a);
   
  -            // Start the DOM document
  -            this.currentBuilder = new DOMBuilder();
  -            this.currentBuilder.startDocument();
  -
  -            Iterator itt = prefixMap.entrySet().iterator();
  -            while (itt.hasNext()) {
  -                Map.Entry entry = (Map.Entry)itt.next();
  -                this.currentBuilder.startPrefixMapping(
  -                    (String)entry.getKey(),
  -                    (String)entry.getValue()
  -                );
               }
  -        }
   
  -        if (extractLevel == 0) {
  -            super.startElement(uri,loc,raw,a);
  -        } else {
  -            this.currentBuilder.startElement(uri,loc,raw,a);
           }
       }
   
  @@ -214,15 +219,19 @@
           if (extractLevel == 0) {
               super.endElement(uri,loc,raw);
           } else {
  -            this.currentBuilder.endElement(uri,loc,raw);
               if (endExtracting(uri, loc, raw)) {
                   extractLevel--;
                   if (this.getLogger().isDebugEnabled()) {
                       getLogger().debug("extractLevel now " + extractLevel + ".");
                   }
   
  -                if (extractLevel == 0) {
  -                    // finish building the fragment. remove existing prefix 
mappings.
  +                if (extractLevel != 0) {
  +                    this.currentBuilder.endElement(uri,loc,raw);
  +                } else {
  +
  +                    // end root element
  +                    endExtractingDocument(uri, loc, raw);
  +                    // finish building the document. remove existing prefix 
mappings.
                       Iterator itt = prefixMap.entrySet().iterator();
                       while (itt.hasNext()) {
                           Map.Entry entry = (Map.Entry) itt.next();
  @@ -232,13 +241,15 @@
                       }
                       this.currentBuilder.endDocument();
   
  -                    endExtractedDocument(this.currentBuilder.getDocument());
  +                    handleExtractedDocument(this.currentBuilder.getDocument());
   
                       if (this.getLogger().isDebugEnabled()) {
                           getLogger().debug("Stored document.");
                       }
   
                   }
  +            } else {
  +                this.currentBuilder.endElement(uri, loc, raw);
               }
           }
       }
  @@ -412,8 +423,8 @@
   
   
       /**
  -     * Receive notification of the beginning of an element.
  -     *
  +     * Receive notification of the beginning of an element and signal extraction 
start.
  +     * 
        * @param uri The Namespace URI, or the empty string if the element has no
        *            Namespace URI or if Namespace
        *            processing is not being performed.
  @@ -428,7 +439,9 @@
       abstract boolean startExtracting(String uri, String loc, String raw, Attributes 
a);
   
       /**
  -     * Receive notification of the beginning of the extracted Document.
  +     * Receive notification of the beginning of the extracted Document. Per default 
send
  +     * startElement message to document builder. Override if necessary. Must 
override 
  +     * {@link #endExtractingDocument} as well. 
        *
        * @param uri The Namespace URI, or the empty string if the element has no
        *            Namespace URI or if Namespace
  @@ -440,10 +453,12 @@
        * @param a The attributes attached to the element. If there are no
        *          attributes, it shall be an empty Attributes object.
        */
  -    abstract void startExtractedDocument(String uri, String loc, String raw, 
Attributes a);
  +    public void startExtractingDocument(String uri, String loc, String raw, 
Attributes a) throws SAXException{
  +        this.currentBuilder.startElement(uri,loc,raw,a);
  +    }
   
       /**
  -     * Receive notification of the beginning of an element.
  +     * Receive notification of the end of an element and signal extraction end.
        *
        * @param uri The Namespace URI, or the empty string if the element has no
        *            Namespace URI or if Namespace
  @@ -456,10 +471,28 @@
       abstract boolean endExtracting(String uri, String loc, String raw);
   
       /**
  +     * Receive notification of the end of the extracted Document. Per default, 
  +     * send endElement message to document builder. Override if necessary.
  +     * Must override {@link #startExtractingDocument} as well.
  +     *
  +     * @param uri The Namespace URI, or the empty string if the element has no
  +     *            Namespace URI or if Namespace
  +     *            processing is not being performed.
  +     * @param loc The local name (without prefix), or the empty string if
  +     *            Namespace processing is not being performed.
  +     * @param raw The raw XML 1.0 name (with prefix), or the empty string if
  +     *            raw names are not available.
  +     */
  +    public void endExtractingDocument(String uri, String loc, String raw) throws 
SAXException{
  +        this.currentBuilder.endElement(uri,loc,raw);
  +    }
  +
  +    /**
        * Receive notification of the end of the extracted Document.
        *
        * @param doc a <code>Document</code> value
        */
  -    abstract void endExtractedDocument(Document doc);
  +    abstract void handleExtractedDocument(Document doc);
  +    
   
   }
  
  
  
  1.5       +49 -19    
xml-cocoon2/src/java/org/apache/cocoon/transformation/SimpleFormInstanceExtractionTransformer.java
  
  Index: SimpleFormInstanceExtractionTransformer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/SimpleFormInstanceExtractionTransformer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SimpleFormInstanceExtractionTransformer.java      15 Jan 2003 15:59:06 -0000     
 1.4
  +++ SimpleFormInstanceExtractionTransformer.java      31 Jan 2003 16:28:31 -0000     
 1.5
  @@ -67,6 +67,7 @@
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.modules.output.OutputModule;
   import org.apache.cocoon.environment.SourceResolver;
  +import org.apache.cocoon.xml.dom.DocumentWrapper;
   
   import org.w3c.dom.Document;
   import org.xml.sax.Attributes;
  @@ -124,6 +125,7 @@
       String qname = "name";
   
       String instanceName = null;
  +    boolean nameAsRoot = true;
   
       String outputModuleName = "request-attr";
       Configuration outputConf = null;
  @@ -143,6 +145,8 @@
           this.nameElement.raw = config.getChild("name").getAttribute("raw-name", 
"form");
           this.qname = config.getChild("name").getAttribute("name-attribute", "name");
   
  +        this.nameAsRoot = 
config.getChild("name-as-root").getValueAsBoolean(this.nameAsRoot);
  +
           this.outputConf = config.getChild("output");
           this.outputModuleName = 
this.outputConf.getAttribute("name",this.outputModuleName);
       }
  @@ -186,22 +190,6 @@
       }
   
       /**
  -     * Receive notification of the beginning of the extracted Document.
  -     *
  -     * @param uri The Namespace URI, or the empty string if the element has no
  -     *            Namespace URI or if Namespace
  -     *            processing is not being performed.
  -     * @param loc The local name (without prefix), or the empty string if
  -     *            Namespace processing is not being performed.
  -     * @param raw The raw XML 1.0 name (with prefix), or the empty string if
  -     *            raw names are not available.
  -     * @param a The attributes attached to the element. If there are no
  -     *          attributes, it shall be an empty Attributes object.
  -     */
  -    public void startExtractedDocument(String uri, String loc, String raw, 
Attributes a) {
  -    }
  -
  -    /**
        * Receive notification of the beginning of an element.
        *
        * @param uri The Namespace URI, or the empty string if the element has no
  @@ -217,25 +205,67 @@
           return res;
       }
   
  +
  +    /**
  +     * Start root element and replace it with the instance name.
  +     * @see 
org.apache.cocoon.transformation.AbstractExtractionTransformer#startExtractingDocument(String,
 String, String, Attributes)
  +     */
  +    public void startExtractingDocument(String uri, String loc, String raw, 
Attributes a) throws SAXException {
  +        if (this.nameAsRoot) {
  +            loc = this.instanceName;
  +            if (uri != null && !uri.equals("")) {
  +                int pos = raw.indexOf(':');
  +                raw = raw.substring(0, pos+1) + this.instanceName;
  +            } else {
  +                raw = loc;
  +            }
  +        }
  +        this.currentBuilder.startElement(uri,loc,raw,a);
  +    }
  +
  +    /**
  +     * End root element and replace it with the instance name.
  +     * @see 
org.apache.cocoon.transformation.AbstractExtractionTransformer#endExtractingDocument(String,
 String, String)
  +     */
  +    public void endExtractingDocument(String uri, String loc, String raw) throws 
SAXException{
  +        if(this.nameAsRoot){
  +            loc = this.instanceName;
  +            if (uri != null && !uri.equals("")) {
  +                int pos = raw.indexOf(':');
  +                raw = raw.substring(0, pos+1) + this.instanceName;
  +            } else {
  +                raw = loc;
  +            }
  +        }
  +        this.currentBuilder.endElement(uri, loc, raw);
  +    }
  +
  +
       /**
        * Receive notification of the end of the extracted Document.
        *
        * @param doc a <code>Document</code> value
        */
  -    public void endExtractedDocument(Document doc) {
  +    public void handleExtractedDocument(Document doc) {
           
           ComponentSelector outputSelector = null;
           OutputModule output = null;
   
           try {
  +            if (getLogger().isDebugEnabled())
  +                getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" 
using "+outputConf);
               outputSelector = (ComponentSelector) 
this.manager.lookup(OUTPUT_MODULE_SELECTOR);
               if (outputSelector.hasComponent(this.outputModuleName)) {
                   output = (OutputModule) 
outputSelector.select(this.outputModuleName);
               }
  -            output.setAttribute(outputConf, this.objectModel, this.instanceName, 
doc);
  +            output.setAttribute(outputConf, this.objectModel, this.instanceName, 
new DocumentWrapper(doc));
               output.commit(outputConf, this.objectModel);
  +            if (getLogger().isDebugEnabled())
  +                getLogger().debug("wrote ['"+this.instanceName+"'] to "+output+" 
using "+outputConf);
   
           } catch (Exception e) {
  +            if (getLogger().isWarnEnabled())
  +                getLogger().warn("Problem writing document data: "+e.getMessage());
           } finally {
               if (outputSelector != null) {
                   if (output != null) {
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to