XmlStringFileHelperpublic class XmlStringFileHelper extends Object
Fields Summary |
---|
private HashMap | mResIdCacheA temporary cache of R.string IDs defined by a given xml file. The key is the
project path of the file, the data is a set of known string Ids for that file. | private XPath | mXPathAn instance of XPath, created lazily on demand. |
Constructors Summary |
---|
public XmlStringFileHelper()
|
Methods Summary |
---|
private java.util.HashSet | getResIdsForFile(org.eclipse.core.resources.IProject project, java.lang.String xmlFileWsPath)Extract all the defined string IDs from a given file using XPath.
HashSet<String> ids = new HashSet<String>();
if (mXPath == null) {
mXPath = AndroidXPathFactory.newXPath();
}
// Access the project that contains the resource that contains the compilation unit
IResource resource = project.getFile(xmlFileWsPath);
if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
InputSource source;
try {
source = new InputSource(((IFile) resource).getContents());
// We want all the IDs in an XML structure like this:
// <resources>
// <string name="ID">something</string>
// </resources>
String xpathExpr = "/resources/string/@name"; //$NON-NLS-1$
Object result = mXPath.evaluate(xpathExpr, source, XPathConstants.NODESET);
if (result instanceof NodeList) {
NodeList list = (NodeList) result;
for (int n = list.getLength() - 1; n >= 0; n--) {
String id = list.item(n).getNodeValue();
ids.add(id);
}
}
} catch (CoreException e1) {
// IFile.getContents failed. Ignore.
} catch (XPathExpressionException e) {
// mXPath.evaluate failed. Ignore.
}
}
return ids;
| public boolean | isResIdDuplicate(org.eclipse.core.resources.IProject project, java.lang.String xmlFileWsPath, java.lang.String stringId)Utility method used by the wizard to check whether the given string ID is already
defined in the XML file which path is given.
// This is going to be called many times on the same file.
// Build a cache of the existing IDs for a given file.
if (mResIdCache == null) {
mResIdCache = new HashMap<String, HashSet<String>>();
}
HashSet<String> cache = mResIdCache.get(xmlFileWsPath);
if (cache == null) {
cache = getResIdsForFile(project, xmlFileWsPath);
mResIdCache.put(xmlFileWsPath, cache);
}
return cache.contains(stringId);
|
|