org.apache.xerces.impl.xs.XMLSchemaLoader Java Examples

The following examples show how to use org.apache.xerces.impl.xs.XMLSchemaLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLModelSchemaValidator.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException {
	if (rootElement) {
		// on the document element, we associate the XML Schema declared in <?xml-model
		// processing instruction
		String defaultNamespace = attributes.getValue(XMLNS_ATTR);
		if (StringUtils.isEmpty(defaultNamespace)) {
			// XML doesn't define a xmlns attribute in the root element -> same support than
			// xsi:noNamespaceSchemaLocation. Ex:
			/**
			 * <?xml-model href="http://www.docbook.org/xml/5.0/xsd/docbook.xsd"?> <book>
			 **/
			String noNamespaceSchemaLocation = href;
			XMLSchemaLoader.processExternalHints(null, noNamespaceSchemaLocation, fLocationPairs, errorReporter);
		} else {
			// XML defines a xmlns attribute in the root element -> same support than
			// xsi:schemaLocation. Ex:
			/**
			 * <?xml-model href="http://www.docbook.org/xml/5.0/xsd/docbook.xsd"?>
			 * <book xmlns="http://docbook.org/ns/docbook">
			 **/
			String schemaLocation = new StringBuilder(defaultNamespace).append(' ').append(href).toString();
			XMLSchemaLoader.processExternalHints(schemaLocation, null, fLocationPairs, errorReporter);
		}
		rootElement = false;
	}
	super.startElement(element, attributes, augs);
}
 
Example #2
Source File: XSDValidator.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create the XML Schema loader to use to validate the XML Schema.
 * 
 * @param reporter the lsp reporter.
 * @return the XML Schema loader to use to validate the XML Schema.
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private static XMLSchemaLoader createSchemaLoader(XMLErrorReporter reporter) {
	XMLSchemaLoader schemaLoader = new XMLSchemaLoader();

	// To validate XML syntax for XML Schema, we need to use the Xerces Reporter
	// (XMLErrorReporter)
	// (and not the Xerces XML ErrorHandler because we need the arguments array to
	// retrieve the attribut e name, element name, etc)

	// Xerces XSD validator can work with Xerces reporter for XSD error but not for
	// XML syntax (only XMLErrorHandler is allowed).
	// To fix this problem, we set the Xerces reporter with Java Reflection.
	if (canCustomizeReporter) {
		try {
			Field f = XMLSchemaLoader.class.getDeclaredField("fSchemaHandler");
			f.setAccessible(true);
			XSDHandler handler = (XSDHandler) f.get(schemaLoader);

			Field g = XSDHandler.class.getDeclaredField("fSchemaParser");
			g.setAccessible(true);
			SchemaDOMParser domParser = (SchemaDOMParser) g.get(handler);
			domParser.setProperty("http://apache.org/xml/properties/internal/error-reporter", reporter);
		} catch (Exception e) {
			canCustomizeReporter = false;
		}
	}
	return schemaLoader;
}
 
Example #3
Source File: ParallelTest.java    From exificient with MIT License 5 votes vote down vote up
public void testParallelXerces() throws XNIException, IOException,
		InterruptedException, ExecutionException {

	Collection<Callable<XSModel>> tasks = new ArrayList<Callable<XSModel>>();
	for (int i = 0; i < 25; i++) {
		Callable<XSModel> task = new Callable<XSModel>() {
			public XSModel call() throws Exception {
				XMLEntityResolver entityResolver = new TestXSDResolver();
				String sXSD = "./data/XSLT/schema-for-xslt20.xsd";
				// load XSD schema & get XSModel
				XMLSchemaLoader sl = new XMLSchemaLoader();
				sl.setEntityResolver(entityResolver);
				XMLInputSource xsdSource = new XMLInputSource(null, sXSD,
						null);

				SchemaGrammar g = (SchemaGrammar) sl.loadGrammar(xsdSource);
				XSModel xsModel = g.toXSModel();
				return xsModel;
			}
		};

		tasks.add(task);
	}

	ExecutorService executor = Executors.newFixedThreadPool(4);
	List<Future<XSModel>> results = executor.invokeAll(tasks);

	for (Future<XSModel> result : results) {
		System.out.println("XSModel: " + result.get());
	}
}
 
Example #4
Source File: SchemaBuilder.java    From xml-avro with Apache License 2.0 5 votes vote down vote up
public Schema createSchema(LSInput input) {
    ErrorHandler errorHandler = new ErrorHandler();

    XMLSchemaLoader loader = new XMLSchemaLoader();
    if (resolver != null)
        loader.setEntityResolver(new EntityResolver(resolver));

    loader.setErrorHandler(errorHandler);
    loader.setParameter(Constants.DOM_ERROR_HANDLER, errorHandler);

    XSModel model = loader.load(input);

    errorHandler.throwExceptionIfHasError();
    return createSchema(model);
}
 
Example #5
Source File: JavaxXmlValidator.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected List<XSModel> getXSModels(List<nl.nn.adapterframework.validation.Schema> schemas) throws IOException, XMLStreamException, ConfigurationException {
	List<XSModel> result = new ArrayList<XSModel>();
	XMLSchemaLoader xsLoader = new XMLSchemaLoader();
	for (nl.nn.adapterframework.validation.Schema schema : schemas) {
		XSModel xsModel = xsLoader.loadURI(schema.getSystemId());
		result.add(xsModel);
	}
	return result;
}
 
Example #6
Source File: XmlAligner.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected static List<XSModel> getSchemaInformation(URL schemaURL) {
	XMLSchemaLoader xsLoader = new XMLSchemaLoader();
	XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
	List<XSModel> schemaInformation= new LinkedList<XSModel>();
	schemaInformation.add(xsModel);
	return schemaInformation;
}
 
Example #7
Source File: XSDValidator.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public static void doDiagnostics(DOMDocument document, XMLEntityResolver entityResolver,
		List<Diagnostic> diagnostics, CancelChecker monitor) {

	try {
		XMLErrorReporter reporter = new LSPErrorReporterForXSD(document, diagnostics);

		XMLGrammarPreparser grammarPreparser = new LSPXMLGrammarPreparser();
		XMLSchemaLoader schemaLoader = createSchemaLoader(reporter);

		grammarPreparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, schemaLoader);

		grammarPreparser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY,
				new XMLGrammarPoolImpl());
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE,
				false);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE, true);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, true);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE, true);

		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE,
				true);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE,
				true);
		grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE,
				true);

		// Add LSP content handler to stop XML parsing if monitor is canceled.
		// grammarPreparser.setContentHandler(new LSPContentHandler(monitor));

		// Add LSP error reporter to fill LSP diagnostics from Xerces errors
		grammarPreparser.setProperty("http://apache.org/xml/properties/internal/error-reporter", reporter);

		if (entityResolver != null) {
			grammarPreparser.setEntityResolver(entityResolver);
		}

		String content = document.getText();
		String uri = document.getDocumentURI();
		InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
		XMLInputSource is = new XMLInputSource(null, uri, uri, inputStream, null);
		grammarPreparser.getLoader(XMLGrammarDescription.XML_SCHEMA);
		grammarPreparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, is);
	} catch (IOException | CancellationException | XMLParseException exception) {
		// ignore error
	} catch (Exception e) {
		LOGGER.log(Level.SEVERE, "Unexpected XSDValidator error", e);
	}
}
 
Example #8
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns the Xerces DOM xs:element which have created the given instance
 * <code>elementDeclaration</code> and null otherwise.
 * 
 * @param elementDeclaration
 * @param xsLoader
 * @return the Xerces DOM xs:element which have created the given instance
 *         <code>elementDeclaration</code> and null otherwise
 */
private static ElementImpl findLocalMappedXercesElement(XSElementDeclaration elementDeclaration,
		XSLoaderImpl xsLoader) {
	try {
		// When XML Schema is loaded by XSLoaderImpl, it uses XMLSchemaLoader which uses
		// XSDHandler.
		// Xerces stores the location in the XSDHandler instance in 2 arrays:
		// - fParticle array of XSParticleDecl where fValue is an instance of
		// XSElementDeclaration
		// - fLocalElementDecl array of Xerces Element which stores the element offset.

		// Get the XMLSchemaLoader instance from the XSLoader instance
		Field f = XSLoaderImpl.class.getDeclaredField("fSchemaLoader");
		f.setAccessible(true);
		XMLSchemaLoader schemaLoader = (XMLSchemaLoader) f.get(xsLoader);

		// Get the XSDHandler instance from the XMLSchemaLoader instance
		Field f2 = XMLSchemaLoader.class.getDeclaredField("fSchemaHandler");
		f2.setAccessible(true);
		XSDHandler handler = (XSDHandler) f2.get(schemaLoader);

		// Get the XSParticleDecl array from the XSDHandler instance
		Field f3 = XSDHandler.class.getDeclaredField("fParticle");
		f3.setAccessible(true);
		XSParticleDecl[] fParticle = (XSParticleDecl[]) f3.get(handler);

		// Get the index where elementDeclaration is associated with a XSParticleDecl
		int i = getXSElementDeclIndex(elementDeclaration, fParticle);
		if (i >= 0) {
			// Get the Xerces Element array from the XSDHandler instance
			Field f4 = XSDHandler.class.getDeclaredField("fLocalElementDecl");
			f4.setAccessible(true);
			Element[] fLocalElementDecl = (Element[]) f4.get(handler);
			return (ElementImpl) fLocalElementDecl[i];
		}
	} catch (Exception e) {
		LOGGER.log(Level.SEVERE,
				"Error while retrieving mapped Xerces xs:element of '" + elementDeclaration.getName() + "'.", e);
	}
	return null;

}
 
Example #9
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args)
{
   if (!args[0].equals("--dump"))
   {
      // Activates the resolver for Drb
      try
      {
         DrbFactoryResolver.setMetadataResolver (
               new DrbCortexMetadataResolver (
            DrbCortexModel.getDefaultModel ()));
      }
      catch (IOException e)
      {
         System.err.println ("Resolver cannot be handled.");
      }
      process(args[0]);
   }
   else
   {
   System.out.println("declare function local:deduplicate($list) {");
   System.out.println("  if (fn:empty($list)) then ()");
   System.out.println("  else");
   System.out.println("    let $head := $list[1],");
   System.out.println("      $tail := $list[position() > 1]");
   System.out.println("    return");
   System.out
         .println("      if (fn:exists($tail[ . = $head ])) then " +
               "local:deduplicate($tail)");
   System.out.println("      else ($head, local:deduplicate($tail))");
   System.out.println("};");
   System.out.println();
   System.out.println("declare function local:values($list) {");
   System.out
         .println("  fn:string-join(local:deduplicate(" +
               "fn:data($list)), ' ')");
   System.out.println("};");
   System.out.println();

   System.out.println("let $doc := !!!! GIVE PATH HERE !!!!");
   System.out.println("return\n(\n");

   /**
    * Open XML Schema
    */
   final XSLoader loader = new XMLSchemaLoader();

   final XSModel model = loader.loadURI(args[1]);

   XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);

   if (map != null)
   {
      for (int j = 0; j < map.getLength(); j++)
      {
         dumpElement("", (XSElementDeclaration) map.item(j));
      }
   }

   System.out.println("\n) ^--- REMOVE LAST COMMA !!!");
   }
}
 
Example #10
Source File: TestDomTreeAligner.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
	public void testFiles(String schemaFile, String namespace, String rootElement, String inputFile, boolean potentialCompactionProblems, String expectedFailureReason) throws Exception {
		URL schemaUrl=getSchemaURL(schemaFile);
    	String xsdUri=schemaUrl.toExternalForm();
    	Schema schema = Utils.getSchemaFromResource(schemaUrl);

    	XMLSchemaLoader xsLoader = new XMLSchemaLoader();
		XSModel xsModel = xsLoader.loadURI(xsdUri);
		List<XSModel> schemaInformation= new LinkedList<XSModel>();
		schemaInformation.add(xsModel);
   	
    	String xmlString=getTestFile(inputFile+".xml");
    	
    	boolean expectValid=expectedFailureReason==null;
    	
    	assertEquals("valid XML", expectValid, Utils.validate(schemaUrl, xmlString));
 	

    	ValidatorHandler validator = schema.newValidatorHandler();
    	DomTreeAligner aligner = new DomTreeAligner(validator, schemaInformation);
 //   	Xml2Json xml2json = new Xml2Json(aligner, false);
    	
    	validator.setContentHandler(aligner);
//    	aligner.setContentHandler(xml2json);
 
    	Document domIn = Utils.string2Dom(xmlString);
    	System.out.println("input DOM ["+Utils.dom2String1(domIn)+"]");
//    	System.out.println("xmlString "+xmlString);
//    	System.out.println("dom in "+ToStringBuilder.reflectionToString(domIn.getDocumentElement()));
    	Source source=aligner.asSource(domIn);
    	System.out.println();
    	System.out.println("start aligning "+inputFile);
   	
		
    	String xmlAct = Utils.source2String(source);
    	System.out.println("xml aligned via source="+xmlAct);
    	assertNotNull("xmlAct is null",xmlAct);
    	assertTrue("XML is not aligned",  Utils.validate(schemaUrl, xmlAct));

//    	InputSource is = new InputSource(new StringReader(xmlString));
//    	try {
// //       	String jsonOut=xml2json.toString();
//        	System.out.println("jsonOut="+jsonOut);
//        	if (!expectValid) {
//    			fail("expected to fail");
//    		}
//    	} catch (Exception e) {
//    		if (expectValid) {
//    			e.printStackTrace();
//    			fail(e.getMessage());
//    		}
//    	}
//    	String xmlString=getTestXml(xml);
//    	String xsdUri=Utils.class.getResource(xsd).toExternalForm();
//    	
//    	assertEquals("valid XML", expectValid, Utils.validate(namespace, xsdUri, xmlString));
//    	
//    	Document dom = Utils.string2Dom(xmlString);
//    	Utils.clean(dom);
//    	
//    	XMLReader parser = new SAXParser();
//    	Schema schema=Utils.getSchemaFromResource(namespace, xsdUri);
//    	ValidatorHandler validator = schema.newValidatorHandler();
//    	XmlAligner instance = implementation.newInstance();
//    	instance.setPsviProvider((PSVIProvider)validator);
//    	
//    	parser.setContentHandler(validator);
//    	validator.setContentHandler(instance);
//    	
//    	System.out.println();
//    	System.out.println("start aligning "+xml);

//    	instance.parse(dom.getDocumentElement());
//    	System.out.println("alignedXml="+Utils.dom2String1(dom));
//    	assertTrue("valid aligned XML", Utils.validate(namespace, xsdUri, dom)); // only if dom  itself is modified...

//    	testXmlAligner(instance, dom.getDocumentElement(), namespace, xsdUri);

//    	JSONObject json=Utils.xml2Json(xmlString);
//    	System.out.println("JSON:"+json);
//    	String jsonString = json.toString();
//    	
//    	jsonString=jsonString.replaceAll(",\"xmlns\":\"[^\"]*\"", "");
//    	System.out.println("JSON with fixed xmlns:"+jsonString);
//    	
//    	String xmlFromJson = Utils.json2Xml(Utils.string2Json(jsonString));
//    	if (StringUtils.isNotEmpty(namespace)) {
//    		xmlFromJson=xmlFromJson.replaceFirst(">", " xmlns=\""+namespace+"\">");
//    	}
//    	System.out.println("start aligning xml from json "+xmlFromJson);
//    	Document domFromJson = Utils.string2Dom(xmlFromJson);
//    	instance.startParse(domFromJson.getDocumentElement());
    	
    }