Example usage for javax.xml.parsers SAXParserFactory setNamespaceAware

List of usage examples for javax.xml.parsers SAXParserFactory setNamespaceAware

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory setNamespaceAware.

Prototype


public void setNamespaceAware(boolean awareness) 

Source Link

Document

Specifies that the parser produced by this code will provide support for XML namespaces.

Usage

From source file:org.kuali.kra.test.OjbRepositoryMappingTest.java

/**
 * @param repositoryFilePath//from ww  w.j  av  a  2 s . c om
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private void validateXml(String repositoryFilePath) throws Exception {
    LOG.debug(String.format("Starting XML validation"));
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(false);

    SAXParser parser = saxParserFactory.newSAXParser();
    final InputStream repositoryStream = getFileResource(repositoryFilePath).getInputStream();
    parser.parse(repositoryStream, new DefaultHandler());
}

From source file:org.kuali.rice.core.impl.config.property.JAXBConfigImpl.java

protected org.kuali.rice.core.impl.config.property.Config unmarshal(Unmarshaller unmarshaller, InputStream in)
        throws SAXException, ParserConfigurationException, IOException, IllegalStateException, JAXBException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);

    XMLFilter filter = new ConfigNamespaceURIFilter();
    filter.setParent(spf.newSAXParser().getXMLReader());

    UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler();
    filter.setContentHandler(handler);//from   w w  w .ja  va2 s.c  om

    filter.parse(new InputSource(in));

    return (org.kuali.rice.core.impl.config.property.Config) handler.getResult();
}

From source file:org.olat.ims.qti.qpool.ItemFileResourceValidator.java

private boolean validateDocument(Document in) {
    try {//from  w w w . j  ava 2s.  com
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);

        SimpleErrorHandler errorHandler = new SimpleErrorHandler();
        ItemContentHandler contentHandler = new ItemContentHandler();

        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(new IMSEntityResolver());
        reader.setErrorHandler(errorHandler);
        reader.setContentHandler(contentHandler);

        SAXValidator validator = new SAXValidator(reader);
        validator.validate(in);

        return errorHandler.isValid() && contentHandler.isItem();
    } catch (ParserConfigurationException e) {
        return false;
    } catch (SAXException e) {
        return false;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.omnaest.utils.xml.JAXBXMLHelper.java

/**
 * Returns a new {@link JAXBContextBasedUnmarshaller} instance. This can be used to marshal multiple {@link InputStream}s
 * without the costly overhead of constructing a new {@link JAXBContext} each time.
 * /*from w  w w .j a v  a 2s .c om*/
 * @param type
 * @param unmarshallingConfiguration
 * @return new {@link JAXBContextBasedUnmarshaller}
 */
public static <E> JAXBContextBasedUnmarshaller<E> newJAXBContextBasedUnmarshaller(Class<E> type,
        UnmarshallingConfiguration unmarshallingConfiguration) {
    //
    JAXBContextBasedUnmarshaller<E> retval = null;

    //
    unmarshallingConfiguration = UnmarshallingConfiguration
            .defaultUnmarshallingConfiguration(unmarshallingConfiguration);
    final ExceptionHandler exceptionHandler = unmarshallingConfiguration.getExceptionHandler();
    final Class<?>[] knownTypes = unmarshallingConfiguration.getKnownTypes();
    final String encoding = unmarshallingConfiguration.getEncoding();
    final Configurator configurator = unmarshallingConfiguration.getConfigurator();

    //
    try {
        //
        final Class<?>[] contextTypes = ArrayUtils.add(knownTypes, type);
        final JAXBContext jaxbContext = JAXBContext.newInstance(contextTypes);
        if (configurator != null) {
            configurator.configure(jaxbContext);
        }

        //
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        if (configurator != null) {
            configurator.configure(unmarshaller);
        }

        //
        final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        saxParserFactory.setNamespaceAware(true);
        if (configurator != null) {
            configurator.configure(saxParserFactory);
        }

        //      
        final XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();

        //
        retval = new JAXBContextBasedUnmarshaller<E>(unmarshaller, xmlReader, exceptionHandler, encoding, type);
    } catch (Exception e) {
        if (exceptionHandler != null) {
            exceptionHandler.handleException(e);
        }
    }

    //
    return retval;
}

From source file:org.onehippo.repository.bootstrap.instructions.ContentResourceInstruction.java

InputStream getPartialContentInputStream(InputStream in, final String contextRelPath)
        throws IOException, RepositoryException {
    File file = File.createTempFile("bootstrap-", ".xml");
    OutputStream out = null;//from  w  ww . j a  v  a2  s.  c  o  m
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
        SAXParser parser = factory.newSAXParser();

        out = new FileOutputStream(file);
        TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance())
                .newTransformerHandler();
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        handler.setResult(new StreamResult(out));

        parser.parse(new InputSource(in),
                new DefaultContentHandler(new PartialSystemViewFilter(handler, contextRelPath)));
        return new TempFileInputStream(file);
    } catch (FactoryConfigurationError e) {
        throw new RepositoryException("SAX parser implementation not available", e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException("SAX parser configuration error", e);
    } catch (SAXException e) {
        Exception exception = e.getException();
        if (exception instanceof RepositoryException) {
            throw (RepositoryException) exception;
        } else if (exception instanceof IOException) {
            throw (IOException) exception;
        } else {
            throw new InvalidSerializedDataException("Error parsing XML import", e);
        }
    } catch (TransformerConfigurationException e) {
        throw new RepositoryException("SAX transformation error", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.onehippo.repository.bootstrap.util.ContentFileInfo.java

public static ContentFileInfo readInfo(final Node item) {
    ContentFileInfoReader contentFileInfoReader = null;
    InputStream in = null;//from  ww  w  .j av a2 s .  c  o  m
    String contentResource = null;
    try {
        contentResource = StringUtils.trim(item.getProperty(HIPPO_CONTENTRESOURCE).getString());
        if (contentResource.endsWith(".zip") || contentResource.endsWith(".jar")) {
            return null;
        }
        final String contentRoot = StringUtils.trim(item.getProperty(HIPPO_CONTENTROOT).getString());
        contentFileInfoReader = new ContentFileInfoReader(contentRoot);
        final URL contentResourceURL = getResource(item, contentResource);
        if (contentResourceURL != null) {
            in = contentResourceURL.openStream();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
            factory.newSAXParser().parse(new InputSource(in), contentFileInfoReader);
        }
    } catch (ContentFileInfoReadingShortCircuitException ignore) {
    } catch (FactoryConfigurationError | SAXException | ParserConfigurationException | IOException
            | RepositoryException e) {
        BootstrapConstants.log.error("Could not read root node name from content file {}", contentResource, e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return contentFileInfoReader == null ? null : contentFileInfoReader.getContentFileInfo();
}

From source file:org.onehippo.repository.bootstrap.util.PartialSystemViewFilterTest.java

public String getPartialContent(InputStream in, String startPath) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    SAXParser parser = factory.newSAXParser();

    StringWriter out = new StringWriter();
    TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance())
            .newTransformerHandler();/*from w  w  w. j  av  a  2 s  . c om*/
    Transformer transformer = handler.getTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    handler.setResult(new StreamResult(out));

    parser.parse(new InputSource(in),
            new DefaultContentHandler(new PartialSystemViewFilter(handler, startPath)));
    return out.toString();
}

From source file:org.openbravo.test.webservice.BaseWSTest.java

/**
 * Validates the xml against the generated schema.
 * /*from   w  w w.  j  a va2 s . c  o  m*/
 * @param xml
 *          the xml to validate
 */
protected void validateXML(String xml) {
    final Reader schemaReader = new StringReader(getXMLSchema());
    final Reader xmlReader = new StringReader(xml);
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(true);

        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource(schemaReader) }));

        SAXParser parser = factory.newSAXParser();

        XMLReader reader = parser.getXMLReader();
        reader.setErrorHandler(new SimpleErrorHandler());
        reader.parse(new InputSource(xmlReader));
    } catch (Exception e) {
        throw new OBException(e);
    }

}

From source file:org.opencastproject.metadata.dublincore.DublinCoreXmlFormat.java

private DublinCoreCatalog readImpl(InputSource in)
        throws ParserConfigurationException, SAXException, IOException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    // no DTD//  www  . j  a  v  a  2 s  .c  om
    factory.setValidating(false);
    // namespaces!
    factory.setNamespaceAware(true);
    // read document                                   
    factory.newSAXParser().parse(in, this);
    return dc;
}

From source file:org.openlca.io.EcoSpoldUnitFetch.java

public EcoSpoldUnitFetch() {
    try {/*from  w ww.j a  v  a 2s .com*/
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        parser = factory.newSAXParser();
    } catch (Exception e) {
        throw new RuntimeException("Could not create parser", e);
    }
}