Java Code Examples for org.w3c.dom.Document#setUserData()

The following examples show how to use org.w3c.dom.Document#setUserData() . These examples are extracted from open source projects. 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
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;
}
 
Example 2
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;
}
 
Example 3
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;
}
 
Example 4
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;
}
 
Example 5
Source Project: scipio-erp   File: MiniLangUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 6
Source Project: scipio-erp   File: WidgetDocumentInfo.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
/**
 * 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;
}
 
Example 8
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);
        }
    }
}
 
Example 9
/**
 * 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);
}
 
Example 10
/**
 * 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);
}