Example usage for org.w3c.dom.ls LSInput setByteStream

List of usage examples for org.w3c.dom.ls LSInput setByteStream

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSInput setByteStream.

Prototype

public void setByteStream(java.io.InputStream byteStream);

Source Link

Document

An attribute of a language and binding dependent type that represents a stream of bytes.

Usage

From source file:Main.java

public static Document parse(InputStream s, boolean validateIfSchema) {
    DOMImplementationLS impl = getDOMImpl();
    LSInput input = impl.createLSInput();
    input.setByteStream(s);
    return parse(input, validateIfSchema);
}

From source file:com.lostinsoftware.xsdparser.XSDParser.java

private static XSDElement parseXSD(InputStream inputStream, List<String> elements, boolean allElements)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        ClassCastException {//from  ww w .ja  v  a 2 s .co  m

    XSDElement mainElement = null;

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

    XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");

    XSLoader schemaLoader = impl.createXSLoader(null);

    DOMConfiguration config = schemaLoader.getConfig();

    // create Error Handler
    DOMErrorHandler errorHandler = new XSDParser();

    // set error handler
    config.setParameter("error-handler", errorHandler);

    // set validation feature
    config.setParameter("validate", Boolean.TRUE);

    // parse document
    LSInput input = new DOMInputImpl();
    input.setByteStream(inputStream);
    XSModel model = schemaLoader.load(input);
    if (model != null) {
        // Main element
        XSElementDeclaration element = model.getElementDeclaration(elements.get(0), null);
        mainElement = new XSDElement();
        mainElement.setName(elements.get(0));
        mainElement.setXsDeclaration(element);
        processElement(mainElement, elements, allElements);
    }
    return mainElement;
}

From source file:com.emc.ia.sipcreator.testing.xdb.TemporaryXDBResource.java

public void add(final String name, final InputStream document, final Map<String, String> vars)
        throws SAXException {

    XhiveSessionIf session = null;//from  w  ww.  ja v a  2 s.  c om
    try {
        session = getSession();
        session.begin();
        final XhiveDatabaseIf database = session.getDatabase();
        final XhiveLibraryIf root = database.getRoot();
        final XhiveLSParserIf parser = root.createLSParser();
        final LSInput input = root.createLSInput();
        input.setByteStream(document);
        final XhiveDocumentIf doc = parser.parse(input);
        doc.setName(name);
        root.appendChild(doc);
        final XhiveMetadataMapIf metadata = doc.getMetadata();
        metadata.putAll(vars);

        session.commit();
    } finally {
        if (session != null) {
            session.rollback();
            releaseSession(session);
        }
    }
}

From source file:de.betterform.xml.xforms.model.Model.java

private XSModel loadSchema(InputStream stream)
        throws IllegalAccessException, ClassNotFoundException, InstantiationException {
    LSInput input = new DOMInputImpl();
    input.setByteStream(stream);

    return getSchemaLoader().load(input);
}

From source file:org.apache.syncope.core.logic.init.CamelRouteLoader.java

private void loadRoutes(final String domain, final DataSource dataSource, final Resource resource,
        final AnyTypeKind anyTypeKind) {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    boolean shouldLoadRoutes = jdbcTemplate.queryForList(
            String.format("SELECT * FROM %s WHERE ANYTYPEKIND = ?", CamelRoute.class.getSimpleName()),
            new Object[] { anyTypeKind.name() }).isEmpty();

    if (shouldLoadRoutes) {
        try {/*from  w  ww .  j  av a2s. c  om*/
            TransformerFactory tf = null;
            DOMImplementationLS domImpl = null;
            NodeList routeNodes;
            if (IS_JBOSS) {
                tf = TransformerFactory.newInstance();
                tf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                dbFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(resource.getInputStream());

                routeNodes = doc.getDocumentElement().getElementsByTagName("route");
            } else {
                DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
                domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
                LSInput lsinput = domImpl.createLSInput();
                lsinput.setByteStream(resource.getInputStream());

                LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

                routeNodes = parser.parse(lsinput).getDocumentElement().getElementsByTagName("route");
            }

            for (int s = 0; s < routeNodes.getLength(); s++) {
                Node routeElement = routeNodes.item(s);
                String routeContent = IS_JBOSS ? nodeToString(routeNodes.item(s), tf)
                        : nodeToString(routeNodes.item(s), domImpl);
                String routeId = ((Element) routeElement).getAttribute("id");

                jdbcTemplate.update(
                        String.format("INSERT INTO %s(ID, ANYTYPEKIND, CONTENT) VALUES (?, ?, ?)",
                                CamelRoute.class.getSimpleName()),
                        new Object[] { routeId, anyTypeKind.name(), routeContent });
                LOG.info("[{}] Route successfully loaded: {}", domain, routeId);
            }
        } catch (Exception e) {
            LOG.error("[{}] Route load failed", domain, e);
        }
    }
}

From source file:org.apache.syncope.core.provisioning.camel.SyncopeCamelContext.java

private void loadContext(final Collection<String> routes) {
    try {/*  w  w  w.j a va 2  s  .  c o m*/
        DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImpl = (DOMImplementationLS) reg.getDOMImplementation("LS");
        LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

        JAXBContext jaxbContext = JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        List<RouteDefinition> routeDefs = new ArrayList<>();
        for (String route : routes) {
            try (InputStream input = IOUtils.toInputStream(route, StandardCharsets.UTF_8)) {
                LSInput lsinput = domImpl.createLSInput();
                lsinput.setByteStream(input);

                Node routeElement = parser.parse(lsinput).getDocumentElement();
                routeDefs.add(unmarshaller.unmarshal(routeElement, RouteDefinition.class).getValue());
            }
        }
        camelContext.addRouteDefinitions(routeDefs);
    } catch (Exception e) {
        LOG.error("While loading Camel context {}", e);
        throw new CamelException(e);
    }
}

From source file:org.betaconceptframework.astroboa.configuration.W3CRelatedSchemaEntityResolver.java

@Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId,
        String baseURI) {//from   w w w . j  av a  2s  .  c  o  m

    InputSource entity;

    try {
        entity = locateEntity(systemId, publicId);
    } catch (IOException e) {
        throw new CmsException(e);
    }

    if (entity == null || entity.getByteStream() == null) {
        return null;
    }

    DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSInput lsInput = domImplementationLS.createLSInput();

    lsInput.setByteStream(entity.getByteStream());
    lsInput.setSystemId(systemId);

    return lsInput;
}

From source file:org.codice.ddf.spatial.ogc.wfs.catalog.endpoint.writer.TestFeatureCollectionMessageBodyWriter.java

@Test
public void testWriteToGeneratesGMLConformantXml() throws IOException, WebApplicationException, SAXException {

    FeatureCollectionMessageBodyWriter wtr = new FeatureCollectionMessageBodyWriter();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    wtr.writeTo(getWfsFeatureCollection(), null, null, null, null, null, stream);
    String actual = stream.toString();

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(new LSResourceResolver() {

        private Map<String, String> schemaLocations;

        private Map<String, LSInput> inputs;

        {/*from   w w w  .j a v  a  2  s.c o m*/
            inputs = new HashMap<String, LSInput>();

            schemaLocations = new HashMap<String, String>();

            schemaLocations.put("xml.xsd", "/w3/1999/xml.xsd");
            schemaLocations.put("xlink.xsd", "/w3/1999/xlink.xsd");
            schemaLocations.put("geometry.xsd", "/gml/2.1.2/geometry.xsd");
            schemaLocations.put("feature.xsd", "/gml/2.1.2/feature.xsd");
            schemaLocations.put("gml.xsd", "/gml/2.1.2/gml.xsd");
            schemaLocations.put("expr.xsd", "/filter/1.0.0/expr.xsd");
            schemaLocations.put("filter.xsd", "/filter/1.0.0/filter.xsd");
            schemaLocations.put("filterCapabilities.xsd", "/filter/1.0.0/filterCapabilties.xsd");
            schemaLocations.put("WFS-capabilities.xsd", "/wfs/1.0.0/WFS-capabilities.xsd");
            schemaLocations.put("OGC-exception.xsd", "/wfs/1.0.0/OGC-exception.xsd");
            schemaLocations.put("WFS-basic.xsd", "/wfs/1.0.0/WFS-basic.xsd");
            schemaLocations.put("WFS-transaction.xsd", "/wfs/1.0.0/WFS-transaction.xsd");
            schemaLocations.put("wfs.xsd", "/wfs/1.0.0/wfs.xsd");
        }

        @Override
        public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId,
                String baseURI) {
            String fileName = new java.io.File(systemId).getName();
            if (inputs.containsKey(fileName)) {
                return inputs.get(fileName);
            }

            LSInput input = new DOMInputImpl();
            InputStream is = getClass().getResourceAsStream(schemaLocations.get(fileName));
            input.setByteStream(is);
            input.setBaseURI(baseURI);
            input.setSystemId(systemId);
            inputs.put(fileName, input);
            return input;
        }
    });

    Source wfsSchemaSource = new StreamSource(getClass().getResourceAsStream("/wfs/1.0.0/wfs.xsd"));
    Source testSchemaSource = new StreamSource(getClass().getResourceAsStream("/schema.xsd"));

    Schema schema = schemaFactory.newSchema(new Source[] { wfsSchemaSource, testSchemaSource });

    try {
        schema.newValidator().validate(new StreamSource(new StringReader(actual)));
    } catch (Exception e) {
        fail("Generated GML Response does not conform to WFS Schema" + e.getMessage());
    }
}

From source file:org.sonar.plugins.xml.schemas.SchemaResolver.java

private static LSInput createLSInput(InputStream inputStream) {
    if (inputStream != null) {
        System.setProperty(DOMImplementationRegistry.PROPERTY,
                "org.apache.xerces.dom.DOMImplementationSourceImpl");

        try {/*ww w. jav a  2 s . c o m*/
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
            DOMImplementation impl = registry.getDOMImplementation("XML 1.0 LS 3.0");
            DOMImplementationLS implls = (DOMImplementationLS) impl;
            LSInput lsInput = implls.createLSInput();
            lsInput.setByteStream(inputStream);
            return lsInput;
        } catch (ClassNotFoundException e) {
            throw new SonarException(e);
        } catch (InstantiationException e) {
            throw new SonarException(e);
        } catch (IllegalAccessException e) {
            throw new SonarException(e);
        }
    }
    return null;
}

From source file:org.xwiki.officeimporter.internal.filter.AbstractHTMLFilterTest.java

protected Document filter(String content, Map<String, String> cleaningParameters) {
    LSInput input = this.lsImpl.createLSInput();
    String actualContent = "<root>" + content + "</root>";
    input.setByteStream(new ByteArrayInputStream(actualContent.getBytes()));
    Document document = XMLUtils.parse(input);
    this.filter.filter(document, cleaningParameters);
    return document;
}