org.apache.xerces.xni.parser.XMLEntityResolver Java Examples

The following examples show how to use org.apache.xerces.xni.parser.XMLEntityResolver. 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: DTDValidator.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
public static void doDiagnostics(DOMDocument document, XMLEntityResolver entityResolver,
		List<Diagnostic> diagnostics, CancelChecker monitor) {
	try {
		XMLDTDLoader loader = new XMLDTDLoader();
		loader.setProperty("http://apache.org/xml/properties/internal/error-reporter",
				new LSPErrorReporterForXML(document, diagnostics));

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

		String content = document.getText();
		String uri = document.getDocumentURI();
		InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
		XMLInputSource source = new XMLInputSource(null, uri, uri, inputStream, null);
		loader.loadGrammar(source);
	} catch (Exception e) {

	}
}
 
Example #2
Source File: ParallelTest.java    From exificient with MIT License 6 votes vote down vote up
public static EXIFactory getExiFactory() throws EXIException {
	GrammarFactory gf = GrammarFactory.newInstance();
	// no internet connection, try offline
	XMLEntityResolver entityResolver = new TestXSDResolver();
	Grammars grammar = gf.createGrammars(
			"./data/XSLT/schema-for-xslt20.xsd", entityResolver);
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setGrammars(grammar);
	FidelityOptions fidelityOptions = FidelityOptions.createDefault();
	fidelityOptions.setFidelity(FidelityOptions.FEATURE_STRICT, true);
	fidelityOptions.setFidelity(FidelityOptions.FEATURE_PREFIX, true);
	// activate this option will make the tests pass
	// fidelityOptions.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE,
	// true);
	exiFactory.setFidelityOptions(fidelityOptions);
	exiFactory.setCodingMode(CodingMode.COMPRESSION);
	EncodingOptions encodingOptions = EncodingOptions.createDefault();
	encodingOptions.setOption(EncodingOptions.INCLUDE_OPTIONS);
	encodingOptions.setOption(EncodingOptions.INCLUDE_SCHEMA_ID);
	exiFactory.setEncodingOptions(encodingOptions);
	return exiFactory;
}
 
Example #3
Source File: DTDDiagnosticsParticipant.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics, CancelChecker monitor) {
	if (!xmlDocument.isDTD()) {
		// Don't use the DTD validator, if it's a DTD
		return;
	}
	// Get entity resolver (XML catalog resolver, XML schema from the file
	// associations settings., ...)
	XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager();
	// Process validation
	DTDValidator.doDiagnostics(xmlDocument, entityResolver, diagnostics, monitor);
}
 
Example #4
Source File: XSDDiagnosticsParticipant.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics, CancelChecker monitor) {
	if (!DOMUtils.isXSD(xmlDocument)) {
		// Don't use the XSD validator, if the XML document is not a XML Schema.
		return;
	}
	// Get entity resolver (XML catalog resolver, XML schema from the file
	// associations settings., ...)
	XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager();
	// Process validation
	XSDValidator.doDiagnostics(xmlDocument, entityResolver, diagnostics, monitor);
}
 
Example #5
Source File: ContentModelDiagnosticsParticipant.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics, CancelChecker monitor) {
	if (xmlDocument.isDTD() || DOMUtils.isXSD(xmlDocument)) {
		// Don't validate DTD / XML Schema with XML validator
		return;
	}
	// Get entity resolver (XML catalog resolver, XML schema from the file
	// associations settings., ...)
	XMLEntityResolver entityResolver = xmlDocument.getResolverExtensionManager();
	// Process validation
	XMLValidator.doDiagnostics(xmlDocument, entityResolver, diagnostics,
			contentModelPlugin.getContentModelSettings(),
			contentModelPlugin.getContentModelManager().getGrammarPool(), monitor);
}
 
Example #6
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 #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: AbstractTestCase.java    From exificient with MIT License 4 votes vote down vote up
protected XMLEntityResolver getXsdEntityResolver() {
	return new TestXSDResolver();
}