Java XML String Transform getSchemaFromResource(String... files)

Here you can find the source of getSchemaFromResource(String... files)

Description

get Schema From Resource

License

Apache License

Declaration

public static Schema getSchemaFromResource(String... files) throws SAXException, FileNotFoundException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.FileNotFoundException;

import java.io.InputStream;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Main {
    public static Schema getSchemaFromResource(String... files) throws SAXException, FileNotFoundException {
        InputStream[] inputStreams = new InputStream[files.length];
        Schema schema = null;//from ww w.  j  av  a  2  s  .  co m
        try {
            for (int i = 0; i < files.length; i++) {
                inputStreams[i] = loadResourceInputStream(files[i]);

            }

            schema = getSchema(inputStreams);

        } finally {
            for (InputStream is : inputStreams) {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (Exception ex) {
                }
            }
        }

        return schema;
    }

    private static InputStream loadResourceInputStream(String file) throws FileNotFoundException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        InputStream inputStream = classLoader.getResourceAsStream(file);

        if (inputStream == null) {
            throw new FileNotFoundException("File: '" + file + "', not found.");
        }

        return inputStream;
    }

    public static Schema getSchema(InputStream... inputs) throws SAXException {
        StreamSource[] streamSource = new StreamSource[inputs.length];

        for (int i = 0; i < inputs.length; i++) {
            streamSource[i] = new StreamSource(inputs[i]);
        }

        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(streamSource);

        return schema;
    }
}

Related

  1. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  2. getOutputMimeType(String xslName)
  3. getOutputSQLPageXML(String inStatement, Map map)
  4. getPropertyAsString(final Properties prop, final String comment)
  5. getSchema(String schemaString)
  6. getSourceAsString(Source source)
  7. getStreamResultForFile(String filename)
  8. getString(Node node, boolean indent)
  9. getStringFromStreamSource(StreamSource src)