Java Code Examples for com.android.utils.XmlUtils

The following examples show how to use com.android.utils.XmlUtils. 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
Source Project: java-n-IDE-for-Android   Source File: XmlPrettyPrinter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Pretty-prints the given XML document, which must be well-formed. If it is not,
 * the original unformatted XML document is returned
 *
 * @param xml the XML content to format
 * @param prefs the preferences to format with
 * @param style the style to format with
 * @param lineSeparator the line separator to use, such as "\n" (can be null, in which
 *     case the system default is looked up via the line.separator property)
 * @return the formatted document (or if a parsing error occurred, returns the
 *     unformatted document)
 */
@NonNull
public static String prettyPrint(
        @NonNull String xml,
        @NonNull XmlFormatPreferences prefs,
        @NonNull XmlFormatStyle style,
        @Nullable String lineSeparator) {
    Document document = XmlUtils.parseDocumentSilently(xml, true);
    if (document != null) {
        XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, lineSeparator);
        printer.setEndWithNewline(xml.endsWith(printer.getLineSeparator()));
        StringBuilder sb = new StringBuilder(3 * xml.length() / 2);
        printer.prettyPrint(-1, document, null, null, sb, false /*openTagOnly*/);
        return sb.toString();
    } else {
        // Parser error: just return the unformatted content
        return xml;
    }
}
 
Example 2
Source Project: java-n-IDE-for-Android   Source File: OrphanXmlElement.java    License: Apache License 2.0 6 votes vote down vote up
public OrphanXmlElement(@NonNull Element xml) {

        mXml = Preconditions.checkNotNull(xml);
        NodeTypes nodeType;
        String elementName = mXml.getNodeName();
        // this is bit more complicated than it should be. Look first if there is a namespace
        // prefix in the name, most elements don't. If they do, however, strip it off if it is the
        // android prefix, but if it's custom namespace prefix, classify the node as CUSTOM.
        int indexOfColon = elementName.indexOf(':');
        if (indexOfColon != -1) {
            String androidPrefix = XmlUtils.lookupNamespacePrefix(xml, SdkConstants.ANDROID_URI);
            if (androidPrefix.equals(elementName.substring(0, indexOfColon))) {
                nodeType = NodeTypes.fromXmlSimpleName(elementName.substring(indexOfColon + 1));
            } else {
                nodeType = NodeTypes.CUSTOM;
            }
        } else {
            nodeType = NodeTypes.fromXmlSimpleName(elementName);
        }
        mType = nodeType;
    }
 
Example 3
Source Project: java-n-IDE-for-Android   Source File: MergerXmlUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given XML string as a DOM document.
 * The parser does not validate the DTD nor any kind of schema.
 * It is namespace aware.
 *
 * @param xml The XML string to parse. Must not be null.
 * @param log An {@link ILogger} for reporting errors. Must not be null.
 * @return A new DOM {@link Document}, or null.
 */
@VisibleForTesting
@Nullable
static Document parseDocument(@NonNull String xml,
        @NonNull IMergerLog log,
        @NonNull FileAndLine errorContext) {
    try {
        Document doc = XmlUtils.parseDocument(xml, true);
        findLineNumbers(doc, 1);
        if (errorContext.getFileName() != null) {
            setSource(doc, new File(errorContext.getFileName()));
        }
        return doc;
    } catch (Exception e) {
        log.error(Severity.ERROR, errorContext, "Failed to parse XML string");
    }

    return null;
}
 
Example 4
Source Project: javaide   Source File: Extractor.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void mergeAnnotationsXml(@NonNull String path, @NonNull String xml) {
    try {
        Document document = XmlUtils.parseDocument(xml, false);
        mergeDocument(document);
    } catch (Exception e) {
        String message = "Failed to merge " + path + ": " + e.toString();
        if (e instanceof SAXParseException) {
            SAXParseException spe = (SAXParseException) e;
            message = "Line " + spe.getLineNumber() + ":" + spe.getColumnNumber() + ": " + message;
        }
        error(message);
        if (!(e instanceof IOException)) {
            e.printStackTrace();
        }
    }
}
 
Example 5
Source Project: javaide   Source File: ManifestMerger2.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static void addToElementInAndroidNS(
        SystemProperty systemProperty,
        ActionRecorder actionRecorder,
        String value,
        XmlElement to) {

    String toolsPrefix = getAndroidPrefix(to.getXml());
    to.getXml().setAttributeNS(SdkConstants.ANDROID_URI,
            toolsPrefix + XmlUtils.NS_SEPARATOR + systemProperty.toCamelCase(),
            value);
    Attr attr = to.getXml().getAttributeNodeNS(SdkConstants.ANDROID_URI,
            systemProperty.toCamelCase());

    XmlAttribute xmlAttribute = new XmlAttribute(to, attr, null);
    actionRecorder.recordAttributeAction(xmlAttribute,
            new Actions.AttributeRecord(
                    Actions.ActionType.INJECTED,
                    new SourceFilePosition(to.getSourceFile(), SourcePosition.UNKNOWN),
                    xmlAttribute.getId(),
                    null, /* reason */
                    null /* attributeOperationType */
            )
    );

}
 
Example 6
Source Project: javaide   Source File: OrphanXmlElement.java    License: GNU General Public License v3.0 6 votes vote down vote up
public OrphanXmlElement(@NonNull Element xml) {

        mXml = Preconditions.checkNotNull(xml);
        NodeTypes nodeType;
        String elementName = mXml.getNodeName();
        // this is bit more complicated than it should be. Look first if there is a namespace
        // prefix in the name, most elements don't. If they do, however, strip it off if it is the
        // android prefix, but if it's custom namespace prefix, classify the node as CUSTOM.
        int indexOfColon = elementName.indexOf(':');
        if (indexOfColon != -1) {
            String androidPrefix = XmlUtils.lookupNamespacePrefix(xml, SdkConstants.ANDROID_URI);
            if (androidPrefix.equals(elementName.substring(0, indexOfColon))) {
                nodeType = NodeTypes.fromXmlSimpleName(elementName.substring(indexOfColon + 1));
            } else {
                nodeType = NodeTypes.CUSTOM;
            }
        } else {
            nodeType = NodeTypes.fromXmlSimpleName(elementName);
        }
        mType = nodeType;
    }
 
Example 7
Source Project: javaide   Source File: MergerXmlUtils.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the given XML string as a DOM document.
 * The parser does not validate the DTD nor any kind of schema.
 * It is namespace aware.
 *
 * @param xml The XML string to parse. Must not be null.
 * @param log An {@link ILogger} for reporting errors. Must not be null.
 * @return A new DOM {@link Document}, or null.
 */
@VisibleForTesting
@Nullable
static Document parseDocument(@NonNull String xml,
        @NonNull IMergerLog log,
        @NonNull FileAndLine errorContext) {
    try {
        Document doc = XmlUtils.parseDocument(xml, true);
        findLineNumbers(doc, 1);
        if (errorContext.getFileName() != null) {
            setSource(doc, new File(errorContext.getFileName()));
        }
        return doc;
    } catch (Exception e) {
        log.error(Severity.ERROR, errorContext, "Failed to parse XML string");
    }

    return null;
}
 
Example 8
Source Project: javaide   Source File: ViewTypeDetector.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private Multimap<String, String> getIdToTagsIn(@NonNull Context context, @NonNull File file) {
    if (!file.getPath().endsWith(DOT_XML)) {
        return null;
    }
    if (mFileIdMap == null) {
        mFileIdMap = Maps.newHashMap();
    }
    Multimap<String, String> map = mFileIdMap.get(file);
    if (map == null) {
        map = ArrayListMultimap.create();
        mFileIdMap.put(file, map);

        String xml = context.getClient().readFile(file);
        // TODO: Use pull parser instead for better performance!
        Document document = XmlUtils.parseDocumentSilently(xml, true);
        if (document != null && document.getDocumentElement() != null) {
            addViewTags(map, document.getDocumentElement());
        }
    }
    return map;
}
 
Example 9
Source Project: javaide   Source File: XmlPrettyPrinter.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Pretty-prints the given XML document, which must be well-formed. If it is not,
 * the original unformatted XML document is returned
 *
 * @param xml the XML content to format
 * @param prefs the preferences to format with
 * @param style the style to format with
 * @param lineSeparator the line separator to use, such as "\n" (can be null, in which
 *     case the system default is looked up via the line.separator property)
 * @return the formatted document (or if a parsing error occurred, returns the
 *     unformatted document)
 */
@NonNull
public static String prettyPrint(
        @NonNull String xml,
        @NonNull XmlFormatPreferences prefs,
        @NonNull XmlFormatStyle style,
        @Nullable String lineSeparator) {
    Document document = XmlUtils.parseDocumentSilently(xml, true);
    if (document != null) {
        XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, lineSeparator);
        printer.setEndWithNewline(xml.endsWith(printer.getLineSeparator()));
        StringBuilder sb = new StringBuilder(3 * xml.length() / 2);
        printer.prettyPrint(-1, document, null, null, sb, false /*openTagOnly*/);
        return sb.toString();
    } else {
        // Parser error: just return the unformatted content
        return xml;
    }
}
 
Example 10
Source Project: NBANDROID-V2   Source File: AndroidManifestParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses the Android Manifest from an {@link InputStream}, and returns a
 * {@link ManifestData} object containing the result of the parsing.
 *
 * @param manifestFileStream the {@link InputStream} representing the
 * manifest file.
 * @return A class containing the manifest info obtained during the parsing
 * or null on error.
 */
public static ManifestData parse(InputStream manifestFileStream)
        throws ParserConfigurationException, SAXException, IOException {
    if (manifestFileStream != null) {
        SAXParser parser = XmlUtils.createSaxParser(sParserFactory);

        ManifestData data = new ManifestData();

        ManifestHandler manifestHandler = new ManifestHandler(data, null);
        parser.parse(new InputSource(manifestFileStream), manifestHandler);

        return data;
    }

    return null;
}
 
Example 11
Source Project: bazel   Source File: ResourceUsageAnalyzer.java    License: Apache License 2.0 6 votes vote down vote up
private void recordResources(@NonNull ResourceFolderType folderType, File folder)
    throws ParserConfigurationException, SAXException, IOException {
  File[] files = folder.listFiles();
  if (files != null) {
    for (File file : files) {
      String path = file.getPath();
      model.file = file;
      try {
        boolean isXml = endsWithIgnoreCase(path, DOT_XML);
        if (isXml) {
          String xml = Files.toString(file, UTF_8);
          Document document = XmlUtils.parseDocument(xml, true);
          model.visitXmlDocument(file, folderType, document);
        } else {
          model.visitBinaryResource(folderType, file);
        }
      } finally {
        model.file = null;
      }
    }
  }
}
 
Example 12
Source Project: java-n-IDE-for-Android   Source File: XmlPrettyPrinter.java    License: Apache License 2.0 5 votes vote down vote up
private boolean keepElementAsSingleLine(int depth, Element element) {
    if (depth == 0) {
        return false;
    }

    return isSingleLineTag(element)
            || (mStyle == XmlFormatStyle.RESOURCE
            && !XmlUtils.hasElementChildren(element));
}
 
Example 13
Source Project: java-n-IDE-for-Android   Source File: ManifestMerger2.java    License: Apache License 2.0 5 votes vote down vote up
private static void addToElementInAndroidNS(
        SystemProperty systemProperty,
        ActionRecorder actionRecorder,
        String value,
        XmlElement to) {

    String toolsPrefix = getAndroidPrefix(to.getXml());
    to.getXml().setAttributeNS(SdkConstants.ANDROID_URI,
            toolsPrefix + XmlUtils.NS_SEPARATOR + systemProperty.toCamelCase(),
            value);
    Attr attr = to.getXml().getAttributeNodeNS(SdkConstants.ANDROID_URI,
            systemProperty.toCamelCase());

    XmlAttribute xmlAttribute = new XmlAttribute(to, attr, null);
    actionRecorder.recordAttributeAction(xmlAttribute,
            new Actions.AttributeRecord(
                    Actions.ActionType.INJECTED,
                    new Actions.ActionLocation(
                            to.getSourceLocation(),
                            PositionImpl.UNKNOWN),
                    xmlAttribute.getId(),
                    null, /* reason */
                    null /* attributeOperationType */
            )
    );

}
 
Example 14
Source Project: java-n-IDE-for-Android   Source File: ManifestMerger2.java    License: Apache License 2.0 5 votes vote down vote up
private static String getAndroidPrefix(Element xml) {
    String toolsPrefix = XmlUtils.lookupNamespacePrefix(
            xml, SdkConstants.ANDROID_URI, SdkConstants.ANDROID_NS_NAME, false);
    if (!toolsPrefix.equals(SdkConstants.ANDROID_NS_NAME) && xml.getOwnerDocument()
            .getDocumentElement().getAttribute("xmlns:" + toolsPrefix) == null) {
        // this is weird, the document is using "android" prefix but it's not bound
        // to our namespace. Add the proper xmlns declaration.
        xml.setAttribute("xmlns:" + toolsPrefix, SdkConstants.ANDROID_URI);
    }
    return toolsPrefix;
}
 
Example 15
Source Project: java-n-IDE-for-Android   Source File: ManifestMerger.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Performs the merge operation in-place in the given DOM.
 * <p/>
 * This does NOT stop on errors, in an attempt to accumulate as much
 * info as possible to return to the user.
 * <p/>
 * The method might modify the input XML documents in-place for its own processing.
 *
 * @param mainDoc The document to merge into. Will be modified in-place.
 * @param libraryDocs The library manifest documents to merge in. Must not be null.
 *                    These will be modified in-place.
 * @return True on success, false if any error occurred (printed to the {@link IMergerLog}).
 */
public boolean process(@NonNull Document mainDoc, @NonNull Document... libraryDocs) {

    boolean success = true;
    mMainDoc = mainDoc;
    MergerXmlUtils.decorateDocument(mainDoc, IMergerLog.MAIN_MANIFEST);

    String prefix = XmlUtils.lookupNamespacePrefix(mainDoc, SdkConstants.NS_RESOURCES);
    mXPath = AndroidXPathFactory.newXPath(prefix);

    expandFqcns(mainDoc);
    for (Document libDoc : libraryDocs) {
        MergerXmlUtils.decorateDocument(libDoc, IMergerLog.LIBRARY);
        if (!mergeLibDoc(cleanupToolsAttributes(libDoc))) {
            success = false;
        }
    }

    cleanupToolsAttributes(mainDoc);

    if (mExtractPackagePrefix) {
        extractFqcns(mainDoc);
    }

    if (mInsertSourceMarkers) {
        insertSourceMarkers(mainDoc);
    }

    mXPath = null;
    mMainDoc = null;
    return success;
}
 
Example 16
private void recordXmlResourcesUsages(@NonNull File file, boolean isDefaultFolder,
        @Nullable Resource from)
        throws IOException, ParserConfigurationException, SAXException {
    String xml = Files.toString(file, UTF_8);
    Document document = XmlUtils.parseDocument(xml, true);
    recordResourceReferences(file, isDefaultFolder, document.getDocumentElement(), from);
}
 
Example 17
Source Project: javaide   Source File: Extractor.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private static Document checkDocument(@NonNull String pkg, @NonNull String xml,
                                      boolean namespaceAware) {
    try {
        return XmlUtils.parseDocument(xml, namespaceAware);
    } catch (SAXException sax) {
        warning("Failed to parse document for package " + pkg + ": " + sax.toString());
    } catch (Exception e) {
        // pass
        // This method is deliberately silent; will return null
    }

    return null;
}
 
Example 18
Source Project: javaide   Source File: LintGradleProject.java    License: GNU General Public License v3.0 5 votes vote down vote up
protected void readManifest(File manifest) {
    if (manifest.exists()) {
        try {
            String xml = Files.toString(manifest, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document != null) {
                readManifest(document);
            }
        } catch (IOException e) {
            mClient.log(e, "Could not read manifest %1$s", manifest);
        }
    }
}
 
Example 19
Source Project: javaide   Source File: ManifestMerger2.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static String getAndroidPrefix(Element xml) {
    String toolsPrefix = XmlUtils.lookupNamespacePrefix(
            xml, SdkConstants.ANDROID_URI, SdkConstants.ANDROID_NS_NAME, false);
    if (!toolsPrefix.equals(SdkConstants.ANDROID_NS_NAME) && xml.getOwnerDocument()
            .getDocumentElement().getAttribute("xmlns:" + toolsPrefix) == null) {
        // this is weird, the document is using "android" prefix but it's not bound
        // to our namespace. Add the proper xmlns declaration.
        xml.setAttribute("xmlns:" + toolsPrefix, SdkConstants.ANDROID_URI);
    }
    return toolsPrefix;
}
 
Example 20
Source Project: javaide   Source File: ManifestMerger.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Performs the merge operation in-place in the given DOM.
 * <p/>
 * This does NOT stop on errors, in an attempt to accumulate as much
 * info as possible to return to the user.
 * <p/>
 * The method might modify the input XML documents in-place for its own processing.
 *
 * @param mainDoc The document to merge into. Will be modified in-place.
 * @param libraryDocs The library manifest documents to merge in. Must not be null.
 *                    These will be modified in-place.
 * @return True on success, false if any error occurred (printed to the {@link IMergerLog}).
 */
public boolean process(@NonNull Document mainDoc, @NonNull Document... libraryDocs) {

    boolean success = true;
    mMainDoc = mainDoc;
    MergerXmlUtils.decorateDocument(mainDoc, IMergerLog.MAIN_MANIFEST);

    String prefix = XmlUtils.lookupNamespacePrefix(mainDoc, SdkConstants.NS_RESOURCES);
    mXPath = AndroidXPathFactory.newXPath(prefix);

    expandFqcns(mainDoc);
    for (Document libDoc : libraryDocs) {
        MergerXmlUtils.decorateDocument(libDoc, IMergerLog.LIBRARY);
        if (!mergeLibDoc(cleanupToolsAttributes(libDoc))) {
            success = false;
        }
    }

    cleanupToolsAttributes(mainDoc);

    if (mExtractPackagePrefix) {
        extractFqcns(mainDoc);
    }

    if (mInsertSourceMarkers) {
        insertSourceMarkers(mainDoc);
    }

    mXPath = null;
    mMainDoc = null;
    return success;
}
 
Example 21
Source Project: javaide   Source File: XmlPrettyPrinter.java    License: GNU General Public License v3.0 5 votes vote down vote up
private boolean keepElementAsSingleLine(int depth, Element element) {
    if (depth == 0) {
        return false;
    }

    return isSingleLineTag(element)
            || (mStyle == XmlFormatStyle.RESOURCE
            && !XmlUtils.hasElementChildren(element));
}
 
Example 22
Source Project: NBANDROID-V2   Source File: FmEscapeXmlTextMethod.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TemplateModel exec(List args) throws TemplateModelException {
  if (args.size() != 1) {
    throw new TemplateModelException("Wrong arguments");
  }
  String string = ((TemplateScalarModel)args.get(0)).getAsString();
  return new SimpleScalar(XmlUtils.toXmlTextValue(string));
}
 
Example 23
Source Project: NBANDROID-V2   Source File: FmEscapeXmlAttributeMethod.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() != 1) {
        throw new TemplateModelException("Wrong arguments");
    }
    try {
        String string = ((TemplateScalarModel) args.get(0)).getAsString();
        return new SimpleScalar(XmlUtils.toXmlAttributeValue(string));
    } catch (Exception exception) {
        throw new TemplateModelException("Wrong arguments");
    }
}
 
Example 24
Source Project: bazel   Source File: ProtoResourceUsageAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void acceptOpaqueFileType(Path path) {
  try {
    String pathString = path.toString();
    if (pathString.endsWith(".js")) {
      model.tokenizeJs(
          declaredResource, new String(Files.readAllBytes(path), StandardCharsets.UTF_8));
    } else if (pathString.endsWith(".css")) {
      model.tokenizeCss(
          declaredResource, new String(Files.readAllBytes(path), StandardCharsets.UTF_8));
    } else if (pathString.endsWith(".html")) {
      model.tokenizeHtml(
          declaredResource, new String(Files.readAllBytes(path), StandardCharsets.UTF_8));
    } else if (pathString.endsWith(".xml")) {
      // Force parsing of raw xml files to get any missing keep attributes.
      // The tool keep and discard attributes are held in raw files.
      // There is already processing to handle this, but there has been flakiness.
      // This step is to ensure as much stability as possible until the flakiness can be
      // diagnosed.
      model.recordResourceReferences(
          ResourceFolderType.getTypeByName(declaredResource.type.getName()),
          XmlUtils.parseDocumentSilently(
              new String(Files.readAllBytes(path), StandardCharsets.UTF_8), true),
          declaredResource);

    } else {
      // Path is a reference to the apk zip -- unpack it before getting a file reference.
      model.tokenizeUnknownBinary(
          declaredResource,
          Files.copy(
                  path,
                  Files.createTempFile("binary-resource", null),
                  StandardCopyOption.REPLACE_EXISTING)
              .toFile());
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 25
Source Project: bazel   Source File: ResourceUsageAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
/** Deletes unused resources from value XML files. */
private void rewriteXml(Set<File> rewrite, Map<File, String> rewritten, Set<Resource> deleted)
    throws IOException, ParserConfigurationException, SAXException {
  // Delete value resources: Must rewrite the XML files
  for (File file : rewrite) {
    String xml = Files.toString(file, UTF_8);
    Document document = XmlUtils.parseDocument(xml, true);
    Element root = document.getDocumentElement();
    if (root != null && TAG_RESOURCES.equals(root.getTagName())) {
      List<Resource> removed = Lists.newArrayList();
      stripUnused(root, removed);
      deleted.addAll(removed);
      logger.fine(
          String.format(
              "Removed %d unused resources from %s:\n  %s",
              removed.size(),
              file,
              Joiner.on(", ")
                  .join(
                      Lists.transform(
                          removed,
                          new Function<Resource, String>() {
                            @Override
                            public String apply(Resource resource) {
                              return resource.getUrl();
                            }
                          }))));
      String formatted = XmlPrettyPrinter.prettyPrint(document, xml.endsWith("\n"));
      rewritten.put(file, formatted);
    }
  }
}
 
Example 26
Source Project: bazel   Source File: ResourceUsageAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
/** Remove public definitions of unused resources. */
private void trimPublicResources(
    File publicXml, Set<Resource> deleted, Map<File, String> rewritten)
    throws IOException, ParserConfigurationException, SAXException {
  if (publicXml.exists()) {
    String xml = rewritten.get(publicXml);
    if (xml == null) {
      xml = Files.toString(publicXml, UTF_8);
    }
    Document document = XmlUtils.parseDocument(xml, true);
    Element root = document.getDocumentElement();
    if (root != null && TAG_RESOURCES.equals(root.getTagName())) {
      NodeList children = root.getChildNodes();
      for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
          Element resourceElement = (Element) child;
          ResourceType type = ResourceType.getEnum(resourceElement.getAttribute(ATTR_TYPE));
          String name = resourceElement.getAttribute(ATTR_NAME);
          if (type != null && name != null) {
            Resource resource = model.getResource(type, name);
            if (resource != null && deleted.contains(resource)) {
              root.removeChild(child);
            }
          }
        }
      }
    }
    String formatted = XmlPrettyPrinter.prettyPrint(document, xml.endsWith("\n"));
    rewritten.put(publicXml, formatted);
  }
}
 
Example 27
Source Project: java-n-IDE-for-Android   Source File: ResourceItem.java    License: Apache License 2.0 4 votes vote down vote up
@NonNull
private static String getMarkupText(@NonNull NodeList children) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0, n = children.getLength(); i < n; i++) {
        Node child = children.item(i);

        short nodeType = child.getNodeType();

        switch (nodeType) {
            case Node.ELEMENT_NODE: {
                Element element = (Element) child;
                String tagName = element.getTagName();
                sb.append('<');
                sb.append(tagName);

                NamedNodeMap attributes = element.getAttributes();
                int attributeCount = attributes.getLength();
                if (attributeCount > 0) {
                    for (int j = 0; j < attributeCount; j++) {
                        Node attribute = attributes.item(j);
                        sb.append(' ');
                        sb.append(attribute.getNodeName());
                        sb.append('=').append('"');
                        XmlUtils.appendXmlAttributeValue(sb, attribute.getNodeValue());
                        sb.append('"');
                    }
                }
                sb.append('>');

                NodeList childNodes = child.getChildNodes();
                if (childNodes.getLength() > 0) {
                    sb.append(getMarkupText(childNodes));
                }

                sb.append('<');
                sb.append('/');
                sb.append(tagName);
                sb.append('>');

                break;
            }
            case Node.TEXT_NODE:
                sb.append(child.getNodeValue());
                break;
            case Node.CDATA_SECTION_NODE:
                sb.append(child.getNodeValue());
                break;
        }
    }

    return sb.toString();
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: XmlPrettyPrinter.java    License: Apache License 2.0 4 votes vote down vote up
private void printOpenElementTag(int depth, Node node) {
    Element element = (Element) node;
    if (newlineBeforeElementOpen(element, depth)) {
        mOut.append(mLineSeparator);
    }
    if (indentBeforeElementOpen(element, depth)) {
        indent(depth);
    }
    mOut.append('<').append(element.getTagName());

    NamedNodeMap attributes = element.getAttributes();
    int attributeCount = attributes.getLength();
    if (attributeCount > 0) {
        // Sort the attributes
        List<Attr> attributeList = new ArrayList<Attr>();
        for (int i = 0; i < attributeCount; i++) {
            attributeList.add((Attr) attributes.item(i));
        }
        Comparator<Attr> comparator = mPrefs.getAttributeComparator();
        if (comparator != null) {
            Collections.sort(attributeList, comparator);
        }

        // Put the single attribute on the same line as the element tag?
        boolean singleLine = mPrefs.oneAttributeOnFirstLine && attributeCount == 1
                // In resource files we always put all the attributes (which is
                // usually just zero, one or two) on the same line
                || mStyle == XmlFormatStyle.RESOURCE;

        // We also place the namespace declaration on the same line as the root element,
        // but this doesn't also imply singleLine handling; subsequent attributes end up
        // on their own lines
        boolean indentNextAttribute;
        if (singleLine || (depth == 0 && XMLNS.equals(attributeList.get(0).getPrefix()))) {
            mOut.append(' ');
            indentNextAttribute = false;
        } else {
            mOut.append(mLineSeparator);
            indentNextAttribute = true;
        }

        Attr last = attributeList.get(attributeCount - 1);
        for (Attr attribute : attributeList) {
            if (indentNextAttribute) {
                indent(depth + 1);
            }
            mOut.append(attribute.getName());
            mOut.append('=').append('"');
            XmlUtils.appendXmlAttributeValue(mOut, attribute.getValue());
            mOut.append('"');

            // Don't add a newline at the last attribute line; the > should
            // immediately follow the last attribute
            if (attribute != last) {
                mOut.append(singleLine ? " " : mLineSeparator); //$NON-NLS-1$
                indentNextAttribute = !singleLine;
            }
        }
    }

    boolean isClosed = isEmptyTag(element);

    // Add a space before the > or /> ? In resource files, only do this when closing the
    // element
    if (mPrefs.spaceBeforeClose && (mStyle != XmlFormatStyle.RESOURCE || isClosed)
            // in <selector> files etc still treat the <item> entries as in resource files
            && !TAG_ITEM.equals(element.getTagName())
            && (isClosed || element.getAttributes().getLength() > 0)) {
        mOut.append(' ');
    }

    if (isClosed) {
        mOut.append('/');
    }

    mOut.append('>');

    if (newlineAfterElementOpen(element, depth, isClosed)) {
        mOut.append(mLineSeparator);
    }
}
 
Example 29
Source Project: java-n-IDE-for-Android   Source File: XmlPrettyPrinter.java    License: Apache License 2.0 4 votes vote down vote up
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file,
        boolean stdout) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                formatFile(prefs, child, stdout);
            }
        }
    } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) {
        XmlFormatStyle style = null;
        if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) {
            style = XmlFormatStyle.MANIFEST;
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                String parentName = parent.getName();
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType == ResourceFolderType.LAYOUT) {
                    style = XmlFormatStyle.LAYOUT;
                } else if (folderType == ResourceFolderType.VALUES) {
                    style = XmlFormatStyle.RESOURCE;
                }
            }
        }

        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document == null) {
                System.err.println("Could not parse " + file);
                System.exit(1);
                return;
            }

            if (style == null) {
                style = XmlFormatStyle.get(document);
            }
            boolean endWithNewline = xml.endsWith("\n");
            int firstNewLine = xml.indexOf('\n');
            String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ?
                    "\r\n" : "\n";
            String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style,
                    lineSeparator, endWithNewline);
            if (stdout) {
                System.out.println(formatted);
            } else {
                Files.write(formatted, file, Charsets.UTF_8);
            }
        } catch (IOException e) {
            System.err.println("Could not read " + file);
            System.exit(1);
        }
    }
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: ManifestMerger.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Performs the merge operation in-place in the given DOM.
 * <p/>
 * This does NOT stop on errors, in an attempt to accumulate as much
 * info as possible to return to the user.
 * <p/>
 * The method might modify the input XML document in-place for its own processing.
 *
 * @param mainDoc The document to merge into. Will be modified in-place.
 * @param libraryFiles The library manifest paths to read. Must not be null.
 *                     These will be modified in-place.
 * @param injectAttributes A map of attributes to inject in the form [pseudo-xpath] => value.
 *   The key is "/manifest/elements...|attribute-ns-uri attribute-local-name",
 *   for example "/manifest/uses-sdk|http://schemas.android.com/apk/res/android minSdkVersion".
 *   (note the space separator between the attribute URI and its local name.)
 *   The elements will be created if they don't exists. Existing attributes will be modified.
 *   The replacement is done on the main document <em>before</em> merging.
 * @param packageOverride an optional package override. This only affects the package attribute,
 *   all components (activities, receivers, etc...) are not affected by this.
 * @return True on success, false if any error occurred (printed to the {@link IMergerLog}).
 */
public boolean process(
        Document mainDoc,
        File[] libraryFiles,
        Map<String, String> injectAttributes,
        String packageOverride) {

    boolean success = true;
    mMainDoc = mainDoc;
    MergerXmlUtils.decorateDocument(mainDoc, IMergerLog.MAIN_MANIFEST);
    MergerXmlUtils.injectAttributes(mainDoc, injectAttributes, mLog);

    String prefix = XmlUtils.lookupNamespacePrefix(mainDoc, SdkConstants.NS_RESOURCES);
    mXPath = AndroidXPathFactory.newXPath(prefix);

    expandFqcns(mainDoc);
    for (File libFile : libraryFiles) {
        Document libDoc = MergerXmlUtils.parseDocument(libFile, mLog, this);
        if (libDoc == null || !mergeLibDoc(cleanupToolsAttributes(libDoc))) {
            success = false;
        }
    }

    if (packageOverride != null) {
        MergerXmlUtils.injectAttributes(mainDoc,
                Collections.singletonMap("/manifest| package", packageOverride),
                mLog);
    }

    cleanupToolsAttributes(mainDoc);

    if (mExtractPackagePrefix) {
        extractFqcns(mainDoc);
    }

    if (mInsertSourceMarkers) {
        insertSourceMarkers(mainDoc);
    }

    mXPath = null;
    mMainDoc = null;
    return success;
}