当前位置: 首页>>代码示例>>Java>>正文


Java SchemaFactory类代码示例

本文整理汇总了Java中javax.xml.validation.SchemaFactory的典型用法代码示例。如果您正苦于以下问题:Java SchemaFactory类的具体用法?Java SchemaFactory怎么用?Java SchemaFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SchemaFactory类属于javax.xml.validation包,在下文中一共展示了SchemaFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Test
public void test() throws SAXException, ParserConfigurationException, IOException {
    Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema)));

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setSchema(schema);
    // saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace",
    // true);

    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader xmlReader = saxParser.getXMLReader();

    xmlReader.setContentHandler(new MyContentHandler());

    // InputStream input =
    // ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml");

    InputStream input = getClass().getResourceAsStream("Bug6946312.xml");
    System.out.println("Parse InputStream:");
    xmlReader.parse(new InputSource(input));
    if (!charEvent) {
        Assert.fail("missing character event");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:Bug6946312Test.java

示例2: parse

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
 * Parse the given XML string an create/update the corresponding entities
 * 
 * @param xml
 *            the XML string
 * @return the parse return code
 * @throws Exception
 */
public int parse(byte[] xml) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    SchemaFactory sf = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try (InputStream inputStream = ResourceLoader.getResourceAsStream(
            getClass(), getSchemaName())) {
        Schema schema = sf.newSchema(new StreamSource(inputStream));
        spf.setSchema(schema);
    }
    SAXParser saxParser = spf.newSAXParser();
    XMLReader reader = saxParser.getXMLReader();
    reader.setFeature(Constants.XERCES_FEATURE_PREFIX
            + Constants.DISALLOW_DOCTYPE_DECL_FEATURE, true);
    reader.setContentHandler(this);
    reader.parse(new InputSource(new ByteArrayInputStream(xml)));
    return 0;
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:27,代码来源:ProductImportParser.java

示例3: rulesXmlIsValid

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Test
public void rulesXmlIsValid() {
	RulesXmlReaderFactory xmlFactory = new RulesXmlReaderFactory();

	try (Reader xmlReader = xmlFactory.newRulesXmlReader(); Reader xsdReader = xmlFactory.newRulesXsdReader()) {
		StreamSource xsdStreamSource = new StreamSource(xsdReader);
		StreamSource xmlStreamSource = new StreamSource(xmlReader);

		SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = schemaFactory.newSchema(xsdStreamSource);
		Validator validator = schema.newValidator();

		validator.validate(xmlStreamSource);
	} catch (Exception e) {
		fail("rules.xml does not conform to schema!");
	}
}
 
开发者ID:Wikia,项目名称:sonar-php-rules,代码行数:18,代码来源:IntegrationTest.java

示例4: fromXML

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
public T fromXML(String xml) {

    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller u = context.createUnmarshaller();

        if(schemaLocation != null) {
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            StreamSource source = new StreamSource(getClass().getResourceAsStream(schemaLocation));
            Schema schema = schemaFactory.newSchema(source);
            u.setSchema(schema);
        }

        StringReader reader = new StringReader(xml);
        T obj = (T) u.unmarshal(reader);

        return obj;
    } catch (Exception e) {
        System.out.println("ERROR: "+e.toString());
        return null;
    }
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:25,代码来源:ConverterImpl.java

示例5: test

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Test
public void test() {

    String dir = Bug6943252Test.class.getResource("Bug6943252In").getPath();
    File inputs = new File(dir);
    File[] files = inputs.listFiles();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    for (int i = 0; i < files.length; i++) {
        try {
            Schema schema = schemaFactory.newSchema(new StreamSource(files[i]));
            Assert.fail(files[i].getName() + "should fail");
        } catch (SAXException e) {
            // expected
            System.out.println(files[i].getName() + ":");
            System.out.println(e.getMessage());
        }
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:Bug6943252Test.java

示例6: getCompiledSchema

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
 * Subclasses can use this to get a compiled schema object.
 * @param schemas Input stream of schemas.
 * @param lsResourceResolver  resolver can be supplied optionally. Otherwise pass null.
 * @return  Compiled Schema object.
 */
protected Schema getCompiledSchema(InputStream[] schemas,
        LSResourceResolver lsResourceResolver) {
    
    Schema schema = null;
    // Convert InputStream[] to StreamSource[]
    StreamSource[] schemaStreamSources = new StreamSource[schemas.length];
    for(int index1=0 ; index1<schemas.length ; index1++)
        schemaStreamSources[index1] = new StreamSource(schemas[index1]);
    
    // Create a compiled Schema object.
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(lsResourceResolver);
    try {
        schema = schemaFactory.newSchema(schemaStreamSources);            
    } catch(SAXException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex);
    } 
    
    return schema;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:XsdBasedValidator.java

示例7: validate

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
  protected void validate(Model model, Schema schema, XsdBasedValidator.Handler handler) {
      try {
          SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
          CatalogModel cm = (CatalogModel) model.getModelSource().getLookup()
.lookup(CatalogModel.class);
   if (cm != null) {
              sf.setResourceResolver(cm);
          }
          sf.setErrorHandler(handler);
          Source saxSource = getSource(model, handler);
          if (saxSource == null) {
              return;
          }
          sf.newSchema(saxSource);
      } catch(SAXException sax) {
          //already processed by handler
      } catch(Exception ex) {
          handler.logValidationErrors(Validator.ResultType.ERROR, ex.getMessage());
      }
  }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:SchemaXsdBasedValidator.java

示例8: validation

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
	 * 利用xsd验证xml
	 * @param xsdFile xsdFile
	 * @param xmlInput xmlInput
	 * @throws SAXException  SAXException
	 * @throws IOException IOException
	 */
	public static void validation(String xsdFile, InputStream xmlInput) throws SAXException, IOException
	{
		SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		URL xsdURL = Validation.class.getClassLoader().getResource(xsdFile);
		if(xsdURL != null)
		{
			Schema schema = factory.newSchema(xsdURL);
			Validator validator = schema.newValidator();
//			validator.setErrorHandler(new AutoErrorHandler());

			Source source = new StreamSource(xmlInput);
			
			try(OutputStream resultOut = new FileOutputStream(new File(PathUtil.getRootDir(), xsdFile + ".xml")))
			{
				Result result = new StreamResult(resultOut);
				validator.validate(source, result);
			}
		}
		else
		{
			throw new FileNotFoundException(String.format("can not found xsd file [%s] from classpath.", xsdFile));
		}
	}
 
开发者ID:LinuxSuRen,项目名称:phoenix.webui.framework,代码行数:31,代码来源:Validation.java

示例9: XMLValidator

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
 *  Constructor for the XMLValidator object
 *
 * @param  uri            NOT YET DOCUMENTED
 * @exception  Exception  NOT YET DOCUMENTED
 */
public XMLValidator(URI uri) throws Exception {
	this.uri = uri;
	SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	Schema schema = null;
	try {
		String uriScheme = uri.getScheme();
		if (uriScheme != null && uriScheme.equals("http"))
			schema = factory.newSchema(uri.toURL());
		else
			schema = factory.newSchema(new File(uri));
		if (schema == null)
			throw new Exception("Schema could not be read from " + uri.toString());
	} catch (Throwable t) {
		throw new Exception("Validator init error: " + t.getMessage());
	}

	this.validator = schema.newValidator();
}
 
开发者ID:NCAR,项目名称:joai-project,代码行数:25,代码来源:XMLValidator.java

示例10: setConfFile

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
public void setConfFile(String confFile) throws Exception {
    this.confFile = confFile;

    Object root;
    try {
        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
        final SchemaFactory schemaFact = SchemaFactory.newInstance(
                javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL url = ObjectFactory.class.getResource("/xsd/httpserver.xsd");
        jaxbUnmarshaller.setSchema(schemaFact.newSchema(url));

        root = jaxbUnmarshaller.unmarshal(new File(confFile));
    } catch (Exception ex) {
        throw new Exception("parsing config file failed, message: " + ex.getMessage(), ex);
    }

    if (root instanceof Httpservers) {
        this.conf = (Httpservers) root;
    } else if (root instanceof JAXBElement) {
        this.conf = (Httpservers) ((JAXBElement<?>) root).getValue();
    } else {
        throw new Exception("invalid root element type");
    }
}
 
开发者ID:xipki,项目名称:xitk,代码行数:26,代码来源:FileHttpServersConf.java

示例11: validate

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));

        // Get a Validator which can be used to validate instance document
        // against this grammar.
        Validator validator = schema.newValidator();
        ErrorHandler eh = new ErrorHandlerImpl();
        validator.setErrorHandler(eh);

        // Validate this instance document against the
        // Instance document supplied
        validator.validate(src, result);
    } catch (Exception ex) {
        throw ex;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:ValidatorTest.java

示例12: loadSchemaFiles

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
public Schema loadSchemaFiles() {
    try (InputStream brStream = ResourceLoader.getResourceAsStream(
            BillingDataRetrievalServiceBean.class, "BillingResult.xsd");
            InputStream localeStream = ResourceLoader.getResourceAsStream(
                    BillingDataRetrievalServiceBean.class, "Locale.xsd")) {

        URL billingResultUri = ResourceLoader.getResource(
                BillingDataRetrievalServiceBean.class, "BillingResult.xsd");
        URL localeUri = ResourceLoader.getResource(
                BillingDataRetrievalServiceBean.class, "Locale.xsd");
        SchemaFactory schemaFactory = SchemaFactory
                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        StreamSource[] sourceDocuments = new StreamSource[] {
                new StreamSource(localeStream, localeUri.getPath()),
                new StreamSource(brStream, billingResultUri.getPath()) };
        return schemaFactory.newSchema(sourceDocuments);
    } catch (SAXException | IOException e) {
        throw new BillingRunFailed("Schema files couldn't be loaded", e);
    }
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:22,代码来源:BillingDataRetrievalServiceBean.java

示例13: toXML

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
public String toXML(T obj) {

    try {
        JAXBContext context = JAXBContext.newInstance(type);

        Marshaller m = context.createMarshaller();
        if(schemaLocation != null) {
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            StreamSource source = new StreamSource(getClass().getResourceAsStream(schemaLocation));
            Schema schema = schemaFactory.newSchema(source);
            m.setSchema(schema);
        }

        StringWriter writer = new StringWriter();

        m.marshal(obj, writer);
        String xml = writer.toString();

        return xml;
    } catch (Exception e) {
        System.out.println("ERROR: "+e.toString());
        return null;
    }
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:27,代码来源:ConverterImpl.java

示例14: XMLConfigurator

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
 * Constructor.
 * 
 * @param retainXML whether to retain the XML configuration elements within the {@link Configuration}.
 * 
 * @throws ConfigurationException thrown if the validation schema for configuration files can not be created
 * 
 * @deprecated this method will be removed once {@link Configuration} no longer has the option to store the XML configuration fragements
 */
public XMLConfigurator(boolean retainXML) throws ConfigurationException {
    retainXMLConfiguration = retainXML;
    parserPool = new BasicParserPool();
    SchemaFactory factory = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaSource = new StreamSource(XMLConfigurator.class
            .getResourceAsStream(XMLConstants.XMLTOOLING_SCHEMA_LOCATION));
    try {
        configurationSchema = factory.newSchema(schemaSource);

        parserPool.setIgnoreComments(true);
        parserPool.setIgnoreElementContentWhitespace(true);
        parserPool.setSchema(configurationSchema);
    } catch (SAXException e) {
        throw new ConfigurationException("Unable to read XMLTooling configuration schema", e);
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:26,代码来源:XMLConfigurator.java

示例15: initialize

import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@PostConstruct
@Override
public synchronized void initialize() throws ProviderFactoryException {
    if (providersHolder.get() == null) {
        final File providersConfigFile = properties.getProvidersConfigurationFile();
        if (providersConfigFile.exists()) {
            try {
                // find the schema
                final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                final Schema schema = schemaFactory.newSchema(StandardProviderFactory.class.getResource(PROVIDERS_XSD));

                // attempt to unmarshal
                final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
                unmarshaller.setSchema(schema);

                // set the holder for later use
                final JAXBElement<Providers> element = unmarshaller.unmarshal(new StreamSource(providersConfigFile), Providers.class);
                providersHolder.set(element.getValue());
            } catch (SAXException | JAXBException e) {
                throw new ProviderFactoryException("Unable to load the providers configuration file at: " + providersConfigFile.getAbsolutePath(), e);
            }
        } else {
            throw new ProviderFactoryException("Unable to find the providers configuration file at " + providersConfigFile.getAbsolutePath());
        }
    }
}
 
开发者ID:apache,项目名称:nifi-registry,代码行数:27,代码来源:StandardProviderFactory.java


注:本文中的javax.xml.validation.SchemaFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。