下面列出了org.w3c.dom.Document#setUserData ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml)
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException
{
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET);
final Map<String, Type> map = new HashMap<>();
final ParserOptions options = ParserOptions.builder()
.stopOnError(true)
.suppressOutput(true)
.warningsFatal(true)
.build();
document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null);
for (int i = 0, size = list.getLength(); i < size; i++)
{
final Type t = new SetType(list.item(i));
map.put(t.name(), t);
}
return map;
}
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml)
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException
{
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET);
final Map<String, Type> map = new HashMap<>();
final ParserOptions options = ParserOptions.builder()
.stopOnError(true)
.suppressOutput(true)
.warningsFatal(true)
.build();
document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null);
for (int i = 0, size = list.getLength(); i < size; i++)
{
final Type t = new EnumType(list.item(i));
map.put(t.name(), t);
}
return map;
}
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml)
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException
{
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET);
final Map<String, Type> map = new HashMap<>();
final ParserOptions options = ParserOptions.builder().stopOnError(true).suppressOutput(true).build();
document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null);
for (int i = 0, size = list.getLength(); i < size; i++)
{
final Type t = new CompositeType(list.item(i));
map.put(t.name(), t);
}
return map;
}
private static Map<String, Type> parseTestXmlWithMap(final String xPathExpr, final String xml)
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException
{
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET);
final Map<String, Type> map = new HashMap<>();
final ParserOptions options = ParserOptions.builder().stopOnError(true).suppressOutput(true).build();
document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, new ErrorHandler(options), null);
for (int i = 0, size = list.getLength(); i < size; i++)
{
final Type t = new EncodedDataType(list.item(i));
map.put(t.name(), t);
}
return map;
}
/**
* Flags a Mini-language XML document as corrected.
* @param element
*/
public static void flagDocumentAsCorrected(Element element) {
Document doc = element.getOwnerDocument();
if (doc != null) {
doc.setUserData("autoCorrected", "true", null);
}
}
public static WidgetDocumentInfo retrieveAlways(Document document) {
WidgetDocumentInfo docInfo = retrieve(document);
if (docInfo == null) {
docInfo = new WidgetDocumentInfo();
document.setUserData(DOCUMENT_USER_DATA_KEY, docInfo, null);
}
return docInfo;
}
/**
* Take an {@link InputSource} and parse it generating map of template ID to Message objects, types, and schema.
*
* @param is source from which schema is read. Ideally it will have the systemId property set to resolve
* relative references.
* @param options to be applied during parsing.
* @return {@link MessageSchema} encoding for the schema.
* @throws Exception on parsing error.
*/
public static MessageSchema parse(final InputSource is, final ParserOptions options) throws Exception
{
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if (options.xIncludeAware())
{
factory.setNamespaceAware(true);
factory.setXIncludeAware(true);
factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
}
final Document document = factory.newDocumentBuilder().parse(is);
final XPath xPath = XPathFactory.newInstance().newXPath();
final ErrorHandler errorHandler = new ErrorHandler(options);
document.setUserData(ERROR_HANDLER_KEY, errorHandler, null);
final Map<String, Type> typeByNameMap = findTypes(document, xPath);
errorHandler.checkIfShouldExit();
final Map<Long, Message> messageByIdMap = findMessages(document, xPath, typeByNameMap);
errorHandler.checkIfShouldExit();
final Node schemaNode = (Node)xPath.compile(MESSAGE_SCHEMA_XPATH_EXPR).evaluate(document, XPathConstants.NODE);
final MessageSchema messageSchema = new MessageSchema(schemaNode, typeByNameMap, messageByIdMap);
errorHandler.checkIfShouldExit();
return messageSchema;
}
private static void parseTestXmlAddToMap(
final Map<String, Type> map, final String xPathExpr, final String xml, final ErrorHandler handler)
throws ParserConfigurationException, XPathExpressionException, IOException, SAXException
{
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
final XPath xPath = XPathFactory.newInstance().newXPath();
final NodeList list = (NodeList)xPath.compile(xPathExpr).evaluate(document, XPathConstants.NODESET);
document.setUserData(XmlSchemaParser.ERROR_HANDLER_KEY, handler, null);
for (int i = 0, size = list.getLength(); i < size; i++)
{
Type type = null;
if (xPathExpr.endsWith("enum"))
{
type = new EnumType(list.item(i));
}
else if (xPathExpr.endsWith("set"))
{
type = new SetType(list.item(i));
}
else if (xPathExpr.endsWith("type"))
{
type = new EncodedDataType(list.item(i));
}
else if (xPathExpr.endsWith("composite"))
{
type = new CompositeType(list.item(i));
}
if (type != null)
{
map.put(type.name(), type);
}
}
}
/**
* Decorates the document with the specified file name, which can be
* retrieved later by calling {@link #extractLineNumber(Node)}.
* <p/>
* It also tries to add line number information, with the caveat that the
* current implementation is a gross approximation.
* <p/>
* There is no need to call this after calling one of the {@code parseDocument()}
* methods since they already decorated their own document.
*
* @param doc The document to decorate.
* @param fileName The name to retrieve later for that document.
*/
static void decorateDocument(@NonNull Document doc, @NonNull String fileName) {
doc.setUserData(DATA_FILE_NAME, fileName, null /*handler*/);
findLineNumbers(doc, 1);
}
/**
* Decorates the document with the specified file name, which can be
* retrieved later by calling {@link #extractLineNumber(Node)}.
* <p/>
* It also tries to add line number information, with the caveat that the
* current implementation is a gross approximation.
* <p/>
* There is no need to call this after calling one of the {@code parseDocument()}
* methods since they already decorated their own document.
*
* @param doc The document to decorate.
* @param fileName The name to retrieve later for that document.
*/
static void decorateDocument(@NonNull Document doc, @NonNull String fileName) {
doc.setUserData(DATA_FILE_NAME, fileName, null /*handler*/);
findLineNumbers(doc, 1);
}