[Home]  [List]  [News]  [Docs]  [FAQ]  [Downloads]  [Resources]  [About]
Search :
/Home /List

list4xt : Mailing list for the XT users community.

[list4xt] Re: How do you set the XSLProcessor as a DocumentHand

Subject: [list4xt] Re: How do you set the XSLProcessor as a DocumentHand

User: Website

From: Sebastien Sahuc (ssahuc@imediation.com)
Date: 29/08/2000 - 15:34


> Sebastien Sahuc wrote:
> >
>>. That's the reason why I prefered to
> > copy/paste the XSLProcessorImpl and split the XMLProcessorImpl.load()
> > method in two part, one to set the Builder (DocHandler) and one that
> > extract the RootNode from the builder when sax events have been totally
> > fired.

> Those 2 classes are public and not defined as final.
> Why wouldn't you extend them rather than copy/paste ?

Well, I started that way, unfortunately field members are all private so
not visible to the extended class. I bet there is better solution, so
please have a look at the two attached classes and let me know if you
find something that need to be improved.

Thanks,

Sebastien

PS: when you reply to an 4xt.org post, it replies to the sender, not the
list. Was it done on purpose ?

-- HTML Attachment decoded to text by Listar --
-- File: XTFilter.java
-- Desc: filename="XTFilter.java"

/****************************************************************************
Ãÿ¿¨* * Copyright (C) The Apache Software Foundation. All rights reserved.
** -------------------------------------------------------------------------
* * This software is published under the terms of the Apache Software
License* * version 1.1, a copy of which has been included with this
distribution in * * the LICENSE file. *
*****************************************************************************
/ package org.apache.cocoon.transformation; import java.io.IOException;
import java.util.Enumeration; import java.util.Dictionary; import
java.text.StringCharacterIterator; import
javax.servlet.http.HttpServletRequest; import org.apache.avalon.Component;
import org.apache.avalon.ComponentManager; import
org.apache.avalon.Composer;import org.apache.avalon.utils.Parameters; import
org.apache.cocoon.Cocoon; import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.parser.Parser; import
org.apache.cocoon.xml.XMLConsumer; import
org.apache.cocoon.xml.util.DocumentHandlerAdapter; import
org.apache.cocoon.xml.util.DocumentHandlerWrapper; import
org.apache.cocoon.transformation.Transformer; import
org.apache.cocoon.components.store.Store; import org.xml.sax.ContentHandler;
import org.xml.sax.Locator; import org.xml.sax.DTDHandler; import
org.xml.sax.DocumentHandler; import org.xml.sax.HandlerBase; import
org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import
org.xml.sax.SAXException; import org.xml.sax.AttributeList; import
org.xml.sax.ext.LexicalHandler; import
com.jclark.xsl.sax.OutputDocumentHandler; import
com.jclark.xsl.sax.FileDestination; import
com.jclark.xsl.sax.FileDescriptorDestination; import
com.jclark.xsl.sax.XSLProcessor; import com.jclark.xsl.sax.XSLProcessorImpl;
import com.jclark.xsl.sax.XMLProcessorImpl; import
org.apache.xerces.parsers.SAXParser; /** * * @author Sahuc Sebastien[1] *
@version */ public class XTFilter extends DocumentHandlerWrapper implements
Transformer, Composer { /** The component manager instance */ private
ComponentManager manager=null; /** The XT XSLProcessor */ private
XTXSLProcessorImpl processor=null; /**The DocHandler */ private
DocumentHandler docHandler=null; /** * Set the EntityResolver, the
Dictionarywith * the object model, the source and sitemap * Parameters used
to process the request. */ public void setup(EntityResolver resolver,
Dictionary objectModel, String src, Parameters par) throws SAXException,
ProcessingException, IOException { /** The Request object */
HttpServletRequest request=(HttpServletRequest)objectModel.get("request");
if(request == null) { throw new ProcessingException ("Missing request object
in objectModel"); } Store store = (Store) manager.getComponent("store"); if
(store == null) { throw new ProcessingException ("Missing store object in
manager"); } // Check the stylesheet uri String xsluri=src; if
(xsluri==null)throw new ProcessingException("No stylesheet"); // Load the
stylesheet (we should cache it in the STORE!) XTXSLProcessorImpl
loaderprocessor=null; // get from the store. loaderprocessor =
(XTXSLProcessorImpl) store.get(xsluri); if (loaderprocessor == null ||
loaderprocessor.hasChanged() ) { loaderprocessor= new XTXSLProcessorImpl();
SAXParser parser = new SAXParser(); loaderprocessor.setParser(parser);
InputSource xslsrc=resolver.resolveEntity(null,xsluri);
loaderprocessor.loadStylesheet(xslsrc); store.store (xsluri,
loaderprocessor); } // Always clone the processor before using it, since
multi threaded application this.processor = (XTXSLProcessorImpl)
loaderprocessor.clone(); // Create the processor and set it as this
documenthandler // FIX ME : set the correct SystemId to the XML inputSource
this.docHandler = new DocHandler (processor.createDocHandler("XTSystemID"));
this.setDocumentHandler(this.docHandler); } /** * Set the current
ComponentManager instance used by this * Composer. */ public void
setComponentManager(ComponentManager manager) { this.manager=manager; } /**
*Set the XMLConsumer that will receive XML data. *
* This method will simply call setContentHandler(consumer) * and
setLexicalHandler(consumer). */ public void setConsumer(XMLConsumer
consumer){ this.setContentHandler(consumer); } /** * Set the ContentHandler
that will receive XML data. *
* Subclasses may retrieve this ContentHandler instance * accessing the
protected super.contentHandler field. */ public void
setContentHandler(ContentHandler content) {
this.processor.setDocumentHandler(new DocumentHandlerAdapter(content)); }
/*** Set the LexicalHandler that will receive XML data. *
* Subclasses may retrieve this LexicalHandler instance * accessing the
protected super.lexicalHandler field. * * @exception IllegalStateException
Ifthe LexicalHandler or * the XMLConsumer were * already set. */ public void
setLexicalHandler(LexicalHandler lexical) { } public class DocHandler
implements DocumentHandler, DTDHandler { private XMLProcessorImpl.Builder
handler = null; public DocHandler(XMLProcessorImpl.Builder handler) {
this.handler = handler; } public void setDocumentLocator (Locator locator) {
handler.setDocumentLocator(locator); } public void ignorableWhitespace
(char[] ch, int start, int length) throws SAXException {
handler.ignorableWhitespace (ch, start, length); } public void
processingInstruction (String target, String data) throws SAXException {
handler.processingInstruction (target, data); } public void startDocument ()
throws SAXException { handler.startDocument(); } public void endDocument ()
throws SAXException { handler.endDocument(); try { // Finish with the source
document. processor.process(handler); } catch (IOException ioe) { throw new
SAXException(ioe); } } public void startElement (String name, AttributeList
atts) throws SAXException { handler.startElement (name, atts); } public void
endElement (String name) throws SAXException { handler.endElement (name); }
public void characters (char[] str, int index, int len) throws SAXException
{handler.characters ( str, index, len); } public void notationDecl (String
name, String publicId, String systemId) throws SAXException {
handler.notationDecl (name, publicId, systemId); } public void
unparsedEntityDecl (String name, String publicId, String systemId, String
notationName) throws SAXException { handler.unparsedEntityDecl (name,
publicId, systemId, notationName); } } }

--- Links ---
   1 mailto:ssahuc@imediation.com

-- Attached file included as plaintext by Listar --
-- File: XTXSLProcessorImpl.java
-- Desc: filename="XTXSLProcessorImpl.java"

/******************************************************
 * File: XTXSLProcessorImpl.java
 * created 28-Aug-00 6:17:08 PM by ssahuc
 */

package org.apache.cocoon.transformation;

import org.apache.avalon.Modifiable;

import org.xml.sax.*;
import com.jclark.xsl.tr.*;
import com.jclark.xsl.om.*;
import com.jclark.xsl.sax.*;
import java.util.Locale;
import java.io.IOException;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;

public class XTXSLProcessorImpl implements Cloneable, ParameterSet, Modifiable {
  private XMLProcessorEx sheetLoader;
  private Parser sheetParser;
  private Sheet sheet;
  private Engine engine;
  private InputSource sheetSource;
  private ResultBase result;
  private DocumentHandler documentHandler;
  private ErrorHandler errorHandler;
  private Hashtable params = new Hashtable();
  private File xslFile;
  private long lastModified;

  public void setParser(Parser sheetParser) {
    this.sheetParser = sheetParser;
    sheetLoader = new XMLProcessorImpl(sheetParser);
  }

  public void setDocumentHandler(DocumentHandler handler) {
    documentHandler = handler;
  }

  public void setErrorHandler(ErrorHandler handler) {
    if (sheetParser != null)
      sheetParser.setErrorHandler(handler);
    if (sheetLoader != null)
      sheetLoader.setErrorHandler(handler);
    this.errorHandler = handler;
  }

  public void loadStylesheet(InputSource sheetSource) throws SAXException, IOException {
      // Set the xslFile for the caching mechanism
      URL url = new URL (sheetSource.getSystemId());
      this.xslFile = new File(url.getFile());
      lastModified = xslFile.lastModified();
      
      engine = new EngineImpl(sheetLoader, new ExtensionHandlerImpl());
    try {
      Node node = sheetLoader.load(sheetSource,
                                   0,
                                   engine.getSheetLoadContext(),
                                   engine.getNameTable());
      sheet = engine.createSheet(node);
    }
    catch (XSLException e) {
      handleXSLException(e);
    }
  }

  public XMLProcessorImpl.Builder createDocHandler (String systemId) {
      XMLProcessorImpl.Builder builder
      = XMLProcessorImpl.createBuilder ( systemId, 0, sheet.getSourceLoadContext(),engine.getNameTable());
      
      return builder;
  }
  
  
  public void process(XMLProcessorImpl.Builder builder) throws SAXException, IOException {
    try {
        result = new MultiNamespaceResult(documentHandler, errorHandler);
      Node root = builder.getRootNode();
      sheet.process(root, sheetLoader, this, result);
    }
    catch (XSLException e) {
      handleXSLException(e);
    }
  }

  void handleXSLException(XSLException e) throws SAXException, IOException {
    String systemId = null;
    int lineNumber = -1;
    Node node = e.getNode();
    if (node != null) {
      URL url = node.getURL();
      if (url != null)
        systemId = url.toString();
      lineNumber = node.getLineNumber();
    }
    Exception wrapped = e.getException();
    String message = e.getMessage();
    if (systemId != null || lineNumber != -1)
      throw new SAXParseException(message,
                                  null,
                                  systemId,
                                  lineNumber,
                                  -1,
                                  wrapped);
    if (message == null) {
      if (wrapped instanceof SAXException)
        throw (SAXException)wrapped;
      if (wrapped instanceof IOException)
        throw (IOException)wrapped;
    }
    throw new SAXException(message, wrapped);
  }

  void phase(int n) { }

  public Object clone() {
    try {
       XTXSLProcessorImpl cloned = (XTXSLProcessorImpl)super.clone();
       cloned.params = (Hashtable)cloned.params.clone();
       return cloned;
    }
    catch (CloneNotSupportedException e) {
      throw new Error("unexpected CloneNotSupportedException");
    }
  }

  public Object getParameter(Name name) {
    String nameString = name.getNamespace();
    if (nameString == null)
      nameString = name.getLocalPart();
    else
      nameString = (nameString
                    + OutputMethodHandler.namespaceSeparator
                    + name.getLocalPart());
    return params.get(nameString);
  }

  public void setParameter(String name, Object obj) {
    params.put(name, obj);
  }
  
  public boolean modifiedSince(long date) {
      return(date < this.xslFile.lastModified());
  }
  
  public boolean hasChanged() {
      if (this.xslFile == null) {
        return false;
      }

      return modifiedSince(this.lastModified);
    }
}

--
Mailing list for the XT users community.     (http://nationalcollectionagencies.com)
(mailto:list4xt-request@4xt.org?Subject=unsubscribe to unsubscribe)



Archive générée par hypermail 2b28 le 06/11/2001 - 11:46 CET

webmaster@4xt.org