Java Code Examples for org.apache.xerces.xni.XMLLocator#getCharacterOffset()

The following examples show how to use org.apache.xerces.xni.XMLLocator#getCharacterOffset() . 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: AbstractLSPErrorReporter.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the LSP position from the SAX location.
 * 
 * @param offset   the adjusted offset.
 * @param location the original SAX location.
 * @param document the text document.
 * @return the LSP position from the SAX location.
 */
private static Position toLSPPosition(int offset, XMLLocator location, TextDocument document) {
	if (location != null && offset == location.getCharacterOffset() - 1) {
		return new Position(location.getLineNumber() - 1, location.getColumnNumber() - 1);
	}
	try {
		return document.positionAt(offset);
	} catch (BadLocationException e) {
		return location != null ? new Position(location.getLineNumber() - 1, location.getColumnNumber() - 1) : null;
	}
}
 
Example 2
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;
}