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

The following examples show how to use org.apache.xerces.impl.xs.SchemaGrammar. 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: XSDUtils.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
private static void updateTracker(SchemaGrammar grammar, Set<SchemaGrammar> trackedGrammars,
		Set<String> trackedURIs, FilesChangedTracker tracker) {
	if (grammar == null || trackedGrammars.contains(grammar)) {
		return;
	}
	trackedGrammars.add(grammar);
	// Loop for all XML Schema (root + included) to track it
	StringList locations = grammar.getDocumentLocations();
	for (int i = 0; i < locations.getLength(); i++) {
		String location = locations.item(i);
		if (!trackedURIs.contains(location)) {
			trackedURIs.add(location);
		}
		if (location != null && URIUtils.isFileResource(location)) {
			// The schema is a file, track when file changed
			tracker.addFileURI(location);
		}
	}
	// Track the imported grammars
	Vector<?> importedGrammars = grammar.getImportedGrammars();
	if (importedGrammars != null) {
		for (Object importedGrammar : importedGrammars) {
			updateTracker((SchemaGrammar) importedGrammar, trackedGrammars, trackedURIs, tracker);
		}
	}
}
 
Example #2
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the location of the local xs:element declared in the given XML Schema
 * <code>targetSchema</code> which matches the given XML element
 * <code>originElement</code> and null otherwise.
 * 
 * @param originElement the XML element
 * @param targetSchema  the XML Schema
 * @param enclosingType the enclosing type of the XS element declaration which
 *                      matches the XML element
 * @param grammar       the Xerces grammar
 * @return the location of the global xs:element declared in the given XML
 *         Schema <code>targetSchema</code> which matches the given XML element
 *         <code>originElement</code> and null otherwise.
 */
private static LocationLink findLocalXSElement(DOMElement originElement, DOMDocument targetSchema,
		XSComplexTypeDefinition enclosingType, SchemaGrammar schemaGrammar) {
	// In local xs:element case, xs:element is declared inside a complex type
	// (enclosing type).
	// Xerces stores in the SchemaGrammar the locator (offset) for each complex type
	// (XSComplexTypeDecl)
	// Here we get the offset of the local enclosing complex type xs:complexType.
	// After that
	// we just loop of children of local xs:complexType and return the
	// location of
	// xs:element/@name which matches the tag name of the origin XML element

	// Get the location of the local xs:complexType
	int complexTypeOffset = getComplexTypeOffset(enclosingType, schemaGrammar);
	if (complexTypeOffset != -1) {
		// location of xs:complexType is found, find the xs:element declared inside the
		// xs:complexType
		DOMNode node = targetSchema.findNodeAt(complexTypeOffset);
		if (node != null && node.isElement() && node.hasChildNodes()) {
			return findXSElement((DOMElement) originElement, node.getChildNodes(), true);
		}
	}
	return null;
}
 
Example #3
Source File: XSDUtils.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public static FilesChangedTracker createFilesChangedTracker(Set<SchemaGrammar> grammars) {
	FilesChangedTracker tracker = new FilesChangedTracker();
	Set<SchemaGrammar> trackedGrammars = new HashSet<>();
	Set<String> trackedURIs = new HashSet<>();
	for (SchemaGrammar grammar : grammars) {
		updateTracker(grammar, trackedGrammars, trackedURIs, tracker);
	}
	return tracker;
}
 
Example #4
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Create files tracker to track all XML Schema (root and imported) from the XS
 * model.
 * 
 * @return a files tracker to track all XML Schema (root and imported) from the
 *         XS model.
 */
private static FilesChangedTracker createFilesChangedTracker(XSModel model) {
	Set<SchemaGrammar> grammars = new HashSet<>();
	XSNamespaceItemList namespaces = model.getNamespaceItems();
	for (int i = 0; i < namespaces.getLength(); i++) {
		SchemaGrammar grammar = getSchemaGrammar(namespaces.item(i));
		if (grammar != null) {
			grammars.add(grammar);
		}
	}
	return XSDUtils.createFilesChangedTracker(grammars);
}
 
Example #5
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the schema URI of the given schema grammar.
 * 
 * @param schemaGrammar the Xerces schema grammar.
 * @return the schema URI of the given schema grammar.
 */
static String getSchemaURI(SchemaGrammar schemaGrammar) {
	if (schemaGrammar == null) {
		return null;
	}
	return schemaGrammar.getDocumentLocations().item(0);
}
 
Example #6
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the offset where the local xs:complexType is declared and -1
 * otherwise.
 * 
 * @param complexType the local complex type
 * @param grammar     the grammar where local complex type is declared.
 * @return the offset where the local xs:complexType is declared and -1
 *         otherwise.
 */
private static int getComplexTypeOffset(XSComplexTypeDefinition complexType, SchemaGrammar grammar) {
	try {
		// Xerces stores in SchemaGrammar instance, the location in 2 arrays:
		// - fCTLocators array of locator
		// - fComplexTypeDecls array of XSComplexTypeDecl

		// As it's not an API, we must use Java Reflection to get those 2 arrays
		Field f = SchemaGrammar.class.getDeclaredField("fCTLocators");
		f.setAccessible(true);
		SimpleLocator[] fCTLocators = (SimpleLocator[]) f.get(grammar);

		// Find the location offset of the given complexType
		XSComplexTypeDecl[] fComplexTypeDecls = getXSComplexTypeDecls(grammar);
		for (int i = 0; i < fComplexTypeDecls.length; i++) {
			if (complexType.equals(fComplexTypeDecls[i])) {
				XMLLocator locator = fCTLocators[i];
				return locator != null ? locator.getCharacterOffset() : -1;
			}
		}
	} catch (Exception e) {
		LOGGER.log(Level.SEVERE,
				"Error while retrieving offset of local xs:complexType'" + complexType.getName() + "'.", e);
	}
	// Offset where xs:complexType is declared cannot be found
	return -1;
}
 
Example #7
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns all xs:complexTypes (global and local) from the given schema grammar
 * 
 * @param grammar the grammar
 * @return all xs:complexTypes (global and local) from the given schema grammar
 */
private static XSComplexTypeDecl[] getXSComplexTypeDecls(SchemaGrammar grammar) {
	try {
		Field fc = SchemaGrammar.class.getDeclaredField("fComplexTypeDecls");
		fc.setAccessible(true);
		return (XSComplexTypeDecl[]) fc.get(grammar);
	} catch (Exception e) {
		LOGGER.log(Level.SEVERE, "Error while retrieving list of xs:complexType of the grammar '"
				+ grammar.getSchemaNamespace() + "'.", e);
		return null;
	}
}
 
Example #8
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private static LocationLink findLocalXSAttribute(DOMAttr originAttribute, DOMDocument targetSchema,
		XSComplexTypeDefinition enclosingType, SchemaGrammar schemaGrammar) {
	int complexTypeOffset = getComplexTypeOffset(enclosingType, schemaGrammar);
	if (complexTypeOffset != -1) {
		// location of xs:complexType is found, find the xs:attribute declared inside
		// the
		// xs:complexType
		DOMNode node = targetSchema.findNodeAt(complexTypeOffset);
		if (node != null && node.isElement() && node.hasChildNodes()) {
			return findXSAttribute(originAttribute, node.getChildNodes(), true);
		}
	}
	return null;
}
 
Example #9
Source File: LSPXMLGrammarPool.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private static FilesChangedTracker create(Grammar grammar) {
	if (grammar instanceof SchemaGrammar) {
		return XSDUtils.createFilesChangedTracker((SchemaGrammar) grammar);
	}
	if (grammar instanceof DTDGrammar) {
		return DTDUtils.createFilesChangedTracker((DTDGrammar) grammar);
	}
	return null;
}
 
Example #10
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 #11
Source File: XercesXmlValidator.java    From iaf with Apache License 2.0 5 votes vote down vote up
private static void registerNamespaces(Grammar grammar, Set<String> namespaces) {
	namespaces.add(grammar.getGrammarDescription().getNamespace());
	if (grammar instanceof SchemaGrammar) {
		List<?> imported = ((SchemaGrammar)grammar).getImportedGrammars();
		if (imported != null) {
			for (Object g : imported) {
				Grammar gr = (Grammar)g;
				registerNamespaces(gr, namespaces);
			}
		}
	}
}
 
Example #12
Source File: XSDUtils.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public static FilesChangedTracker createFilesChangedTracker(SchemaGrammar grammar) {
	return createFilesChangedTracker(Collections.singleton(grammar));
}
 
Example #13
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public LocationLink findTypeLocation(DOMNode originNode) {
	DOMElement originElement = null;
	DOMAttr originAttribute = null;
	if (originNode.isElement()) {
		originElement = (DOMElement) originNode;
	} else if (originNode.isAttribute()) {
		originAttribute = (DOMAttr) originNode;
		originElement = originAttribute.getOwnerElement();
	}
	if (originElement == null || originElement.getLocalName() == null) {
		return null;
	}
	// Try to retrieve XSD element declaration from the given element.
	CMXSDElementDeclaration elementDeclaration = (CMXSDElementDeclaration) findCMElement(originElement,
			originElement.getNamespaceURI());
	if (elementDeclaration == null) {
		return null;
	}

	// Try to find the Xerces xs:element (which stores the offset) bound with the
	// XSElementDeclaration
	// case when xs:element is declared inside xs:choice, xs:all, xs:sequence, etc
	ElementImpl xercesElement = findLocalMappedXercesElement(elementDeclaration.getElementDeclaration(), xsLoader);
	// case when xs:element is declared as global or inside xs:complexType
	SchemaGrammar schemaGrammar = getOwnerSchemaGrammar(elementDeclaration.getElementDeclaration());
	if (schemaGrammar == null && xercesElement == null) {
		return null;
	}

	String documentURI = xercesElement != null ? xercesElement.getOwnerDocument().getDocumentURI()
			: getSchemaURI(schemaGrammar);
	if (URIUtils.isFileResource(documentURI)) {
		// Only XML Schema file is supported. In the case of XML file is bound with an
		// HTTP url and cache is enable, documentURI is a file uri from the cache
		// folder.

		// Xerces doesn't give the capability to know the location of xs:element,
		// xs:attribute.
		// To retrieve the proper location of xs:element, xs:attribute, we load the XML
		// Schema in the DOM Document which stores location.
		DOMDocument targetSchema = DOMUtils.loadDocument(documentURI,
				originNode.getOwnerDocument().getResolverExtensionManager());
		if (targetSchema == null) {
			return null;
		}
		if (originAttribute != null) {
			// find location of xs:attribute declaration
			String attributeName = originAttribute.getName();
			CMXSDAttributeDeclaration attributeDeclaration = (CMXSDAttributeDeclaration) elementDeclaration
					.findCMAttribute(attributeName);
			if (attributeDeclaration != null) {
				XSAttributeDeclaration attributeDecl = attributeDeclaration.getAttrDeclaration();
				if (attributeDecl.getScope() == XSConstants.SCOPE_LOCAL) {
					return findLocalXSAttribute(originAttribute, targetSchema,
							attributeDecl.getEnclosingCTDefinition(), schemaGrammar);
				}
			}
		} else {
			// find location of xs:element declaration
			boolean globalElement = elementDeclaration.getElementDeclaration()
					.getScope() == XSElementDecl.SCOPE_GLOBAL;
			if (globalElement) {
				// global xs:element
				return findGlobalXSElement(originElement, targetSchema);
			} else {
				// local xs:element
				// 1) use the Xerces xs:element strategy
				if (xercesElement != null) {
					return findLocalXSElement(originElement, targetSchema, xercesElement.getCharacterOffset());
				}
				// 2) use the Xerces xs:complexType strategy
				XSComplexTypeDefinition complexTypeDefinition = elementDeclaration.getElementDeclaration()
						.getEnclosingCTDefinition();
				if (complexTypeDefinition != null) {
					return findLocalXSElement(originElement, targetSchema, complexTypeDefinition, schemaGrammar);
				}
			}
		}
	}
	return null;
}
 
Example #14
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String getDocumentURI() {
	SchemaGrammar schemaGrammar = document.getOwnerSchemaGrammar(elementDeclaration);
	return CMXSDDocument.getSchemaURI(schemaGrammar);
}
 
Example #15
Source File: XSParser.java    From jlibs with Apache License 2.0 4 votes vote down vote up
public static XSModel getBuiltInSchema(){
    return new XSModelImpl(new SchemaGrammar[0]);
}
 
Example #16
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns the schema grammar from the given namespace and null otherwise.
 * 
 * @param namespaceItem the namespace
 * @return the schema grammar from the given namespace and null otherwise.
 */
private static SchemaGrammar getSchemaGrammar(XSNamespaceItem namespaceItem) {
	return (namespaceItem != null && namespaceItem instanceof SchemaGrammar) ? (SchemaGrammar) namespaceItem : null;
}