FileDocCategorySizeDatePackage
FirstElementParser.javaAPI DocAndroid 1.5 API5260Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors

FirstElementParser

public final class FirstElementParser extends Object
Quickly parses a (potential) XML file to extract its first element (i.e. the root element) and namespace, if any.

This is used to determine if a file is an XML document that the XmlEditor can process.

TODO use this to remove the hardcoded "android" namespace prefix limitation.

Fields Summary
private static SAXParserFactory
sSaxfactory
Constructors Summary
private FirstElementParser()
Private constructor. Use the static parse() method instead.

        // pass
    
Methods Summary
public static com.android.ide.eclipse.editors.FirstElementParser$Resultparse(java.lang.String osFilename, java.lang.String xmlnsUri)
Parses the given filename.

param
osFilename The file to parse.
param
xmlnsUri An optional URL of which we want to know the prefix.
return
The element details found or null if not found.

        if (sSaxfactory == null) {
            // TODO just create a single factory in CommonPlugin and reuse it
            sSaxfactory = SAXParserFactory.newInstance();
            sSaxfactory.setNamespaceAware(true);
        }

        Result result = new Result();
        if (xmlnsUri != null && xmlnsUri.length() > 0) {
            result.setXmlnsUri(xmlnsUri);
        }

        try {
            SAXParser parser = sSaxfactory.newSAXParser();
            XmlHandler handler = new XmlHandler(result);
            parser.parse(new InputSource(new FileReader(osFilename)), handler);

        } catch(ResultFoundException e) {
            // XML handling was aborted because the required element was found.
            // Simply return the result.
            return result;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

        return null;