Search in sources :

Example 71 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project buck by facebook.

the class SchemeGeneratorTest method whenProvidedAPrimaryTargetThatIsBuiltWithBuckDoesntBuildDependencies.

@Test
public void whenProvidedAPrimaryTargetThatIsBuiltWithBuckDoesntBuildDependencies() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget libraryTarget = new PBXNativeTarget("library");
    libraryTarget.setGlobalID("libraryGID");
    libraryTarget.setProductReference(new PBXFileReference("lib.a", "lib.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    libraryTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget rootTarget = new PBXNativeTarget("rootRule");
    rootTarget.setGlobalID("rootGID");
    rootTarget.setProductName("Foo");
    String runnablePath = "buck-out/gen/Foo/Foo.app";
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(libraryTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(rootTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.of(rootTarget), ImmutableSet.of(libraryTarget, rootTarget), ImmutableSet.of(), ImmutableSet.of(), "TestScheme", Paths.get("_gen/Foo.xcworkspace/scshareddata/xcshemes"), true, /* primaryTargetIsBuildWithBuck */
    false, /* parallelizeBuild */
    Optional.of(runnablePath), /* 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 buildActionXpath = xpathFactory.newXPath();
    XPathExpression buildActionExpr = buildActionXpath.compile("//BuildAction//BuildActionEntry");
    NodeList buildActionNodes = (NodeList) buildActionExpr.evaluate(scheme, XPathConstants.NODESET);
    Node libraryNode = null;
    for (int i = 0; i < buildActionNodes.getLength(); i++) {
        Node node = buildActionNodes.item(i);
        if (node.getChildNodes().getLength() != 1) {
            continue;
        }
        Node buildableReference = node.getChildNodes().item(0);
        if (buildableReference.getAttributes().getNamedItem("BlueprintIdentifier").getNodeValue().equals("libraryGID")) {
            libraryNode = node;
            break;
        }
    }
    assertThat(libraryNode, is(notNullValue()));
    assertThat(libraryNode.getAttributes().getNamedItem("buildForRunning").getNodeValue(), equalTo("NO"));
}
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) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) 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 72 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project buck by facebook.

the class SchemeGeneratorTest method schemeWithNoPrimaryRuleCanIncludeTests.

@Test
public void schemeWithNoPrimaryRuleCanIncludeTests() throws Exception {
    ImmutableMap.Builder<PBXTarget, Path> targetToProjectPathMapBuilder = ImmutableMap.builder();
    PBXTarget testLibraryTarget = new PBXNativeTarget("testLibrary");
    testLibraryTarget.setGlobalID("testLibraryGID");
    testLibraryTarget.setProductReference(new PBXFileReference("lib.a", "lib.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testLibraryTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget testTarget = new PBXNativeTarget("testRule");
    testTarget.setGlobalID("testGID");
    testTarget.setProductReference(new PBXFileReference("test.a", "test.a", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testTarget.setProductType(ProductType.STATIC_LIBRARY);
    PBXTarget testBundleTarget = new PBXNativeTarget("testBundleRule");
    testBundleTarget.setGlobalID("testBundleGID");
    testBundleTarget.setProductReference(new PBXFileReference("test.xctest", "test.xctest", PBXReference.SourceTree.BUILT_PRODUCTS_DIR, Optional.empty()));
    testBundleTarget.setProductType(ProductType.UNIT_TEST);
    Path pbxprojectPath = Paths.get("foo/Foo.xcodeproj/project.pbxproj");
    targetToProjectPathMapBuilder.put(testLibraryTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(testTarget, pbxprojectPath);
    targetToProjectPathMapBuilder.put(testBundleTarget, pbxprojectPath);
    SchemeGenerator schemeGenerator = new SchemeGenerator(projectFilesystem, Optional.empty(), ImmutableSet.of(), ImmutableSet.of(testBundleTarget), ImmutableSet.of(testBundleTarget), "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();
    String schemeXml = projectFilesystem.readFileIfItExists(schemePath).get();
    System.out.println(schemeXml);
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document scheme = dBuilder.parse(projectFilesystem.newFileInputStream(schemePath));
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath buildActionXpath = xpathFactory.newXPath();
    XPathExpression buildActionExpr = buildActionXpath.compile("//BuildAction//BuildableReference/@BlueprintIdentifier");
    NodeList buildActionNodes = (NodeList) buildActionExpr.evaluate(scheme, XPathConstants.NODESET);
    List<String> expectedOrdering = ImmutableList.of("testBundleGID");
    List<String> actualOrdering = Lists.newArrayList();
    for (int i = 0; i < buildActionNodes.getLength(); i++) {
        actualOrdering.add(buildActionNodes.item(i).getNodeValue());
    }
    assertThat(actualOrdering, equalTo(expectedOrdering));
    XPath testActionXpath = xpathFactory.newXPath();
    XPathExpression testActionExpr = testActionXpath.compile("//TestAction//BuildableReference/@BlueprintIdentifier");
    String testActionBlueprintIdentifier = (String) testActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(testActionBlueprintIdentifier, equalTo("testBundleGID"));
    XPath launchActionXpath = xpathFactory.newXPath();
    XPathExpression launchActionExpr = launchActionXpath.compile("//LaunchAction//BuildableReference/@BlueprintIdentifier");
    String launchActionBlueprintIdentifier = (String) launchActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(launchActionBlueprintIdentifier, equalTo(""));
    XPath launchActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression launchActionBuildConfigurationExpr = launchActionBuildConfigurationXpath.compile("//LaunchAction//@buildConfiguration");
    String launchActionBuildConfigurationBlueprintIdentifier = (String) launchActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(launchActionBuildConfigurationBlueprintIdentifier, equalTo("Debug"));
    XPath profileActionXpath = xpathFactory.newXPath();
    XPathExpression profileActionExpr = profileActionXpath.compile("//ProfileAction//BuildableReference/@BlueprintIdentifier");
    String profileActionBlueprintIdentifier = (String) profileActionExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(profileActionBlueprintIdentifier, equalTo(""));
    XPath profileActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression profileActionBuildConfigurationExpr = profileActionBuildConfigurationXpath.compile("//ProfileAction//@buildConfiguration");
    String profileActionBuildConfigurationBlueprintIdentifier = (String) profileActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(profileActionBuildConfigurationBlueprintIdentifier, equalTo("Release"));
    XPath analyzeActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression analyzeActionBuildConfigurationExpr = analyzeActionBuildConfigurationXpath.compile("//AnalyzeAction//@buildConfiguration");
    String analyzeActionBuildConfigurationBlueprintIdentifier = (String) analyzeActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(analyzeActionBuildConfigurationBlueprintIdentifier, equalTo("Debug"));
    XPath archiveActionBuildConfigurationXpath = xpathFactory.newXPath();
    XPathExpression archiveActionBuildConfigurationExpr = archiveActionBuildConfigurationXpath.compile("//ArchiveAction//@buildConfiguration");
    String archiveActionBuildConfigurationBlueprintIdentifier = (String) archiveActionBuildConfigurationExpr.evaluate(scheme, XPathConstants.STRING);
    assertThat(archiveActionBuildConfigurationBlueprintIdentifier, equalTo("Release"));
}
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) 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 73 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project buck by facebook.

the class WorkspaceGeneratorTest method testFlatWorkspaceContainsCorrectFileRefs.

@Test
public void testFlatWorkspaceContainsCorrectFileRefs() throws Exception {
    generator.addFilePath(Paths.get("./Project.xcodeproj"));
    Path workspacePath = generator.writeWorkspace();
    Path contentsPath = workspacePath.resolve("contents.xcworkspacedata");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document workspace = dBuilder.parse(projectFilesystem.newFileInputStream(contentsPath));
    assertThat(workspace, hasXPath("/Workspace[@version = \"1.0\"]"));
    assertThat(workspace, hasXPath("/Workspace/FileRef/@location", equalTo("container:Project.xcodeproj")));
}
Also used : Path(java.nio.file.Path) Matchers.hasXPath(org.hamcrest.Matchers.hasXPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 74 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project buck by facebook.

the class WorkspaceGeneratorTest method testWorkspaceContainsNodeInAlphabeticalOrder.

@Test
public void testWorkspaceContainsNodeInAlphabeticalOrder() throws Exception {
    generator.addFilePath(Paths.get("./2/parent/C.xcodeproj"));
    generator.addFilePath(Paths.get("./2/parent/B.xcodeproj"));
    generator.addFilePath(Paths.get("./2/parent/D.xcodeproj"));
    generator.addFilePath(Paths.get("./1/parent/E.xcodeproj"));
    generator.addFilePath(Paths.get("./3/parent/A.xcodeproj"));
    Path workspacePath = generator.writeWorkspace();
    Path contentsPath = workspacePath.resolve("contents.xcworkspacedata");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document workspace = dBuilder.parse(projectFilesystem.newFileInputStream(contentsPath));
    Node workspaceNode = workspace.getChildNodes().item(0);
    ImmutableList.Builder<String> groupsBuilder = ImmutableList.builder();
    for (int i = 0; i < workspaceNode.getChildNodes().getLength(); ++i) {
        groupsBuilder.add(workspaceNode.getChildNodes().item(i).getAttributes().getNamedItem("name").getNodeValue());
    }
    assertThat(groupsBuilder.build(), equalTo(ImmutableList.of("1", "2", "3")));
    Node secondGroup = workspaceNode.getChildNodes().item(1);
    assertThat(secondGroup.getAttributes().getNamedItem("name").getNodeValue(), equalTo("2"));
    ImmutableList.Builder<String> projectsBuilder = ImmutableList.builder();
    for (int i = 0; i < secondGroup.getChildNodes().getLength(); ++i) {
        projectsBuilder.add(secondGroup.getChildNodes().item(i).getAttributes().getNamedItem("location").getNodeValue());
    }
    assertThat(projectsBuilder.build(), equalTo(ImmutableList.of("container:2/parent/B.xcodeproj", "container:2/parent/C.xcodeproj", "container:2/parent/D.xcodeproj")));
}
Also used : Path(java.nio.file.Path) Matchers.hasXPath(org.hamcrest.Matchers.hasXPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ImmutableList(com.google.common.collect.ImmutableList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 75 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project tinker by Tencent.

the class AndroidParser method parse.

private void parse() throws ParserException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    Document document;
    try {
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        Node manifestNode = document.getElementsByTagName("manifest").item(0);
        NodeList nodes = manifestNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            String nodeName = node.getNodeName();
            if (nodeName.equals("application")) {
                NodeList children = node.getChildNodes();
                for (int j = 0; j < children.getLength(); j++) {
                    Node child = children.item(j);
                    String childName = child.getNodeName();
                    switch(childName) {
                        case "service":
                            services.add(getAndroidComponent(child, TYPE_SERVICE));
                            break;
                        case "activity":
                            activities.add(getAndroidComponent(child, TYPE_ACTIVITY));
                            break;
                        case "receiver":
                            receivers.add(getAndroidComponent(child, TYPE_BROADCAST_RECEIVER));
                            break;
                        case "provider":
                            providers.add(getAndroidComponent(child, TYPE_CONTENT_PROVIDER));
                            break;
                        case "meta-data":
                            NamedNodeMap attributes = child.getAttributes();
                            metaDatas.put(getAttribute(attributes, "android:name"), getAttribute(attributes, "android:value"));
                            break;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ParserException("Error parsing AndroidManifest.xml", e);
    }
}
Also used : ParserException(net.dongliu.apk.parser.exception.ParserException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) IOException(java.io.IOException) ParserException(net.dongliu.apk.parser.exception.ParserException) ParseException(java.text.ParseException)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56