Example usage for javax.xml.transform.stream StreamSource getSystemId

List of usage examples for javax.xml.transform.stream StreamSource getSystemId

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamSource getSystemId.

Prototype

@Override
public String getSystemId() 

Source Link

Document

Get the system identifier that was set with setSystemId.

Usage

From source file:Main.java

/**
 * Utility to get the bytes uri//  w w  w. j  a va  2 s  .  com
 *
 * @param source the resource to get
 */
public static InputSource sourceToInputSource(Source source) {
    if (source instanceof SAXSource) {
        return ((SAXSource) source).getInputSource();
    } else if (source instanceof DOMSource) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Node node = ((DOMSource) source).getNode();
        if (node instanceof Document) {
            node = ((Document) node).getDocumentElement();
        }
        Element domElement = (Element) node;
        ElementToStream(domElement, baos);
        InputSource isource = new InputSource(source.getSystemId());
        isource.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
        return isource;
    } else if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        InputSource isource = new InputSource(ss.getSystemId());
        isource.setByteStream(ss.getInputStream());
        isource.setCharacterStream(ss.getReader());
        isource.setPublicId(ss.getPublicId());
        return isource;
    } else {
        return getInputSourceFromURI(source.getSystemId());
    }
}

From source file:Utils.java

public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);//from ww w. j a v a 2 s  .com
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource is2 = new InputSource();
    is2.setSystemId(is.getSystemId());
    is2.setByteStream(is.getInputStream());
    is2.setCharacterStream(is.getReader());

    return db.parse(is2);
}

From source file:org.dataconservancy.packaging.tool.ser.JenaModelSerializer.java

/**
 * {@inheritDoc}//from w  ww. j a  v a  2  s. c om
 * <p>
 * Overridden in this implementation to always unmarshal an {@code InputStream} if it is available from {@code
 * streamSource}, instead of wrapping it with a SAX {@code InputSource}.
 * </p>
 *
 * @param streamSource {@inheritDoc}
 * @return {@inheritDoc}
 * @throws XmlMappingException {@inheritDoc}
 * @throws IOException {@inheritDoc}
 */
@Override
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
    InputStream in = streamSource.getInputStream();
    if (in != null) {
        return unmarshalInputStream(in);
    }

    Reader r = streamSource.getReader();
    if (r != null) {
        return unmarshalReader(streamSource.getReader());
    }

    return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
}

From source file:nl.nn.adapterframework.validation.xerces_2_11.XMLSchemaFactory.java

public Schema newSchema(Source[] schemas) throws SAXException {

    // this will let the loader store parsed Grammars into the pool.
    XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension();
    fXMLGrammarPoolWrapper.setGrammarPool(pool);

    XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length];
    InputStream inputStream;/*  ww w.j  a  v  a 2s  .  c o  m*/
    Reader reader;
    for (int i = 0; i < schemas.length; ++i) {
        Source source = schemas[i];
        if (source instanceof StreamSource) {
            StreamSource streamSource = (StreamSource) source;
            String publicId = streamSource.getPublicId();
            String systemId = streamSource.getSystemId();
            inputStream = streamSource.getInputStream();
            reader = streamSource.getReader();
            XMLInputSource xmlInputSource = new XMLInputSource(publicId, systemId, null);
            xmlInputSource.setByteStream(inputStream);
            xmlInputSource.setCharacterStream(reader);
            xmlInputSources[i] = xmlInputSource;
        } else if (source instanceof SAXSource) {
            SAXSource saxSource = (SAXSource) source;
            InputSource inputSource = saxSource.getInputSource();
            if (inputSource == null) {
                throw new SAXException(JAXPValidationMessageFormatter
                        .formatMessage(fXMLSchemaLoader.getLocale(), "SAXSourceNullInputSource", null));
            }
            xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource);
        } else if (source instanceof DOMSource) {
            DOMSource domSource = (DOMSource) source;
            Node node = domSource.getNode();
            String systemID = domSource.getSystemId();
            xmlInputSources[i] = new DOMInputSource(node, systemID);
        }
        //            else if (source instanceof StAXSource) {
        //                StAXSource staxSource = (StAXSource) source;
        //                XMLEventReader eventReader = staxSource.getXMLEventReader();
        //                if (eventReader != null) {
        //                    xmlInputSources[i] = new StAXInputSource(eventReader);
        //                }
        //                else {
        //                    xmlInputSources[i] = new StAXInputSource(staxSource.getXMLStreamReader());
        //                }
        //            }
        else if (source == null) {
            throw new NullPointerException(JAXPValidationMessageFormatter
                    .formatMessage(fXMLSchemaLoader.getLocale(), "SchemaSourceArrayMemberNull", null));
        } else {
            throw new IllegalArgumentException(
                    JAXPValidationMessageFormatter.formatMessage(fXMLSchemaLoader.getLocale(),
                            "SchemaFactorySourceUnrecognized", new Object[] { source.getClass().getName() }));
        }
    }

    try {
        fXMLSchemaLoader.loadGrammar(xmlInputSources);
    } catch (XNIException e) {
        // this should have been reported to users already.
        throw Util.toSAXException(e);
    } catch (IOException e) {
        // this hasn't been reported, so do so now.
        SAXParseException se = new SAXParseException(e.getMessage(), null, e);
        if (fErrorHandler != null) {
            fErrorHandler.error(se);
        }
        throw se; // and we must throw it.
    }

    // Clear reference to grammar pool.
    fXMLGrammarPoolWrapper.setGrammarPool(null);

    // Select Schema implementation based on grammar count.
    final int grammarCount = pool.getGrammarCount();
    AbstractXMLSchema schema = null;
    if (fUseGrammarPoolOnly) {
        throw new NotImplementedException("fUseGrammarPoolOnly");
        //            if (grammarCount > 1) {
        //                schema = new XMLSchemaHack(new ReadOnlyGrammarPool(pool));
        //            }
        //            else if (grammarCount == 1) {
        //                Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
        //                schema = new XMLSchemaHack(grammars[0]);
        //            }
        //            else {
        //                schema = new XMLSchemaHack();
        //            }
    } else {
        schema = new XMLSchema(new ReadOnlyGrammarPool(pool), false);
    }
    propagateFeatures(schema);
    return schema;
}

From source file:org.apache.ode.utils.DOMUtils.java

public static Document toDocumentFromStream(StreamSource source) throws IOException, SAXException {
    DocumentBuilder builder = getBuilder();
    Document document = null;//w w w  .ja  va  2s. co m
    Reader reader = source.getReader();
    if (reader != null) {
        document = builder.parse(new InputSource(reader));
    } else {
        InputStream inputStream = source.getInputStream();
        if (inputStream != null) {
            InputSource inputsource = new InputSource(inputStream);
            inputsource.setSystemId(source.getSystemId());
            document = builder.parse(inputsource);
        } else {
            throw new IOException("No input stream or reader available");
        }
    }
    return document;
}

From source file:org.dhatim.delivery.AbstractParser.java

protected static Reader getReader(Source source, String contentEncoding) {
    if (source != null) {
        if (source instanceof StreamSource) {
            StreamSource streamSource = (StreamSource) source;
            if (streamSource.getReader() != null) {
                return streamSource.getReader();
            } else if (streamSource.getInputStream() != null) {
                return streamToReader(streamSource.getInputStream(), contentEncoding);
            } else if (streamSource.getSystemId() != null) {
                return systemIdToReader(streamSource.getSystemId(), contentEncoding);
            }/*from   w  ww. jav a  2  s  .com*/

            throw new SmooksException("Invalid " + StreamSource.class.getName()
                    + ".  No InputStream, Reader or SystemId instance.");
        } else if (source.getSystemId() != null) {
            return systemIdToReader(source.getSystemId(), contentEncoding);
        }
    }

    return new NullReader();
}

From source file:org.dhatim.delivery.AbstractParser.java

protected InputStream getInputStream(StreamSource streamSource) {
    InputStream inputStream = streamSource.getInputStream();
    String systemId = streamSource.getSystemId();

    if (inputStream != null) {
        return inputStream;
    } else if (systemId != null) {
        return systemIdToStream(systemId);
    }/*from   w ww.  java  2s  .c o  m*/

    return null;
}

From source file:org.exist.repo.ExistRepository.java

public File resolveXQueryModule(String namespace) throws XPathException {
    // the URI//  w  w  w.j  a va  2 s .  c  o  m
    URI uri;
    try {
        uri = new URI(namespace);
    } catch (final URISyntaxException ex) {
        throw new XPathException("Invalid URI: " + namespace, ex);
    }
    for (final Packages pp : myParent.listPackages()) {
        final Package pkg = pp.latest();
        // FIXME: Rely on having a file system storage, that's probably a bad design!
        final FileSystemResolver resolver = (FileSystemResolver) pkg.getResolver();
        final ExistPkgInfo info = (ExistPkgInfo) pkg.getInfo("exist");
        if (info != null) {
            final String f = info.getXQuery(uri);
            if (f != null) {
                return resolver.resolveComponentAsFile(f);
            }
        }
        String sysid = null; // declared here to be used in catch
        try {
            final StreamSource src = pkg.resolve(namespace, URISpace.XQUERY);
            if (src != null) {
                sysid = src.getSystemId();
                return new File(new URI(sysid));
            }
        } catch (final URISyntaxException ex) {
            throw new XPathException("Error parsing the URI of the query library: " + sysid, ex);
        } catch (final PackageException ex) {
            throw new XPathException("Error resolving the query library: " + namespace, ex);
        }
    }
    return null;
}

From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java

@SuppressWarnings("deprecation") // on JDK 9
private Source processSource(Source source) {
    if (StaxUtils.isStaxSource(source) || source instanceof DOMSource) {
        return source;
    }/*www  .j a  va 2 s.co  m*/

    XMLReader xmlReader = null;
    InputSource inputSource = null;

    if (source instanceof SAXSource) {
        SAXSource saxSource = (SAXSource) source;
        xmlReader = saxSource.getXMLReader();
        inputSource = saxSource.getInputSource();
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        if (streamSource.getInputStream() != null) {
            inputSource = new InputSource(streamSource.getInputStream());
        } else if (streamSource.getReader() != null) {
            inputSource = new InputSource(streamSource.getReader());
        } else {
            inputSource = new InputSource(streamSource.getSystemId());
        }
    }

    try {
        if (xmlReader == null) {
            xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
        }
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        String name = "http://xml.org/sax/features/external-general-entities";
        xmlReader.setFeature(name, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        return new SAXSource(xmlReader, inputSource);
    } catch (SAXException ex) {
        logger.warn("Processing of external entities could not be disabled", ex);
        return source;
    }
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Template method for handling {@code StreamSource}s.
 * <p>This implementation delegates to {@code unmarshalInputStream} or {@code unmarshalReader}.
 * @param streamSource the {@code StreamSource}
 * @return the object graph/* w  w  w  .ja  v a 2s.c o  m*/
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given source cannot be mapped to an object
 */
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
    if (streamSource.getInputStream() != null) {
        if (isProcessExternalEntities() && isSupportDtd()) {
            return unmarshalInputStream(streamSource.getInputStream());
        } else {
            InputSource inputSource = new InputSource(streamSource.getInputStream());
            inputSource.setEncoding(getDefaultEncoding());
            return unmarshalSaxSource(new SAXSource(inputSource));
        }
    } else if (streamSource.getReader() != null) {
        if (isProcessExternalEntities() && isSupportDtd()) {
            return unmarshalReader(streamSource.getReader());
        } else {
            return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader())));
        }
    } else {
        return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
    }
}