Search in sources :

Example 1 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project buck by facebook.

the class SchemeGeneratorTest method buildableReferenceShouldHaveExpectedProperties.

@Test
public void buildableReferenceShouldHaveExpectedProperties() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget rootTarget = new PBXNativeTarget("rootRule");
    rootTarget.setGlobalID("rootGID");
    rootTarget.setProductReference(new PBXFileReference("root.a", "root.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    rootTarget.setProductType(ProductType.STATIC_LIBRARY);
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(rootTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.of(rootTarget), ImmutableSet.of(rootTarget), ImmutableSet.of(), ImmutableSet.of(), "TestScheme", Paths.get("_gen/Foo.xcworkspace/scshareddata/xcshemes"), false, /* primaryTargetIsBuildWithBuck */
    false, /* parallelizeBuild */
    Optional.empty(), /* runnablePath */
    Optional.empty(), /* remoteRunnablePath */
    SchemeActionType.DEFAULT_CONFIG_NAMES, targetToProjectPathMapBuilder.build(), XCScheme.LaunchAction.LaunchStyle.AUTO);
    Path schemePath = schemeGenerator.writeScheme();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document scheme = dBuilder.parse(projectFilesystem.newFileInputStream(schemePath));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath buildableReferenceXPath = xpathFactory.newXPath();
    XPathExpression buildableReferenceExpr = buildableReferenceXPath.compile("//BuildableReference");
    NodeList buildableReferences = (NodeList) buildableReferenceExpr.evaluate(scheme, XPathConstants.NODESET);
    assertThat(buildableReferences.getLength(), greaterThan(0));
    for (int i = 0; i < buildableReferences.getLength(); i++) {
        NamedNodeMap attributes = buildableReferences.item(i).getAttributes();
        assertThat(attributes, notNullValue());
        assertThat(attributes.getNamedItem("BlueprintIdentifier"), notNullValue());
        assertThat(attributes.getNamedItem("BuildableIdentifier"), notNullValue());
        assertThat(attributes.getNamedItem("ReferencedContainer"), notNullValue());
        assertThat(attributes.getNamedItem("BlueprintName"), notNullValue());
        assertThat(attributes.getNamedItem("BuildableName"), notNullValue());
    }
}
Also used : PBXTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXTarget) XPath(javax.xml.xpath.XPath) Path(java.nio.file.Path) XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) PBXNativeTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ImmutableMap(com.google.common.collect.ImmutableMap) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference) Test(org.junit.Test)

Example 2 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap 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 3 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap 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 4 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project cogtool by cogtool.

the class ImportCogToolXML method addAttributes.

private static void addAttributes(IAttributed attributed, Node node, Map<IAttributed, String> pendingAttrSets) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        int numAttributes = attributes.getLength();
        if (numAttributes > 0) {
            if (notInitialized) {
                registerAttributes();
            }
            for (int i = 0; i < numAttributes; i++) {
                Node attributeNode = attributes.item(i);
                // Should never be null; sanity check
                if (attributeNode != null) {
                    String attribute = attributeNode.getNodeName();
                    IAttributed.AttributeDefinition<?> attrDefn = ATTRIBUTE_REGISTRY.get(attribute);
                    if (attrDefn != null) {
                        String attrName = attrDefn.attrName;
                        String attrNodeValue = attributeNode.getNodeValue();
                        if (WidgetAttributes.SELECTION_ATTR.equals(attrName)) {
                            // attrNodeValue is name of the selected widget,
                            // but the widget may not exist yet.
                            pendingAttrSets.put(attributed, attrNodeValue);
                        } else if (VALUE_REGISTRY.containsKey(attrNodeValue)) {
                            Object attrValue = VALUE_REGISTRY.get(attrNodeValue);
                            if (!NullSafe.equals(attrValue, attrDefn.defaultValue)) {
                                attributed.setAttribute(attrName, attrValue);
                            }
                        } else {
                            // Assume string value (eg, APPENDED_TEXT_ATTR)
                            attributed.setAttribute(attrName, attrNodeValue);
                        }
                    }
                }
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed)

Example 5 with NamedNodeMap

use of org.w3c.dom.NamedNodeMap in project Apktool by iBotPeaches.

the class ResXmlPatcher method removeApplicationDebugTag.

/**
     * Removes "debug" tag from file
     *
     * @param file AndroidManifest file
     * @throws AndrolibException
     */
public static void removeApplicationDebugTag(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            Node application = doc.getElementsByTagName("application").item(0);
            // load attr
            NamedNodeMap attr = application.getAttributes();
            Node debugAttr = attr.getNamedItem("android:debuggable");
            // remove application:debuggable
            if (debugAttr != null) {
                attr.removeNamedItem("android:debuggable");
            }
            saveDocument(file, doc);
        } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

NamedNodeMap (org.w3c.dom.NamedNodeMap)985 Node (org.w3c.dom.Node)682 NodeList (org.w3c.dom.NodeList)334 Attr (org.w3c.dom.Attr)293 Element (org.w3c.dom.Element)235 Document (org.w3c.dom.Document)151 ArrayList (java.util.ArrayList)92 HashMap (java.util.HashMap)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)58 IOException (java.io.IOException)53 SAXException (org.xml.sax.SAXException)41 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)40 List (java.util.List)36 Map (java.util.Map)33 File (java.io.File)27 InputStream (java.io.InputStream)26 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)25 DOMException (org.w3c.dom.DOMException)25 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19