Search in sources :

Example 16 with SAXNotSupportedException

use of org.xml.sax.SAXNotSupportedException in project rdf4j by eclipse.

the class AbstractSPARQLXMLParser method parseQueryResultInternal.

protected boolean parseQueryResultInternal(InputStream in, boolean attemptParseBoolean, boolean attemptParseTuple) throws IOException, QueryResultParseException, QueryResultHandlerException {
    if (!attemptParseBoolean && !attemptParseTuple) {
        throw new IllegalArgumentException("Internal error: Did not specify whether to parse as either boolean and/or tuple");
    }
    BufferedInputStream buff = new BufferedInputStream(in);
    // Wrap in a custom InputStream that doesn't allow close to be called by dependencies before we are ready to call it
    UncloseableInputStream uncloseable = new UncloseableInputStream(buff);
    SAXException caughtException = null;
    boolean result = false;
    try {
        if (attemptParseBoolean) {
            buff.mark(Integer.MAX_VALUE);
            try {
                SPARQLBooleanSAXParser valueParser = new SPARQLBooleanSAXParser();
                XMLReader xmlReader;
                if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
                    xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
                } else {
                    xmlReader = XMLReaderFactory.createXMLReader();
                }
                xmlReader.setErrorHandler(this);
                // not explicitly set
                for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) {
                    try {
                        xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // not explicitly set
                for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) {
                    try {
                        xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(valueParser);
                internalSAXParser.parse(uncloseable);
                result = valueParser.getValue();
                try {
                    if (this.handler != null) {
                        this.handler.handleBoolean(result);
                    }
                } catch (QueryResultHandlerException e) {
                    if (e.getCause() != null && e.getCause() instanceof IOException) {
                        throw (IOException) e.getCause();
                    } else {
                        throw new QueryResultParseException("Found an issue with the query result handler", e);
                    }
                }
                // result;
                return result;
            } catch (SAXException e) {
                caughtException = e;
            }
            // Reset the buffered input stream and try again looking for tuple
            // results
            buff.reset();
        }
        if (attemptParseTuple) {
            try {
                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                xmlReader.setErrorHandler(this);
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(new SPARQLResultsSAXParser(this.valueFactory, this.handler));
                internalSAXParser.parse(uncloseable);
                // we had success, so remove the exception that we were tracking
                // from
                // the boolean failure
                caughtException = null;
            } catch (SAXException e) {
                caughtException = e;
            }
        }
        if (caughtException != null) {
            Exception wrappedExc = caughtException.getException();
            if (wrappedExc == null) {
                throw new QueryResultParseException(caughtException);
            } else if (wrappedExc instanceof QueryResultParseException) {
                throw (QueryResultParseException) wrappedExc;
            } else if (wrappedExc instanceof QueryResultHandlerException) {
                throw (QueryResultHandlerException) wrappedExc;
            } else {
                throw new QueryResultParseException(wrappedExc);
            }
        }
    } finally {
        // Explicitly call the delegator to the close method to actually close it
        uncloseable.doClose();
    }
    return result;
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) IOException(java.io.IOException) QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) BufferedInputStream(java.io.BufferedInputStream) UncloseableInputStream(org.eclipse.rdf4j.common.io.UncloseableInputStream) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) XMLReader(org.xml.sax.XMLReader)

Example 17 with SAXNotSupportedException

use of org.xml.sax.SAXNotSupportedException in project webtools.sourceediting by eclipse.

the class JAXPSAXProcessorInvoker method addStylesheet.

protected Transformer addStylesheet(Source source, URIResolver resolver, Map parameters, Properties outputProperties) throws TransformerConfigurationException {
    if (tFactory == null)
        createTransformerFactory();
    TransformerHandler newTh = tFactory.newTransformerHandler(source);
    Transformer transformer = newTh.getTransformer();
    if (resolver != null)
        transformer.setURIResolver(resolver);
    if (parameters != null) {
        for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iter.next();
            String name = (String) entry.getKey();
            Object value = entry.getValue();
            // $NON-NLS-1$ //$NON-NLS-2$
            log.info(Messages.getString("JAXPSAXProcessorInvoker.2") + name + Messages.getString("JAXPSAXProcessorInvoker.3") + value);
            transformer.setParameter(name, value);
        }
    }
    if (outputProperties != null) {
        StringBuffer sb = new StringBuffer();
        for (Iterator iter = outputProperties.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iter.next();
            // $NON-NLS-1$ //$NON-NLS-2$
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append(" ");
        }
        if (outputProperties.size() > 0) {
            // $NON-NLS-1$
            log.info(Messages.getString("JAXPSAXProcessorInvoker.6") + sb.toString());
            transformer.setOutputProperties(outputProperties);
        }
    }
    if (th != null)
        th.setResult(new SAXResult(newTh));
    else {
        reader.setContentHandler(newTh);
        try {
            // $NON-NLS-1$
            reader.setProperty("http://xml.org/sax/properties/lexical-handler", newTh);
        } catch (SAXNotRecognizedException ex) {
            // $NON-NLS-1$
            log.warn(Messages.getString("JAXPSAXProcessorInvoker_4"));
        } catch (SAXNotSupportedException e) {
            // $NON-NLS-1$
            log.warn(Messages.getString("JAXPSAXProcessorInvoker_5"));
        }
    }
    th = newTh;
    return th.getTransformer();
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXResult(javax.xml.transform.sax.SAXResult) Iterator(java.util.Iterator) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) Map(java.util.Map)

Example 18 with SAXNotSupportedException

use of org.xml.sax.SAXNotSupportedException in project zm-mailbox by Zimbra.

the class W3cDomUtil method makeSAXParserFactory.

public static SAXParserFactory makeSAXParserFactory() throws XmlParseException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setXIncludeAware(false);
    factory.setValidating(false);
    try {
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        // XXE attack prevention
        factory.setFeature(Constants.DISALLOW_DOCTYPE_DECL, true);
        factory.setFeature(Constants.EXTERNAL_GENERAL_ENTITIES, false);
        factory.setFeature(Constants.EXTERNAL_PARAMETER_ENTITIES, false);
        factory.setFeature(Constants.LOAD_EXTERNAL_DTD, false);
    } catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException ex) {
        ZimbraLog.misc.error("Problem setting up SAXParser which supports secure XML processing", ex);
        throw XmlParseException.PARSE_ERROR();
    }
    return factory;
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 19 with SAXNotSupportedException

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

the class DTMManagerDefault method getDTM.

/**
 * Get an instance of a DTM, loaded with the content from the
 * specified source.  If the unique flag is true, a new instance will
 * always be returned.  Otherwise it is up to the DTMManager to return a
 * new instance or an instance that it already created and may be being used
 * by someone else.
 *
 * A bit of magic in this implementation: If the source is null, unique is true,
 * and incremental and doIndexing are both false, we return an instance of
 * SAX2RTFDTM, which see.
 *
 * (I think more parameters will need to be added for error handling, and entity
 * resolution, and more explicit control of the RTF situation).
 *
 * @param source the specification of the source object.
 * @param unique true if the returned DTM must be unique, probably because it
 * is going to be mutated.
 * @param whiteSpaceFilter Enables filtering of whitespace nodes, and may
 *                         be null.
 * @param incremental true if the DTM should be built incrementally, if
 *                    possible.
 * @param doIndexing true if the caller considers it worth it to use
 *                   indexing schemes.
 *
 * @return a non-null DTM reference.
 */
public synchronized DTM getDTM(Source source, boolean unique, DTMWSFilter whiteSpaceFilter, boolean incremental, boolean doIndexing) {
    if (DEBUG && null != source)
        System.out.println("Starting " + (unique ? "UNIQUE" : "shared") + " source: " + source.getSystemId());
    XMLStringFactory xstringFactory = m_xsf;
    int dtmPos = getFirstFreeDTMID();
    int documentID = dtmPos << IDENT_DTM_NODE_BITS;
    if ((null != source) && source instanceof DOMSource) {
        DOM2DTM dtm = new DOM2DTM(this, (DOMSource) source, documentID, whiteSpaceFilter, xstringFactory, doIndexing);
        addDTM(dtm, dtmPos, 0);
        return dtm;
    } else {
        boolean isSAXSource = (null != source) ? (source instanceof SAXSource) : true;
        boolean isStreamSource = (null != source) ? (source instanceof StreamSource) : false;
        if (isSAXSource || isStreamSource) {
            XMLReader reader = null;
            SAX2DTM dtm;
            try {
                InputSource xmlSource;
                if (null == source) {
                    xmlSource = null;
                } else {
                    reader = getXMLReader(source);
                    xmlSource = SAXSource.sourceToInputSource(source);
                    String urlOfSource = xmlSource.getSystemId();
                    if (null != urlOfSource) {
                        try {
                            urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource);
                        } catch (Exception e) {
                            // %REVIEW% Is there a better way to send a warning?
                            System.err.println("Can not absolutize URL: " + urlOfSource);
                        }
                        xmlSource.setSystemId(urlOfSource);
                    }
                }
                if (source == null && unique && !incremental && !doIndexing) {
                    // Special case to support RTF construction into shared DTM.
                    // It should actually still work for other uses,
                    // but may be slightly deoptimized relative to the base
                    // to allow it to deal with carrying multiple documents.
                    // 
                    // %REVIEW% This is a sloppy way to request this mode;
                    // we need to consider architectural improvements.
                    dtm = new SAX2RTFDTM(this, source, documentID, whiteSpaceFilter, xstringFactory, doIndexing);
                } else /**
                 ************************************************************
                 *          // EXPERIMENTAL 3/22/02
                 *          else if(JKESS_XNI_EXPERIMENT && m_incremental) {
                 *            dtm = new XNI2DTM(this, source, documentID, whiteSpaceFilter,
                 *                              xstringFactory, doIndexing);
                 *          }
                 *************************************************************
                 */
                // Create the basic SAX2DTM.
                {
                    dtm = new SAX2DTM(this, source, documentID, whiteSpaceFilter, xstringFactory, doIndexing);
                }
                // Go ahead and add the DTM to the lookup table.  This needs to be
                // done before any parsing occurs. Note offset 0, since we've just
                // created a new DTM.
                addDTM(dtm, dtmPos, 0);
                boolean haveXercesParser = (null != reader) && (reader.getClass().getName().equals("org.apache.xerces.parsers.SAXParser"));
                if (haveXercesParser) {
                    // No matter what.  %REVIEW%
                    incremental = true;
                }
                // build, then we still want to set up the IncrementalSAXSource stuff.
                if (m_incremental && incremental) /* || ((null == reader) && incremental) */
                {
                    IncrementalSAXSource coParser = null;
                    if (haveXercesParser) {
                        // IncrementalSAXSource_Xerces to avoid threading.
                        try {
                            coParser = (IncrementalSAXSource) Class.forName("org.apache.xml.dtm.ref.IncrementalSAXSource_Xerces").newInstance();
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            coParser = null;
                        }
                    }
                    if (coParser == null) {
                        // Create a IncrementalSAXSource to run on the secondary thread.
                        if (null == reader) {
                            coParser = new IncrementalSAXSource_Filter();
                        } else {
                            IncrementalSAXSource_Filter filter = new IncrementalSAXSource_Filter();
                            filter.setXMLReader(reader);
                            coParser = filter;
                        }
                    }
                    /**
                     ************************************************************
                     *            // EXPERIMENTAL 3/22/02
                     *            if (JKESS_XNI_EXPERIMENT && m_incremental &&
                     *                  dtm instanceof XNI2DTM &&
                     *                  coParser instanceof IncrementalSAXSource_Xerces) {
                     *                org.apache.xerces.xni.parser.XMLPullParserConfiguration xpc=
                     *                      ((IncrementalSAXSource_Xerces)coParser)
                     *                                           .getXNIParserConfiguration();
                     *              if (xpc!=null) {
                     *                // Bypass SAX; listen to the XNI stream
                     *                ((XNI2DTM)dtm).setIncrementalXNISource(xpc);
                     *              } else {
                     *                  // Listen to the SAX stream (will fail, diagnostically...)
                     *                dtm.setIncrementalSAXSource(coParser);
                     *              }
                     *            } else
                     **************************************************************
                     */
                    // Have the DTM set itself up as IncrementalSAXSource's listener.
                    dtm.setIncrementalSAXSource(coParser);
                    if (null == xmlSource) {
                        // Then the user will construct it themselves.
                        return dtm;
                    }
                    if (null == reader.getErrorHandler()) {
                        reader.setErrorHandler(dtm);
                    }
                    reader.setDTDHandler(dtm);
                    try {
                        // Launch parsing coroutine.  Launches a second thread,
                        // if we're using IncrementalSAXSource.filter().
                        coParser.startParse(xmlSource);
                    } catch (RuntimeException re) {
                        dtm.clearCoRoutine();
                        throw re;
                    } catch (Exception e) {
                        dtm.clearCoRoutine();
                        throw new org.apache.xml.utils.WrappedRuntimeException(e);
                    }
                } else {
                    if (null == reader) {
                        // Then the user will construct it themselves.
                        return dtm;
                    }
                    // not incremental
                    reader.setContentHandler(dtm);
                    reader.setDTDHandler(dtm);
                    if (null == reader.getErrorHandler()) {
                        reader.setErrorHandler(dtm);
                    }
                    try {
                        reader.setProperty("http://xml.org/sax/properties/lexical-handler", dtm);
                    } catch (SAXNotRecognizedException e) {
                    } catch (SAXNotSupportedException e) {
                    }
                    try {
                        reader.parse(xmlSource);
                    } catch (RuntimeException re) {
                        dtm.clearCoRoutine();
                        throw re;
                    } catch (Exception e) {
                        dtm.clearCoRoutine();
                        throw new org.apache.xml.utils.WrappedRuntimeException(e);
                    }
                }
                if (DUMPTREE) {
                    System.out.println("Dumping SAX2DOM");
                    dtm.dumpDTM(System.err);
                }
                return dtm;
            } finally {
                // after creating the DTM.
                if (reader != null && !(m_incremental && incremental)) {
                    reader.setContentHandler(m_defaultHandler);
                    reader.setDTDHandler(m_defaultHandler);
                    reader.setErrorHandler(m_defaultHandler);
                    // Reset the LexicalHandler to null after creating the DTM.
                    try {
                        reader.setProperty("http://xml.org/sax/properties/lexical-handler", null);
                    } catch (Exception e) {
                    }
                }
                releaseXMLReader(reader);
            }
        } else {
            // "Not supported: " + source);
            throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object[] { source }));
        }
    }
}
Also used : DTMException(org.apache.xml.dtm.DTMException) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) DOM2DTM(org.apache.xml.dtm.ref.dom2dtm.DOM2DTM) StreamSource(javax.xml.transform.stream.StreamSource) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) DTMException(org.apache.xml.dtm.DTMException) SAXException(org.xml.sax.SAXException) XMLStringFactory(org.apache.xml.utils.XMLStringFactory) SAXSource(javax.xml.transform.sax.SAXSource) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAX2DTM(org.apache.xml.dtm.ref.sax2dtm.SAX2DTM) SAX2RTFDTM(org.apache.xml.dtm.ref.sax2dtm.SAX2RTFDTM) XMLReader(org.xml.sax.XMLReader)

Example 20 with SAXNotSupportedException

use of org.xml.sax.SAXNotSupportedException in project robovm by robovm.

the class SAXNotSupportedExceptionTest method testSAXNotSupportedException_String.

public void testSAXNotSupportedException_String() {
    SAXNotSupportedException e = new SAXNotSupportedException(ERR);
    assertEquals(ERR, e.getMessage());
    e = new SAXNotSupportedException(null);
    assertNull(e.getMessage());
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException)

Aggregations

SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)33 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)25 XMLReader (org.xml.sax.XMLReader)16 SAXException (org.xml.sax.SAXException)13 SAXParser (javax.xml.parsers.SAXParser)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 SAXParserFactory (javax.xml.parsers.SAXParserFactory)8 InputSource (org.xml.sax.InputSource)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 SAXSource (javax.xml.transform.sax.SAXSource)4 BufferedInputStream (java.io.BufferedInputStream)3 Reader (java.io.Reader)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 Source (javax.xml.transform.Source)2 Transformer (javax.xml.transform.Transformer)2 DOMSource (javax.xml.transform.dom.DOMSource)2