Search in sources :

Example 1 with SAXParser

use of javax.xml.parsers.SAXParser in project tomcat by apache.

the class JspDocumentParser method parse.

/*
     * Parses a JSP document by responding to SAX events.
     *
     * @throws JasperException
     */
public static Node.Nodes parse(ParserController pc, String path, Jar jar, Node parent, boolean isTagFile, boolean directivesOnly, String pageEnc, String jspConfigPageEnc, boolean isEncodingSpecifiedInProlog, boolean isBomPresent) throws JasperException {
    JspDocumentParser jspDocParser = new JspDocumentParser(pc, path, isTagFile, directivesOnly);
    Node.Nodes pageNodes = null;
    try {
        // Create dummy root and initialize it with given page encodings
        Node.Root dummyRoot = new Node.Root(null, parent, true);
        dummyRoot.setPageEncoding(pageEnc);
        dummyRoot.setJspConfigPageEncoding(jspConfigPageEnc);
        dummyRoot.setIsEncodingSpecifiedInProlog(isEncodingSpecifiedInProlog);
        dummyRoot.setIsBomPresent(isBomPresent);
        jspDocParser.current = dummyRoot;
        if (parent == null) {
            jspDocParser.addInclude(dummyRoot, jspDocParser.pageInfo.getIncludePrelude());
        } else {
            jspDocParser.isTop = false;
        }
        jspDocParser.isValidating = false;
        // Parse the input
        SAXParser saxParser = getSAXParser(false, jspDocParser);
        InputSource source = JspUtil.getInputSource(path, jar, jspDocParser.ctxt);
        try {
            saxParser.parse(source, jspDocParser);
        } catch (EnableDTDValidationException e) {
            saxParser = getSAXParser(true, jspDocParser);
            jspDocParser.isValidating = true;
            try {
                source.getByteStream().close();
            } catch (IOException e2) {
            // ignore
            }
            source = JspUtil.getInputSource(path, jar, jspDocParser.ctxt);
            saxParser.parse(source, jspDocParser);
        } finally {
            try {
                source.getByteStream().close();
            } catch (IOException e) {
            // ignore
            }
        }
        if (parent == null) {
            jspDocParser.addInclude(dummyRoot, jspDocParser.pageInfo.getIncludeCoda());
        }
        // Create Node.Nodes from dummy root
        pageNodes = new Node.Nodes(dummyRoot);
    } catch (IOException ioe) {
        jspDocParser.err.jspError(ioe, "jsp.error.data.file.read", path);
    } catch (SAXParseException e) {
        jspDocParser.err.jspError(new Mark(jspDocParser.ctxt, path, e.getLineNumber(), e.getColumnNumber()), e, e.getMessage());
    } catch (Exception e) {
        jspDocParser.err.jspError(e, "jsp.error.data.file.processing", path);
    }
    return pageNodes;
}
Also used : InputSource(org.xml.sax.InputSource) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) JasperException(org.apache.jasper.JasperException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 2 with SAXParser

use of javax.xml.parsers.SAXParser in project cogtool by cogtool.

the class ObjectLoader method load.

/**
     * Based on the serialization contained by the given input source
     * and the initial aggregate loader (that is, for top-level objects
     * that are themselves aggregates), reconstitute the top-level objects
     * and return them in an ordered collection.
     * <p>
     * The caller is responsible for recovering any resources associated
     * with the input source (such as <code>close()</code>).
     *
     * @param src the previously saved serialization of a sequence of objects
     * @param initialLoader the loader to use for creating top-level aggregate
     *                      objects; it may be <code>null</code> if no
     *                      top-level object is an aggregate
     * @throws ParserConfigurationException based on the SAX parsing
     * @throws SAXException based on the SAX parsing
     * @throws java.io.IOException if the source generates one
     *         during read calls generated by the SAX parser
     * @throws java.io.IOException
     */
@SuppressWarnings("unchecked")
public List<Object> load(InputSource src, IAggregateLoader initialLoader) throws ParserConfigurationException, SAXException, java.io.IOException {
    activeLoaders.push((initialLoader != null) ? initialLoader : AAggregateLoader.ONLY);
    // A List will keep the reconstituted objects in order
    pendingObjects.push(new ArrayList<Object>());
    pushObjectState(IN_COLLECTION);
    SAXParser p = parserFactory.newSAXParser();
    // The List is populated during the parse
    p.parse(src, this);
    return (List<Object>) pendingObjects.peek();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SAXParser

use of javax.xml.parsers.SAXParser in project OpenPanodroid by duerrfk.

the class RESTRequestorXML method parseResponse.

@Override
protected void parseResponse(InputStream is, int contentLength) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        DefaultHandler saxHandler = createSAXHandler();
        if (saxHandler == null) {
            setSuccessState(false);
        } else {
            parser.parse(is, saxHandler);
        }
    } catch (Exception ex) {
        setErrorMsg(ex.getLocalizedMessage());
        setSuccessState(false);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 4 with SAXParser

use of javax.xml.parsers.SAXParser in project che by eclipse.

the class RefactoringSessionReader method createParser.

/**
	 * Creates a new parser from the specified factory.
	 *
	 * @param factory
	 *            the parser factoring to use
	 * @return the created parser
	 * @throws ParserConfigurationException
	 *             if no parser is available with the given configuration
	 * @throws SAXException
	 *             if an error occurs while creating the parser
	 */
private SAXParser createParser(final SAXParserFactory factory) throws ParserConfigurationException, SAXException {
    final SAXParser parser = factory.newSAXParser();
    final XMLReader reader = parser.getXMLReader();
    try {
        //$NON-NLS-1$
        reader.setFeature("http://xml.org/sax/features/validation", false);
        //$NON-NLS-1$
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (SAXNotRecognizedException exception) {
    // Do nothing
    } catch (SAXNotSupportedException exception) {
    // Do nothing
    }
    return parser;
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXParser(javax.xml.parsers.SAXParser) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) XMLReader(org.xml.sax.XMLReader)

Example 5 with SAXParser

use of javax.xml.parsers.SAXParser in project che by eclipse.

the class CheCodeFormatterOptions method getCheDefaultSettings.

private Map<String, String> getCheDefaultSettings() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLParser parserXML = new XMLParser();
    try {
        SAXParser parser = factory.newSAXParser();
        parser.parse(getClass().getResourceAsStream(DEFAULT_CODESTYLE), parserXML);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOG.error("It is not possible to parse file " + DEFAULT_CODESTYLE, e);
    }
    return parserXML.getSettings();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9