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

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

Introduction

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

Prototype

public String getPublicId() 

Source Link

Document

Get the public identifier that was set with setPublicId.

Usage

From source file:Main.java

/**
 * Utility to get the bytes uri//from w ww.ja v  a 2 s .c o  m
 *
 * @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: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;/*w ww . j  av  a2  s  .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;
}