Search in sources :

Example 16 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project jangaroo-tools by CoreMedia.

the class ExmlToConfigClassParser method parseFileWithHandler.

public static void parseFileWithHandler(File source, ContentHandler handler) {
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(source);
        XMLReader xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(handler);
        if (handler instanceof LexicalHandler) {
            xr.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        }
        xr.parse(new org.xml.sax.InputSource(inputStream));
    } catch (ExmlcException e) {
        // Simply pass our own exceptions.
        e.setFile(source);
        throw e;
    } catch (Exception e) {
        throw new ExmlcException("could not parse EXML file", source, e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
        //never happened
        }
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExmlcException(net.jangaroo.exml.api.ExmlcException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLReader(org.xml.sax.XMLReader) ExmlcException(net.jangaroo.exml.api.ExmlcException) IOException(java.io.IOException)

Example 17 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project webservices-axiom by apache.

the class TestGetSAXResultWithDTD method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement root = metaFactory.getOMFactory().createOMElement("root", null);
    SAXResult result = root.getSAXResult();
    LexicalHandler lexicalHandler = result.getLexicalHandler();
    ContentHandler contentHandler = result.getHandler();
    contentHandler.startDocument();
    lexicalHandler.startDTD("test", null, "my.dtd");
    lexicalHandler.endDTD();
    contentHandler.startElement("", "test", "test", new AttributesImpl());
    contentHandler.endElement("", "test", "test");
    contentHandler.endDocument();
    OMNode child = root.getFirstOMChild();
    assertTrue(child instanceof OMElement);
    assertEquals("test", ((OMElement) child).getLocalName());
}
Also used : OMNode(org.apache.axiom.om.OMNode) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXResult(javax.xml.transform.sax.SAXResult) LexicalHandler(org.xml.sax.ext.LexicalHandler) OMElement(org.apache.axiom.om.OMElement) ContentHandler(org.xml.sax.ContentHandler)

Example 18 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project j2objc by google.

the class TreeWalker method startNode.

/**
   * Start processing given node
   *
   *
   * @param node Node to process
   *
   * @throws org.xml.sax.SAXException
   */
protected void startNode(Node node) throws org.xml.sax.SAXException {
    if (m_contentHandler instanceof NodeConsumer) {
        ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    }
    if (node instanceof Locator) {
        Locator loc = (Locator) node;
        m_locator.setColumnNumber(loc.getColumnNumber());
        m_locator.setLineNumber(loc.getLineNumber());
        m_locator.setPublicId(loc.getPublicId());
        m_locator.setSystemId(loc.getSystemId());
    } else {
        m_locator.setColumnNumber(0);
        m_locator.setLineNumber(0);
    }
    switch(node.getNodeType()) {
        case Node.COMMENT_NODE:
            {
                String data = ((Comment) node).getData();
                if (m_contentHandler instanceof LexicalHandler) {
                    LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);
                    lh.comment(data.toCharArray(), 0, data.length());
                }
            }
            break;
        case Node.DOCUMENT_FRAGMENT_NODE:
            // ??;
            break;
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            NamedNodeMap atts = ((Element) node).getAttributes();
            int nAttrs = atts.getLength();
            for (int i = 0; i < nAttrs; i++) {
                Node attr = atts.item(i);
                String attrName = attr.getNodeName();
                // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
                    // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());
                    int index;
                    // Use "" instead of null, as Xerces likes "" for the 
                    // name of the default namespace.  Fix attributed 
                    // to "Steven Murray" <smurray@ebt.com>.
                    String prefix = (index = attrName.indexOf(":")) < 0 ? "" : attrName.substring(index + 1);
                    this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
                }
            }
            // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));
            // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));
            String ns = m_dh.getNamespaceOfNode(node);
            if (null == ns)
                ns = "";
            this.m_contentHandler.startElement(ns, m_dh.getLocalNameOfNode(node), node.getNodeName(), new AttList(atts, m_dh));
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            {
                ProcessingInstruction pi = (ProcessingInstruction) node;
                String name = pi.getNodeName();
                // String data = pi.getData();
                if (name.equals("xslt-next-is-raw")) {
                    nextIsRaw = true;
                } else {
                    this.m_contentHandler.processingInstruction(pi.getNodeName(), pi.getData());
                }
            }
            break;
        case Node.CDATA_SECTION_NODE:
            {
                boolean isLexH = (m_contentHandler instanceof LexicalHandler);
                LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;
                if (isLexH) {
                    lh.startCDATA();
                }
                dispatachChars(node);
                {
                    if (isLexH) {
                        lh.endCDATA();
                    }
                }
            }
            break;
        case Node.TEXT_NODE:
            {
                if (nextIsRaw) {
                    nextIsRaw = false;
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
                    dispatachChars(node);
                    m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
                } else {
                    dispatachChars(node);
                }
            }
            break;
        case Node.ENTITY_REFERENCE_NODE:
            {
                EntityReference eref = (EntityReference) node;
                if (m_contentHandler instanceof LexicalHandler) {
                    ((LexicalHandler) this.m_contentHandler).startEntity(eref.getNodeName());
                } else {
                // warning("Can not output entity to a pure SAX ContentHandler");
                }
            }
            break;
        default:
    }
}
Also used : Locator(org.xml.sax.Locator) NamedNodeMap(org.w3c.dom.NamedNodeMap) LexicalHandler(org.xml.sax.ext.LexicalHandler) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 19 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project j2objc by google.

the class TransformerIdentityImpl method createResultContentHandler.

/**
   * Create a result ContentHandler from a Result object, based
   * on the current OutputProperties.
   *
   * @param outputTarget Where the transform result should go,
   * should not be null.
   *
   * @return A valid ContentHandler that will create the
   * result tree when it is fed SAX events.
   *
   * @throws TransformerException
   */
private void createResultContentHandler(Result outputTarget) throws TransformerException {
    if (outputTarget instanceof SAXResult) {
        SAXResult saxResult = (SAXResult) outputTarget;
        m_resultContentHandler = saxResult.getHandler();
        m_resultLexicalHandler = saxResult.getLexicalHandler();
        if (m_resultContentHandler instanceof Serializer) {
            // Dubious but needed, I think.
            m_serializer = (Serializer) m_resultContentHandler;
        }
    } else if (outputTarget instanceof DOMResult) {
        DOMResult domResult = (DOMResult) outputTarget;
        Node outputNode = domResult.getNode();
        Node nextSibling = domResult.getNextSibling();
        Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (Node.DOCUMENT_NODE == type) ? (Document) outputNode : outputNode.getOwnerDocument();
        } else {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (ParserConfigurationException pce) {
                    }
                }
                DocumentBuilder db = dbf.newDocumentBuilder();
                doc = db.newDocument();
            } catch (ParserConfigurationException pce) {
                throw new TransformerException(pce);
            }
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder domBuilder = (Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            domBuilder.setNextSibling(nextSibling);
        m_resultContentHandler = domBuilder;
        m_resultLexicalHandler = domBuilder;
    } else if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            Serializer serializer = SerializerFactory.getSerializer(m_outputFormat.getProperties());
            m_serializer = serializer;
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0) {
                        fileURL = fileURL.substring(8);
                    } else {
                        fileURL = fileURL.substring(7);
                    }
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0) {
                        fileURL = fileURL.substring(6);
                    } else {
                        fileURL = fileURL.substring(5);
                    }
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            m_resultContentHandler = serializer.asContentHandler();
        } catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    // + outputTarget.getClass().getName()
    // + "!");
    }
    if (m_resultContentHandler instanceof DTDHandler)
        m_resultDTDHandler = (DTDHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof DeclHandler)
        m_resultDeclHandler = (DeclHandler) m_resultContentHandler;
    if (m_resultContentHandler instanceof LexicalHandler)
        m_resultLexicalHandler = (LexicalHandler) m_resultContentHandler;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMBuilder(org.apache.xml.utils.DOMBuilder) DeclHandler(org.xml.sax.ext.DeclHandler) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) LexicalHandler(org.xml.sax.ext.LexicalHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DTDHandler(org.xml.sax.DTDHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer)

Example 20 with LexicalHandler

use of org.xml.sax.ext.LexicalHandler in project j2objc by google.

the class TransformerImpl method executeChildTemplates.

/**
      * Execute each of the children of a template element.
      *
      * @param elem The ElemTemplateElement that contains the children
      * that should execute.
      * @param handler The ContentHandler to where the result events
      * should be fed.
      *
      * @throws TransformerException
      * @xsl.usage advanced
      */
public void executeChildTemplates(ElemTemplateElement elem, ContentHandler handler) throws TransformerException {
    SerializationHandler xoh = this.getSerializationHandler();
    // These may well not be the same!  In this case when calling
    // the Redirect extension, it has already set the ContentHandler
    // in the Transformer.
    SerializationHandler savedHandler = xoh;
    try {
        xoh.flushPending();
        // %REVIEW% Make sure current node is being pushed.
        LexicalHandler lex = null;
        if (handler instanceof LexicalHandler) {
            lex = (LexicalHandler) handler;
        }
        m_serializationHandler = new ToXMLSAXHandler(handler, lex, savedHandler.getEncoding());
        m_serializationHandler.setTransformer(this);
        executeChildTemplates(elem, true);
    } catch (TransformerException e) {
        throw e;
    } catch (SAXException se) {
        throw new TransformerException(se);
    } finally {
        m_serializationHandler = savedHandler;
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

LexicalHandler (org.xml.sax.ext.LexicalHandler)34 Node (org.w3c.dom.Node)14 IOException (java.io.IOException)11 TransformerException (javax.xml.transform.TransformerException)10 ContentHandler (org.xml.sax.ContentHandler)9 Element (org.w3c.dom.Element)8 EntityReference (org.w3c.dom.EntityReference)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 SAXException (org.xml.sax.SAXException)8 DTDHandler (org.xml.sax.DTDHandler)6 SAXResult (javax.xml.transform.sax.SAXResult)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 DOMResult (javax.xml.transform.dom.DOMResult)4 StreamResult (javax.xml.transform.stream.StreamResult)4 SerializationHandler (org.apache.xml.serializer.SerializationHandler)4 Serializer (org.apache.xml.serializer.Serializer)4 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)4 DOMBuilder (org.apache.xml.utils.DOMBuilder)4