FileDocCategorySizeDatePackage
XmlStringFileHelper.javaAPI DocAndroid 1.5 API4661Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.adt.refactorings.extractstring

XmlStringFileHelper

public class XmlStringFileHelper extends Object

Fields Summary
private HashMap
mResIdCache
A 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
mXPath
An instance of XPath, created lazily on demand.
Constructors Summary
public XmlStringFileHelper()

    
Methods Summary
private java.util.HashSetgetResIdsForFile(org.eclipse.core.resources.IProject project, java.lang.String xmlFileWsPath)
Extract all the defined string IDs from a given file using XPath.

param
project The project contain the XML file.
param
xmlFileWsPath The project path of the file to parse. It may not exist.
return
The set of all string IDs defined in the file. The returned set is always non null. It is empty if the file does not exist.

        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 booleanisResIdDuplicate(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.

param
project The project contain the XML file.
param
xmlFileWsPath The project path of the XML file, e.g. "/res/values/strings.xml". The given file may or may not exist.
param
stringId The string ID to find.
return
True if such a string ID is already defined.

        // 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);