Search in sources :

Example 41 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ProcessorExsltFunction method validate.

/**
   * Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
   * there are no literal result elements except within a func:result element and that
   * the func:result element does not contain any following siblings except xsl:fallback.
   */
public void validate(ElemTemplateElement elem, StylesheetHandler handler) throws SAXException {
    String msg = "";
    while (elem != null) {
        //System.out.println("elem " + elem);
        if (elem instanceof ElemExsltFuncResult && elem.getNextSiblingElem() != null && !(elem.getNextSiblingElem() instanceof ElemFallback)) {
            msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
            handler.error(msg, new SAXException(msg));
        }
        if ((elem instanceof ElemApplyImport || elem instanceof ElemApplyTemplates || elem instanceof ElemAttribute || elem instanceof ElemCallTemplate || elem instanceof ElemComment || elem instanceof ElemCopy || elem instanceof ElemCopyOf || elem instanceof ElemElement || elem instanceof ElemLiteralResult || elem instanceof ElemNumber || elem instanceof ElemPI || elem instanceof ElemText || elem instanceof ElemTextLiteral || elem instanceof ElemValueOf) && !(ancestorIsOk(elem))) {
            msg = "misplaced literal result in a func:function container.";
            handler.error(msg, new SAXException(msg));
        }
        ElemTemplateElement nextElem = elem.getFirstChildElem();
        while (nextElem == null) {
            nextElem = elem.getNextSiblingElem();
            if (nextElem == null)
                elem = elem.getParentElem();
            if (elem == null || elem instanceof ElemExsltFunction)
                // ok
                return;
        }
        elem = nextElem;
    }
}
Also used : ElemApplyImport(org.apache.xalan.templates.ElemApplyImport) ElemFallback(org.apache.xalan.templates.ElemFallback) ElemExsltFuncResult(org.apache.xalan.templates.ElemExsltFuncResult) ElemCallTemplate(org.apache.xalan.templates.ElemCallTemplate) ElemApplyTemplates(org.apache.xalan.templates.ElemApplyTemplates) ElemComment(org.apache.xalan.templates.ElemComment) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) SAXException(org.xml.sax.SAXException) ElemCopy(org.apache.xalan.templates.ElemCopy) ElemText(org.apache.xalan.templates.ElemText) ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) ElemLiteralResult(org.apache.xalan.templates.ElemLiteralResult) ElemAttribute(org.apache.xalan.templates.ElemAttribute) ElemCopyOf(org.apache.xalan.templates.ElemCopyOf) ElemElement(org.apache.xalan.templates.ElemElement) ElemPI(org.apache.xalan.templates.ElemPI) ElemExsltFunction(org.apache.xalan.templates.ElemExsltFunction) ElemNumber(org.apache.xalan.templates.ElemNumber) ElemValueOf(org.apache.xalan.templates.ElemValueOf)

Example 42 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class SerializerUtils method addAttribute.

/**
     * Copy an DOM attribute to the created output element, executing
     * attribute templates as need be, and processing the xsl:use
     * attribute.
     *
     * @param handler SerializationHandler to which the attributes are added.
     * @param attr Attribute node to add to SerializationHandler.
     *
     * @throws TransformerException
     */
public static void addAttribute(SerializationHandler handler, int attr) throws TransformerException {
    TransformerImpl transformer = (TransformerImpl) handler.getTransformer();
    DTM dtm = transformer.getXPathContext().getDTM(attr);
    if (SerializerUtils.isDefinedNSDecl(handler, attr, dtm))
        return;
    String ns = dtm.getNamespaceURI(attr);
    if (ns == null)
        ns = "";
    // %OPT% ...can I just store the node handle?
    try {
        handler.addAttribute(ns, dtm.getLocalName(attr), dtm.getNodeName(attr), "CDATA", dtm.getNodeValue(attr), false);
    } catch (SAXException e) {
    // do something?
    }
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) DTM(org.apache.xml.dtm.DTM) SAXException(org.xml.sax.SAXException)

Example 43 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class ElemAttribute method constructNode.

/**
   * Construct a node in the result tree.  This method is overloaded by 
   * xsl:attribute. At this class level, this method creates an element.
   *
   * @param nodeName The name of the node, which may be null.
   * @param prefix The prefix for the namespace, which may be null.
   * @param nodeNamespace The namespace of the node, which may be null.
   * @param transformer non-null reference to the the current transform-time state.
   * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
   * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.
   *
   * @throws TransformerException
   */
void constructNode(String nodeName, String prefix, String nodeNamespace, TransformerImpl transformer) throws TransformerException {
    if (null != nodeName && nodeName.length() > 0) {
        SerializationHandler rhandler = transformer.getSerializationHandler();
        // Evaluate the value of this attribute
        String val = transformer.transformToString(this);
        try {
            // Let the result tree handler add the attribute and its String value.
            String localName = QName.getLocalPart(nodeName);
            if (prefix != null && prefix.length() > 0) {
                rhandler.addAttribute(nodeNamespace, localName, nodeName, "CDATA", val, true);
            } else {
                rhandler.addAttribute("", localName, nodeName, "CDATA", val, true);
            }
        } catch (SAXException e) {
        }
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) SAXException(org.xml.sax.SAXException)

Example 44 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class XMLReaderManager method getXMLReader.

/**
     * Retrieves a cached XMLReader for this thread, or creates a new
     * XMLReader, if the existing reader is in use.  When the caller no
     * longer needs the reader, it must release it with a call to
     * {@link #releaseXMLReader}.
     */
public synchronized XMLReader getXMLReader() throws SAXException {
    XMLReader reader;
    boolean readerInUse;
    if (m_readers == null) {
        // When the m_readers.get() method is called for the first time
        // on a thread, a new XMLReader will automatically be created.
        m_readers = new ThreadLocal();
    }
    if (m_inUse == null) {
        m_inUse = new Hashtable();
    }
    // If the cached reader for this thread is in use, construct a new
    // one; otherwise, return the cached reader.
    reader = (XMLReader) m_readers.get();
    boolean threadHasReader = (reader != null);
    if (!threadHasReader || m_inUse.get(reader) == Boolean.TRUE) {
        try {
            try {
                // According to JAXP 1.2 specification, if a SAXSource
                // is created using a SAX InputSource the Transformer or
                // TransformerFactory creates a reader via the
                // XMLReaderFactory if setXMLReader is not used
                reader = XMLReaderFactory.createXMLReader();
            } catch (Exception e) {
                try {
                    // the XMLReader from JAXP
                    if (m_parserFactory == null) {
                        m_parserFactory = SAXParserFactory.newInstance();
                        m_parserFactory.setNamespaceAware(true);
                    }
                    reader = m_parserFactory.newSAXParser().getXMLReader();
                } catch (ParserConfigurationException pce) {
                    // pass along pce
                    throw pce;
                }
            }
            try {
                reader.setFeature(NAMESPACES_FEATURE, true);
                reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false);
            } catch (SAXException se) {
            // Try to carry on if we've got a parser that
            // doesn't know about namespace prefixes.
            }
        } catch (ParserConfigurationException ex) {
            throw new SAXException(ex);
        } catch (FactoryConfigurationError ex1) {
            throw new SAXException(ex1.toString());
        } catch (NoSuchMethodError ex2) {
        } catch (AbstractMethodError ame) {
        }
        // a reader for this thread.
        if (!threadHasReader) {
            m_readers.set(reader);
            m_inUse.put(reader, Boolean.TRUE);
        }
    } else {
        m_inUse.put(reader, Boolean.TRUE);
    }
    return reader;
}
Also used : Hashtable(java.util.Hashtable) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) XMLReader(org.xml.sax.XMLReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 45 with SAXException

use of org.xml.sax.SAXException in project j2objc by google.

the class DocumentBuilderTest method testNewDocument.

/**
     *  javax.xml.parsers.DocumentBuilder#getSchema()
     *  TBD getSchema() is not supported
     */
/*   public void test_getSchema() {
        assertNull(db.getSchema());
        SchemaFactory sf =
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            Schema schema = sf.newSchema();
            dbf.setSchema(schema);
            assertNotNull(dbf.newDocumentBuilder().getSchema());
        } catch (ParserConfigurationException pce) {
            fail("Unexpected ParserConfigurationException " + pce.toString());
        } catch (SAXException sax) {
            fail("Unexpected SAXException " + sax.toString());
        }
    }
*/
public void testNewDocument() {
    Document d;
    try {
        d = dbf.newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertNotNull(d);
    assertNull(d.getDoctype());
    assertNull(d.getDocumentElement());
    assertNull(d.getNamespaceURI());
}
Also used : Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXException (org.xml.sax.SAXException)1248 IOException (java.io.IOException)754 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)466 Document (org.w3c.dom.Document)316 DocumentBuilder (javax.xml.parsers.DocumentBuilder)265 InputSource (org.xml.sax.InputSource)249 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)203 InputStream (java.io.InputStream)160 Element (org.w3c.dom.Element)143 NodeList (org.w3c.dom.NodeList)131 File (java.io.File)129 SAXParseException (org.xml.sax.SAXParseException)119 Node (org.w3c.dom.Node)106 ByteArrayInputStream (java.io.ByteArrayInputStream)99 StringReader (java.io.StringReader)92 TransformerException (javax.xml.transform.TransformerException)91 SAXParser (javax.xml.parsers.SAXParser)83 FileInputStream (java.io.FileInputStream)78 ArrayList (java.util.ArrayList)78 XMLReader (org.xml.sax.XMLReader)70