Search in sources :

Example 91 with NodeList

use of org.w3c.dom.NodeList in project dropwizard by dropwizard.

the class DbDumpCommandTest method assertCreateTable.

/**
     * Assert correctness of a change set with creation of a table
     *
     * @param changeSet actual XML element
     */
private static void assertCreateTable(Element changeSet) {
    final Element createTable = getFirstElement(changeSet, "createTable");
    assertThat(createTable.getAttribute("catalogName")).isEqualTo("TEST-DB");
    assertThat(createTable.getAttribute("schemaName")).isEqualTo("PUBLIC");
    assertThat(createTable.getAttribute("tableName")).isEqualTo("PERSONS");
    final NodeList columns = createTable.getElementsByTagName("column");
    final Element idColumn = (Element) columns.item(0);
    assertThat(idColumn.getAttribute("autoIncrement")).isEqualTo("true");
    assertThat(idColumn.getAttribute("name")).isEqualTo("ID");
    assertThat(idColumn.getAttribute("type")).isEqualTo("INT(10)");
    final Element idColumnConstraints = getFirstElement(idColumn, "constraints");
    assertThat(idColumnConstraints.getAttribute("primaryKey")).isEqualTo("true");
    assertThat(idColumnConstraints.getAttribute("primaryKeyName")).isEqualTo("PK_PERSONS");
    final Element nameColumn = (Element) columns.item(1);
    assertThat(nameColumn.getAttribute("name")).isEqualTo("NAME");
    assertThat(nameColumn.getAttribute("type")).isEqualTo("VARCHAR(256)");
    final Element nameColumnConstraints = getFirstElement(nameColumn, "constraints");
    assertThat(nameColumnConstraints.getAttribute("nullable")).isEqualTo("false");
    final Element emailColumn = (Element) columns.item(2);
    assertThat(emailColumn.getAttribute("name")).isEqualTo("EMAIL");
    assertThat(emailColumn.getAttribute("type")).isEqualTo("VARCHAR(128)");
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList)

Example 92 with NodeList

use of org.w3c.dom.NodeList in project dropwizard by dropwizard.

the class DbDumpCommandTest method testDumpSchemaAndData.

@Test
public void testDumpSchemaAndData() throws Exception {
    final Map<String, Object> attributes = new HashMap<>();
    for (String name : Iterables.concat(attributeNames, ImmutableList.of("data"))) {
        attributes.put(name, true);
    }
    dumpCommand.run(null, new Namespace(attributes), existedDbConf);
    final NodeList changeSets = toXmlDocument(baos).getDocumentElement().getElementsByTagName("changeSet");
    assertCreateTable((Element) changeSets.item(0));
    assertInsertData((Element) changeSets.item(1));
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.Test)

Example 93 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class ClasspathEntry method decodeUnknownNode.

private static void decodeUnknownNode(Node node, XMLWriter xmlWriter, boolean insertNewLine) {
    switch(node.getNodeType()) {
        case Node.ELEMENT_NODE:
            NamedNodeMap attributes;
            HashMap parameters = null;
            if ((attributes = node.getAttributes()) != null) {
                int length = attributes.getLength();
                if (length > 0) {
                    parameters = new HashMap();
                    for (int i = 0; i < length; i++) {
                        Node attribute = attributes.item(i);
                        parameters.put(attribute.getNodeName(), attribute.getNodeValue());
                    }
                }
            }
            NodeList children = node.getChildNodes();
            int childrenLength = children.getLength();
            String nodeName = node.getNodeName();
            xmlWriter.printTag(nodeName, parameters, false, /*don't insert tab*/
            false, /*don't insert new line*/
            childrenLength == 0);
            if (childrenLength > 0) {
                for (int i = 0; i < childrenLength; i++) {
                    decodeUnknownNode(children.item(i), xmlWriter, false);
                }
                xmlWriter.endTag(nodeName, false, /*don't insert tab*/
                insertNewLine);
            }
            break;
        case Node.TEXT_NODE:
            String data = ((Text) node).getData();
            xmlWriter.printString(data, false, /*don't insert tab*/
            false);
            break;
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Example 94 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class TemplateReaderWriter method read.

/**
	 * Reads templates from an <code>InputSource</code> and adds them to the templates.
	 *
	 * @param source the input source
	 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur
	 * @param singleId the template id to extract, or <code>null</code> to read in all templates
	 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code>
	 * @throws IOException if reading from the stream fails
	 */
private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) throws IOException {
    try {
        Collection templates = new ArrayList();
        Set ids = new HashSet();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();
        parser.setErrorHandler(new DefaultHandler());
        Document document = parser.parse(source);
        NodeList elements = document.getElementsByTagName(TEMPLATE_ELEMENT);
        int count = elements.getLength();
        for (int i = 0; i != count; i++) {
            Node node = elements.item(i);
            NamedNodeMap attributes = node.getAttributes();
            if (attributes == null)
                continue;
            String id = getStringValue(attributes, ID_ATTRIBUTE, null);
            if (id != null && ids.contains(id))
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.duplicate.id"));
            if (singleId != null && !singleId.equals(id))
                continue;
            boolean deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false);
            String name = getStringValue(attributes, NAME_ATTRIBUTE);
            name = translateString(name, bundle);
            //$NON-NLS-1$
            String description = getStringValue(attributes, DESCRIPTION_ATTRIBUTE, "");
            description = translateString(description, bundle);
            String context = getStringValue(attributes, CONTEXT_ATTRIBUTE);
            if (name == null || context == null)
                //$NON-NLS-1$
                throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.missing_attribute"));
            boolean enabled = getBooleanValue(attributes, ENABLED_ATTRIBUTE, true);
            boolean autoInsertable = getBooleanValue(attributes, AUTO_INSERTABLE_ATTRIBUTE, true);
            StringBuffer buffer = new StringBuffer();
            NodeList children = node.getChildNodes();
            for (int j = 0; j != children.getLength(); j++) {
                String value = children.item(j).getNodeValue();
                if (value != null)
                    buffer.append(value);
            }
            String pattern = buffer.toString();
            pattern = translateString(pattern, bundle);
            Template template = new Template(name, description, context, pattern, autoInsertable);
            TemplatePersistenceData data = new TemplatePersistenceData(template, enabled, id);
            data.setDeleted(deleted);
            templates.add(data);
            if (singleId != null && singleId.equals(id))
                break;
        }
        return (TemplatePersistenceData[]) templates.toArray(new TemplatePersistenceData[templates.size()]);
    } catch (ParserConfigurationException e) {
        Assert.isTrue(false);
    } catch (SAXException e) {
        //$NON-NLS-1$
        throw (IOException) new IOException("Could not read template file").initCause(e);
    }
    // dummy
    return null;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Template(org.eclipse.jface.text.templates.Template) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Collection(java.util.Collection) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Example 95 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class DialogSettings method load.

/* (non-Javadoc)
     * Load the setting from the <code>document</code>
     */
private void load(Document document, Element root) {
    name = root.getAttribute(TAG_NAME);
    NodeList l = root.getElementsByTagName(TAG_ITEM);
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (root == n.getParentNode()) {
            String key = ((Element) l.item(i)).getAttribute(TAG_KEY);
            String value = ((Element) l.item(i)).getAttribute(TAG_VALUE);
            items.put(key, value);
        }
    }
    l = root.getElementsByTagName(TAG_LIST);
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (root == n.getParentNode()) {
            Element child = (Element) l.item(i);
            String key = child.getAttribute(TAG_KEY);
            NodeList list = child.getElementsByTagName(TAG_ITEM);
            List<String> valueList = new ArrayList<String>();
            for (int j = 0; j < list.getLength(); j++) {
                Element node = (Element) list.item(j);
                if (child == node.getParentNode()) {
                    valueList.add(node.getAttribute(TAG_VALUE));
                }
            }
            String[] value = new String[valueList.size()];
            valueList.toArray(value);
            arrayItems.put(key, value);
        }
    }
    l = root.getElementsByTagName(TAG_SECTION);
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (root == n.getParentNode()) {
            //$NON-NLS-1$
            DialogSettings s = new DialogSettings("NoName");
            s.load(document, (Element) n);
            addSection(s);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59