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

The following examples show how to use org.w3c.dom.Document#setUserData() . 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: SetTypeTest.java    From simple-binary-encoding with Apache License 2.0 6 votes vote down vote up
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
Source File: EnumTypeTest.java    From simple-binary-encoding with Apache License 2.0 6 votes vote down vote up
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
Source File: CompositeTypeTest.java    From simple-binary-encoding with Apache License 2.0 6 votes vote down vote up
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
Source File: EncodedDataTypeTest.java    From simple-binary-encoding with Apache License 2.0 6 votes vote down vote up
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 File: MiniLangUtil.java    From scipio-erp with 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 File: WidgetDocumentInfo.java    From scipio-erp with 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
Source File: XmlSchemaParser.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
/**
 * 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
Source File: ErrorHandlerTest.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
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
Source File: MergerXmlUtils.java    From java-n-IDE-for-Android with Apache License 2.0 2 votes vote down vote up
/**
 * 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
Source File: MergerXmlUtils.java    From javaide with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}