Java Code Examples for com.android.utils.XmlUtils#parseDocument()
The following examples show how to use
com.android.utils.XmlUtils#parseDocument() .
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: MergerXmlUtils.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * 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 2
Source File: Extractor.java From javaide with GNU General Public License v3.0 | 6 votes |
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 3
Source File: MergerXmlUtils.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * 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 File: ResourceUsageAnalyzer.java From bazel with Apache License 2.0 | 6 votes |
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 5
Source File: ResourceUsageAnalyzer.java From javaide with GNU General Public License v3.0 | 5 votes |
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 6
Source File: Extractor.java From javaide with GNU General Public License v3.0 | 5 votes |
@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 7
Source File: ResourceUsageAnalyzer.java From bazel with Apache License 2.0 | 5 votes |
/** 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 8
Source File: ResourceUsageAnalyzer.java From bazel with Apache License 2.0 | 5 votes |
/** 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 9
Source File: ResourceUsageAnalyzer.java From javaide with GNU General Public License v3.0 | 4 votes |
private void recordManifestUsages(File manifest) throws IOException, ParserConfigurationException, SAXException { String xml = Files.toString(manifest, UTF_8); Document document = XmlUtils.parseDocument(xml, true); recordManifestUsages(document.getDocumentElement()); }
Example 10
Source File: ResourceUsageAnalyzer.java From bazel with Apache License 2.0 | 4 votes |
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); }