com.android.utils.XmlUtils Java Examples

The following examples show how to use com.android.utils.XmlUtils. 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: XmlPrettyPrinter.java    From java-n-IDE-for-Android with 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 File: XmlPrettyPrinter.java    From javaide with 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 #3
Source File: AndroidManifestParser.java    From NBANDROID-V2 with 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 #4
Source File: ViewTypeDetector.java    From javaide with 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 #5
Source File: MergerXmlUtils.java    From javaide with 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 #6
Source File: OrphanXmlElement.java    From javaide with 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 File: ManifestMerger2.java    From javaide with 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 #8
Source File: Extractor.java    From javaide with 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 #9
Source File: MergerXmlUtils.java    From java-n-IDE-for-Android with 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 #10
Source File: OrphanXmlElement.java    From java-n-IDE-for-Android with 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 #11
Source File: ResourceUsageAnalyzer.java    From bazel with 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 File: ProtoResourceUsageAnalyzer.java    From bazel with 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 #13
Source File: FmEscapeXmlTextMethod.java    From NBANDROID-V2 with 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 #14
Source File: XmlPrettyPrinter.java    From javaide with 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 #15
Source File: FmEscapeXmlAttributeMethod.java    From NBANDROID-V2 with 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 #16
Source File: XmlPrettyPrinter.java    From java-n-IDE-for-Android with 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 #17
Source File: ManifestMerger.java    From javaide with 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 #18
Source File: ManifestMerger.java    From java-n-IDE-for-Android with 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 #19
Source File: ManifestMerger2.java    From java-n-IDE-for-Android with 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 #20
Source File: ManifestMerger2.java    From javaide with 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 #21
Source File: ManifestMerger2.java    From java-n-IDE-for-Android with 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 #22
Source File: LintGradleProject.java    From javaide with 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 #23
Source File: Extractor.java    From javaide with 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 #24
Source File: ResourceUsageAnalyzer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
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 #25
Source File: ResourceUsageAnalyzer.java    From bazel with 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 File: ResourceUsageAnalyzer.java    From bazel with 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 File: StringFileAdapter.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
@Override
public File unmarshal(String s) throws Exception {
    String unescapedString = XmlUtils.fromXmlAttributeValue(s);
    return new File(toSystemDependentName(unescapedString, File.separatorChar));
}
 
Example #28
Source File: ResourceItem.java    From java-n-IDE-for-Android with 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 #29
Source File: ResourceUsageAnalyzer.java    From bazel with Apache License 2.0 4 votes vote down vote up
private void recordManifestUsages(Path manifest)
    throws IOException, ParserConfigurationException, SAXException {
  String xml = Files.toString(manifest.toFile(), UTF_8);
  Document document = XmlUtils.parseDocument(xml, true);
  model.visitXmlDocument(manifest.toFile(), null, document);
}
 
Example #30
Source File: ResValueGenerator.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generates the resource files
 */
public void generate() throws IOException, ParserConfigurationException {
    File pkgFolder = getFolderPath();
    if (!pkgFolder.isDirectory()) {
        if (!pkgFolder.mkdirs()) {
            throw new RuntimeException("Failed to create " + pkgFolder.getAbsolutePath());
        }
    }

    File resFile = new File(pkgFolder, RES_VALUE_FILENAME_XML);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    factory.setIgnoringComments(true);
    DocumentBuilder builder;

    builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();

    Node rootNode = document.createElement(TAG_RESOURCES);
    document.appendChild(rootNode);

    rootNode.appendChild(document.createTextNode("\n"));
    rootNode.appendChild(document.createComment("Automatically generated file. DO NOT MODIFY"));
    rootNode.appendChild(document.createTextNode("\n\n"));

    for (Object item : mItems) {
        if (item instanceof ClassField) {
            ClassField field = (ClassField)item;

            ResourceType type = ResourceType.getEnum(field.getType());
            boolean hasResourceTag = (type != null && RESOURCES_WITH_TAGS.contains(type));

            Node itemNode = document.createElement(hasResourceTag ? field.getType() : TAG_ITEM);
            Attr nameAttr = document.createAttribute(ATTR_NAME);

            nameAttr.setValue(field.getName());
            itemNode.getAttributes().setNamedItem(nameAttr);

            if (!hasResourceTag) {
                Attr typeAttr = document.createAttribute(ATTR_TYPE);
                typeAttr.setValue(field.getType());
                itemNode.getAttributes().setNamedItem(typeAttr);
            }

            if (type == ResourceType.STRING) {
                Attr translatable = document.createAttribute(ATTR_TRANSLATABLE);
                translatable.setValue(VALUE_FALSE);
                itemNode.getAttributes().setNamedItem(translatable);
            }

            if (!field.getValue().isEmpty()) {
                itemNode.appendChild(document.createTextNode(field.getValue()));
            }

            rootNode.appendChild(itemNode);
        } else if (item instanceof String) {
            rootNode.appendChild(document.createTextNode("\n"));
            rootNode.appendChild(document.createComment((String) item));
            rootNode.appendChild(document.createTextNode("\n"));
        }
    }

    String content;
    try {
        content = XmlPrettyPrinter.prettyPrint(document, true);
    } catch (Throwable t) {
        content = XmlUtils.toXml(document);
    }

    Files.write(content, resFile, Charsets.UTF_8);
}