Search in sources :

Example 1 with XSLoader

use of org.apache.xerces.xs.XSLoader in project winery by eclipse.

the class BackendUtils method getXSModel.

/**
 * @param ref the file to read from
 */
public static Optional<XSModel> getXSModel(final RepositoryFileReference ref) {
    Objects.requireNonNull(ref);
    final InputStream is;
    try {
        is = RepositoryFactory.getRepository().newInputStream(ref);
    } catch (IOException e) {
        BackendUtils.LOGGER.debug("Could not create input stream", e);
        return Optional.empty();
    }
    // we rely on xerces to parse the XSD
    // idea based on http://stackoverflow.com/a/5165177/873282
    XSImplementation impl = new XSImplementationImpl();
    XSLoader schemaLoader = impl.createXSLoader(null);
    // minimal LSInput implementation sufficient for XSLoader in Oracle's JRE7
    LSInput input = new LSInput() {

        @Override
        public void setSystemId(String systemId) {
        }

        @Override
        public void setStringData(String stringData) {
        }

        @Override
        public void setPublicId(String publicId) {
        }

        @Override
        public void setEncoding(String encoding) {
        }

        @Override
        public void setCharacterStream(Reader characterStream) {
        }

        @Override
        public void setCertifiedText(boolean certifiedText) {
        }

        @Override
        public void setByteStream(InputStream byteStream) {
        }

        @Override
        public void setBaseURI(String baseURI) {
        }

        @Override
        public String getSystemId() {
            return null;
        }

        @Override
        public String getStringData() {
            return null;
        }

        @Override
        public String getPublicId() {
            return BackendUtils.getPathInsideRepo(ref);
        }

        @Override
        public String getEncoding() {
            return "UTF-8";
        }

        @Override
        public Reader getCharacterStream() {
            try {
                return new InputStreamReader(is, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                System.out.println("exeption");
                throw new IllegalStateException("UTF-8 is unkown", e);
            }
        }

        @Override
        public boolean getCertifiedText() {
            return false;
        }

        @Override
        public InputStream getByteStream() {
            return null;
        }

        @Override
        public String getBaseURI() {
            return null;
        }
    };
    return Optional.ofNullable(schemaLoader.load(input));
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XSImplementationImpl(org.apache.xerces.impl.xs.XSImplementationImpl) LSInput(org.w3c.dom.ls.LSInput) XSLoader(org.apache.xerces.xs.XSLoader) XSImplementation(org.apache.xerces.xs.XSImplementation) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 XSImplementationImpl (org.apache.xerces.impl.xs.XSImplementationImpl)1 XSImplementation (org.apache.xerces.xs.XSImplementation)1 XSLoader (org.apache.xerces.xs.XSLoader)1 LSInput (org.w3c.dom.ls.LSInput)1