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

The following examples show how to use org.apache.xerces.xni.parser.XMLInputSource. 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: CMDTDContentModelProvider.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public CMDocument createCMDocument(String key) {
	try {
		CMDTDDocument document = new CMDTDDocument(key);
		document.setEntityResolver(resolverExtensionManager);
		document.setErrorHandler(SILENT_ERROR_HANDLER);
		Grammar grammar = document.loadGrammar(new XMLInputSource(null, key, null));
		if (grammar != null) {
			// DTD can be loaded
			return document;
		}
	} catch (Exception e) {
		return null;
	}
	return null;
}
 
Example #2
Source File: HTMLParser.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Parses and then inserts the specified HTML content into the HTML content currently being parsed.
 * @param html the HTML content to push
 */
public void pushInputString(final String html) {
    page_.registerParsingStart();
    page_.registerInlineSnippetParsingStart();
    try {
        final WebResponse webResponse = page_.getWebResponse();
        final Charset charset = webResponse.getContentCharset();
        final String url = webResponse.getWebRequest().getUrl().toString();
        final XMLInputSource in = new XMLInputSource(null, url, null, new StringReader(html), charset.name());
        ((HTMLConfiguration) fConfiguration).evaluateInputSource(in);
    }
    finally {
        page_.registerParsingEnd();
        page_.registerInlineSnippetParsingEnd();
    }
}
 
Example #3
Source File: XSDResolver.java    From RISE-V2G with MIT License 6 votes vote down vote up
private XSDResolver() {
	EXISchemaFactory exiSchemaFactory = new EXISchemaFactory();
	EXISchemaFactoryExceptionHandler esfe = new EXISchemaFactoryExceptionHandler();
	exiSchemaFactory.setCompilerErrorHandler(esfe);
	
	InputStream isV2GCIMsgDef = getClass().getResourceAsStream(GlobalValues.SCHEMA_PATH_MSG_DEF.toString());
	XMLInputSource xmlISV2GCIMsgDef = new XMLInputSource(null, null, null, isV2GCIMsgDef, null);
	
	InputStream isV2GCIMsgHeader = getClass().getResourceAsStream(GlobalValues.SCHEMA_PATH_MSG_HEADER.toString());
	XMLInputSource xmlISV2GCIMsgHeader = new XMLInputSource(null, null, null, isV2GCIMsgHeader, null);
	
	InputStream isV2GCIMsgBody = getClass().getResourceAsStream(GlobalValues.SCHEMA_PATH_MSG_BODY.toString());
	XMLInputSource xmlISV2GCIMsgBody = new XMLInputSource(null, null, null, isV2GCIMsgBody, null);
	
	InputStream isV2GCIMsgDataTypes = getClass().getResourceAsStream(GlobalValues.SCHEMA_PATH_MSG_DATA_TYPES.toString());
	XMLInputSource xmlISV2GCIMsgDataTypes = new XMLInputSource(null, null, null, isV2GCIMsgDataTypes, null);
	
	InputStream isXMLDSig = getClass().getResourceAsStream(GlobalValues.SCHEMA_PATH_XMLDSIG.toString());
	XMLInputSource xmlISXMLDSig = new XMLInputSource(null, null, null, isXMLDSig, null);
	
	setEntity("V2G_CI_MsgDef.xsd", xmlISV2GCIMsgDef);
	setEntity("V2G_CI_MsgBody.xsd", xmlISV2GCIMsgBody);
	setEntity("V2G_CI_MsgHeader.xsd", xmlISV2GCIMsgHeader);
	setEntity("V2G_CI_MsgDataTypes.xsd", xmlISV2GCIMsgDataTypes);
	setEntity("xmldsig-core-schema.xsd", xmlISXMLDSig);
}
 
Example #4
Source File: LSPXMLGrammarPreparser.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Grammar preparseGrammar(String type, XMLInputSource is) throws XNIException, IOException {
	// if (fLoaders.containsKey(type)) {
	// XMLGrammarLoaderContainer xglc = (XMLGrammarLoaderContainer) getLoader(type);
	XMLGrammarLoader gl = getLoader(type);
	// if (xglc.modCount != fModCount) {
	// make sure gl's been set up with all the "basic" properties:
	gl.setProperty(SYMBOL_TABLE, fSymbolTable);
	gl.setProperty(ENTITY_RESOLVER, fEntityResolver);
	gl.setProperty(ERROR_REPORTER, errorReporter);
	// potentially, not all will support this one...
	if (fGrammarPool != null) {
		try {
			gl.setProperty(GRAMMAR_POOL, fGrammarPool);
		} catch (Exception e) {
			// too bad...
		}
	}
	// xglc.modCount = fModCount;
	// }
	return gl.loadGrammar(is);
	// }
	// return null;
}
 
Example #5
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 #6
Source File: ClassLoaderXmlEntityResolverTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void bytesClassPath() throws SAXException, IOException, ConfigurationException {
	ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();

	URL file = this.getClass().getResource(JAR_FILE);
	assertNotNull("jar url not found", file);
	JarFile jarFile = new JarFile(file.getFile());
	assertNotNull("jar file not found",jarFile);

	JarFileClassLoader cl = new JarFileClassLoader(localClassLoader);
	cl.setJar(file.getFile());
	cl.configure(null, "");

	ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(cl);

	XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("ClassLoader/Xslt/names.xsl");

	XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier);
	assertNotNull(inputSource);
}
 
Example #7
Source File: ClassLoaderXmlEntityResolverTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
public void bytesClassPathAbsolute() throws SAXException, IOException, ConfigurationException  {
	ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();

	URL file = this.getClass().getResource(JAR_FILE);
	assertNotNull("jar url not found", file);
	JarFile jarFile = new JarFile(file.getFile());
	assertNotNull("jar file not found",jarFile);

	JarFileClassLoader cl = new JarFileClassLoader(localClassLoader);
	cl.setJar(file.getFile());
	cl.configure(null, "");

	ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(cl);

	XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/ClassLoader/Xslt/names.xsl");

	XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier);
	assertNotNull(inputSource);
}
 
Example #8
Source File: HtmlUnitNekoDOMBuilder.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Parses and then inserts the specified HTML content into the HTML content currently being parsed.
 * @param html the HTML content to push
 */
@Override
public void pushInputString(final String html) {
    page_.registerParsingStart();
    page_.registerInlineSnippetParsingStart();
    try {
        final WebResponse webResponse = page_.getWebResponse();
        final Charset charset = webResponse.getContentCharset();
        final String url = webResponse.getWebRequest().getUrl().toString();
        final XMLInputSource in = new XMLInputSource(null, url, null, new StringReader(html), charset.name());
        ((HTMLConfiguration) fConfiguration).evaluateInputSource(in);
    }
    finally {
        page_.registerParsingEnd();
        page_.registerInlineSnippetParsingEnd();
    }
}
 
Example #9
Source File: ClassLoaderXmlEntityResolverTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void localClassPathFileOnRootOfClasspathAbsolute() throws SAXException, IOException {
	ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();
	ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader);

	XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/AppConstants.properties");

	XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier);
	assertNotNull(inputSource);
}
 
Example #10
Source File: ClassLoaderXmlEntityResolverTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void localClassPathAbsolute() throws SAXException, IOException {
	ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();
	ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader);

	XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("/Xslt/importDocument/lookup.xml");
	
	XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier);
	assertNotNull(inputSource);
}
 
Example #11
Source File: ClassLoaderXmlEntityResolverTest.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Test
public void localClassPathFileOnRootOfClasspath() throws SAXException, IOException {
	ClassLoader localClassLoader = Thread.currentThread().getContextClassLoader();
	ClassLoaderXmlEntityResolver resolver = new ClassLoaderXmlEntityResolver(localClassLoader);
	
	XMLResourceIdentifier resourceIdentifier = getXMLResourceIdentifier("AppConstants.properties");
	
	XMLInputSource inputSource = resolver.resolveEntity(resourceIdentifier);
	assertNotNull(inputSource);
}
 
Example #12
Source File: ClassLoaderXmlEntityResolver.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	if (log.isDebugEnabled()) log.debug("resolveEntity publicId ["+resourceIdentifier.getPublicId()+"] baseSystemId ["+resourceIdentifier.getBaseSystemId()+"] expandedSystemId ["+resourceIdentifier.getExpandedSystemId()+"] literalSystemId ["+resourceIdentifier.getLiteralSystemId()+"] namespace ["+resourceIdentifier.getNamespace()+"]");
	if (resourceIdentifier.getBaseSystemId() == null
			&& resourceIdentifier.getExpandedSystemId() == null
			&& resourceIdentifier.getLiteralSystemId() == null
			&& resourceIdentifier.getNamespace() == null
			&& resourceIdentifier.getPublicId() == null) {
		// This seems to happen sometimes. For example with import of
		// sub01a.xsd and sub05.xsd without namespace in
		// /XmlValidator/import_include/root.xsd of Ibis4TestIAF. The
		// default resolve entity implementation seems to ignore it, hence
		// return null.
		return null;
	}
	
	String base = resourceIdentifier.getBaseSystemId();
	String href = resourceIdentifier.getLiteralSystemId();
	if (href == null) {
		// Ignore import with namespace but without schemaLocation
		return null;
	}

	Resource resource;
	try {
		resource = resolveToResource(href, base);
	} catch (TransformerException e) {
		throw new XNIException(e);
	}

	
	InputStream inputStream = resource.getURL().openStream();
	return new XMLInputSource(null, resource.getSystemId(), null, inputStream, null);
}
 
Example #13
Source File: HtmlUnitNekoDOMBuilder.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void parse(final XMLInputSource inputSource) throws XNIException, IOException {
    final HTMLParserDOMBuilder oldBuilder = page_.getDOMBuilder();
    page_.setDOMBuilder(this);
    try {
        super.parse(inputSource);
    }
    finally {
        page_.setDOMBuilder(oldBuilder);
    }
}
 
Example #14
Source File: XercesXmlValidator.java    From iaf with Apache License 2.0 5 votes vote down vote up
private static XMLInputSource stringToXMLInputSource(Schema schema) throws IOException, ConfigurationException {
	// SystemId is needed in case the schema has an import. Maybe we should
	// already resolve this at the SchemaProvider side (except when
	// noNamespaceSchemaLocation is being used this is already done in
	// (Wsdl)XmlValidator (using
	// mergeXsdsGroupedByNamespaceToSchemasWithoutIncludes)).
	// See comment in method XmlValidator.getSchemas() too.
	// See ClassLoaderXmlEntityResolver too.
	return new XMLInputSource(null, schema.getSystemId(), null, schema.getInputStream(), null);
}
 
Example #15
Source File: SchemaBuilder.java    From xml-avro with Apache License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier id) throws XNIException, IOException {
    String systemId = id.getLiteralSystemId();
    debug("Resolving " + systemId);

    XMLInputSource source = new XMLInputSource(id);
    source.setByteStream(resolver.getStream(systemId));
    return source;
}
 
Example #16
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 #17
Source File: XSDResolver.java    From RISE-V2G with MIT License 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 
		throws XNIException, IOException {
	String literalSystemId = resourceIdentifier.getLiteralSystemId(); 
	
	if (xmlInputSourceEntities != null && xmlInputSourceEntities.containsKey(literalSystemId)) {
		return xmlInputSourceEntities.get(literalSystemId);
	}

	return null;
}
 
Example #18
Source File: XSDResolver.java    From RISE-V2G with MIT License 5 votes vote down vote up
public void setEntity(String literalSystemId, XMLInputSource xmlInput) {
	if (xmlInputSourceEntities == null) {
		xmlInputSourceEntities = new HashMap<String, XMLInputSource>();
	}
	
	xmlInputSourceEntities.put(literalSystemId, xmlInput);
}
 
Example #19
Source File: HTMLParser.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void parse(final XMLInputSource inputSource) throws XNIException, IOException {
    final HtmlUnitDOMBuilder oldBuilder = page_.getBuilder();
    page_.setBuilder(this);
    try {
        super.parse(inputSource);
    }
    finally {
        page_.setBuilder(oldBuilder);
    }
}
 
Example #20
Source File: XSLURIResolverExtension.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	String publicId = resourceIdentifier.getNamespace();
	if (XSL_NAMESPACE_URI.equals(publicId)) {
		String baseLocation = resourceIdentifier.getBaseSystemId();
		String xslFilePath = resolve(baseLocation, publicId, null);
		if (xslFilePath != null) {
			return new XMLInputSource(publicId, xslFilePath, xslFilePath);
		}
	}
	return null;
}
 
Example #21
Source File: XMLCatalogResolverExtension.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	if (catalogResolver != null) {
		return catalogResolver.resolveEntity(resourceIdentifier);
	}
	return null;
}
 
Example #22
Source File: XMLCacheResolverExtension.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	String url = resourceIdentifier.getExpandedSystemId();
	// Try to get the downloaded resource. In the case where the resource is
	// downloading but takes too long, a CacheResourceDownloadingException is
	// thrown.
	Path file = getCachedResource(url);
	if (file != null) {
		// The resource was downloaded locally, use it.
		return new XMLFileInputSource(resourceIdentifier, file);
	}
	return null;
}
 
Example #23
Source File: XSDURIResolverExtension.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	String publicId = resourceIdentifier.getNamespace();
	if (SCHEMA_FOR_SCHEMA_URI_2001.equals(publicId) || SCHEMA_FOR_NAMESPACE_URI_1998.equals(publicId)) {
		String baseLocation = resourceIdentifier.getBaseSystemId();
		String xslFilePath = resolve(baseLocation, publicId, null);
		if (xslFilePath != null) {
			return new XMLInputSource(publicId, xslFilePath, xslFilePath);
		}
	}
	return null;
}
 
Example #24
Source File: CMDTDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public void loadInternalDTD(String internalSubset, String baseSystemId, String systemId)
		throws XNIException, IOException {
	// Load empty DTD grammar
	XMLInputSource source = new XMLInputSource("", "", "", new StringReader(""), "");
	grammar = (DTDGrammar) loadGrammar(source);
	// To get the DTD scanner to end at the right place we have to fool
	// it into thinking that it reached the end of the internal subset
	// in a real document.
	fDTDScanner.reset();
	StringBuilder buffer = new StringBuilder(internalSubset.length() + 2);
	buffer.append(internalSubset).append("]>");
	XMLInputSource is = new XMLInputSource(null, baseSystemId, null, new StringReader(buffer.toString()), null);
	fEntityManager.startDocumentEntity(is);
	fDTDScanner.scanDTDInternalSubset(true, false, systemId != null);
}
 
Example #25
Source File: XMLCatalogURIResolverExtension.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	if (hasDTDorXMLSchema(resourceIdentifier.getBaseSystemId())) {
		return null;
	}
	String publicId = resourceIdentifier.getNamespace();
	if (CATALOG_NAMESPACE_URI.equals(publicId)) {
		return new XMLInputSource(publicId, CATALOG_SYSTEM, CATALOG_SYSTEM);
	}
	return null;
}
 
Example #26
Source File: URIResolverExtensionManager.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
	XMLInputSource is = null;
	for (URIResolverExtension resolver : resolvers) {
		is = resolver.resolveEntity(resourceIdentifier);
		if (is != null) {
			return is;
		}
	}
	return defaultURIResolverExtension.resolveEntity(resourceIdentifier);
}
 
Example #27
Source File: HTMLParser.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the HTML content from the given string into an object tree representation.
 *
 * @param parent where the new parsed nodes will be added to
 * @param context the context to build the fragment context stack
 * @param source the (X)HTML to be parsed
 * @throws SAXException if a SAX error occurs
 * @throws IOException if an IO error occurs
 */
public static void parseFragment(final DomNode parent, final DomNode context, final String source)
    throws SAXException, IOException {
    final Page page = parent.getPage();
    if (!(page instanceof HtmlPage)) {
        return;
    }
    final HtmlPage htmlPage = (HtmlPage) page;
    final URL url = htmlPage.getUrl();

    final HtmlUnitDOMBuilder domBuilder = new HtmlUnitDOMBuilder(parent, url, source);
    domBuilder.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
    // build fragment context stack
    DomNode node = context;
    final List<QName> ancestors = new ArrayList<>();
    while (node != null && node.getNodeType() != Node.DOCUMENT_NODE) {
        ancestors.add(0, new QName(null, node.getNodeName(), null, null));
        node = node.getParentNode();
    }
    if (ancestors.isEmpty() || !"html".equals(ancestors.get(0).localpart)) {
        ancestors.add(0, new QName(null, "html", null, null));
    }
    if (ancestors.size() == 1 || !"body".equals(ancestors.get(1).localpart)) {
        ancestors.add(1, new QName(null, "body", null, null));
    }

    domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, true);
    domBuilder.setProperty(HTMLTagBalancer.FRAGMENT_CONTEXT_STACK, ancestors.toArray(new QName[] {}));

    final XMLInputSource in = new XMLInputSource(null, url.toString(), null, new StringReader(source), null);

    htmlPage.registerParsingStart();
    htmlPage.registerSnippetParsingStart();
    try {
        domBuilder.parse(in);
    }
    finally {
        htmlPage.registerParsingEnd();
        htmlPage.registerSnippetParsingEnd();
    }
}
 
Example #28
Source File: ScriptFilter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private XMLInputSource newInputSource( String replacementText ) {
    StringBuffer systemID = new StringBuffer( _systemID );
    systemID.append( "script" ).append( ++_scriptIndex );

    return new XMLInputSource( null, systemID.toString(), null, new StringReader( replacementText ), "UTF-8" );
}
 
Example #29
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 #30
Source File: CMDTDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Grammar loadGrammar(XMLInputSource source) throws IOException, XNIException {
	grammar = (DTDGrammar) super.loadGrammar(source);
	this.tracker = DTDUtils.createFilesChangedTracker(grammar);
	return grammar;
}