001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.IOException;
016: import java.io.InputStream;
017:
018: import javax.xml.parsers.FactoryConfigurationError;
019: import javax.xml.parsers.ParserConfigurationException;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.pde.internal.core.util.SAXParserWrapper;
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.helpers.DefaultHandler;
027:
028: /**
029: * XMLContentTypeHandler
030: *
031: */
032: public class XMLRootElementMatcher {
033: public static boolean fileMatchesElement(IFile file, String element) {
034: try {
035: return matchFile(file.getContents(), element);
036: } catch (CoreException e) {
037: return false;
038: }
039: }
040:
041: public static boolean fileMatchesElement(File file, String element) {
042: try {
043: InputStream stream = new FileInputStream(file);
044: return matchFile(stream, element);
045: } catch (FileNotFoundException e) {
046: return false;
047: }
048: }
049:
050: private static boolean matchFile(InputStream stream, String element) {
051: XMLContentTypeHandler handler = new XMLContentTypeHandler();
052: try {
053: SAXParserWrapper parser = new SAXParserWrapper();
054: parser.parse(stream, handler);
055: } catch (ParserConfigurationException e) {
056: return false;
057: } catch (AbortParseException e) {
058: return handler.isRootType(element);
059: } catch (SAXException e) {
060: return false;
061: } catch (FactoryConfigurationError e) {
062: return false;
063: } catch (IOException e) {
064: return false;
065: }
066:
067: return handler.isRootType(element);
068: }
069:
070: /**
071: * AbortParseException
072: *
073: */
074: private static class AbortParseException extends SAXException {
075: private static final long serialVersionUID = 1L;
076:
077: public AbortParseException() {
078: super (
079: "Parsing operation forcibly aborted to save on performance time."); //$NON-NLS-1$
080: }
081: }
082:
083: private static class XMLContentTypeHandler extends DefaultHandler {
084: private String fRootElem;
085:
086: /**
087: *
088: */
089: private XMLContentTypeHandler() {
090: fRootElem = null;
091: }
092:
093: /* (non-Javadoc)
094: * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
095: */
096: public void startElement(String uri, String localName,
097: String qName, Attributes attributes)
098: throws SAXException {
099: fRootElem = qName;
100: // Only care about the root node
101: // Abort parsing to save on performance
102: throw new AbortParseException();
103: }
104:
105: /**
106: * @return
107: */
108: public boolean isRootType(String rootType) {
109: return fRootElem != null && fRootElem.equals(rootType);
110: }
111: }
112: }
|