Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
OW2 Util Copyright (C) 2008 Contact: easybeans@objectweb.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------- $Id: IFXmlHelperImpl.java --------------------------------------------------------------------------
 
 
 package org.ow2.util.plan.deploy.deployable.internal.xmlhelper;
 
 import java.io.File;
 
 
 
 public class XmlHelper {

    
The parser that will be used for validation with namespaces.
 
     private SAXParser namespaceAwareParser = null;

    
The parser that will be used for parsing when namespaces are not considered.
 
     private SAXParser namespaceUnawareParser = null;

    
Default constructor for XmlHelperImpl.

Throws:
javax.xml.parsers.ParserConfigurationException in case of SAX misconfiguration.
org.xml.sax.SAXException is SAX errors occured.
 
     public XmlHelper() throws ParserConfigurationExceptionSAXException {
         SAXParserFactory factory = SAXParserFactory.newInstance();;
          = factory.newSAXParser();
         factory.setNamespaceAware(true);
          = factory.newSAXParser();
     }

    
 
     public synchronized boolean isWellFormedXml(final File filethrows IllegalArgumentExceptionIOException {
         try {
             .parse(filenew DefaultHandler());
             return true;
         } catch (SAXException e) {
             return false;
         }
     }

    
 
     public synchronized boolean isLikelyXml(final File filethrows IllegalArgumentExceptionIOException {
         try {
             .parse(filenew CheckFirstElementHandler());
             return false;
         } catch (GetOutException e) {
             return true;
         } catch (SAXException e) {
             return false;
         }
 
     }

    
 
     public synchronized String getRootElementNameSpace(final File filethrows IOExceptionSAXException {
         try {
             .parse(filenew CheckFirstElementHandler());
             return null// else the compiler is unhappy
         } catch (GetOutException e) {
             return e.getNameSpace();
        }
    }

    
Exception used to cancel the parsing once the first element of an XML file has been read.

Author(s):
mleduque
    static class GetOutException extends SAXException {
        private static final long serialVersionUID = 27568468415776L;

        
The namespace of the first element of the file.
        private String nameSpace = null;

        
the first element name.
        private String elementName = null;

        

Parameters:
nameSpace the name space.
elementName the element name.
        public GetOutException(final String nameSpacefinal String localName) {
            this. = nameSpace;
            this. = localName;
        }

        
Sets the namespace.

Parameters:
nameSpace the new namespace.
        public void setNameSpace(final String nameSpace) {
            this. = nameSpace;
        }

        
Gets the namespace.

Returns:
the namespace.
        public String getNameSpace() {
            return ;
        }

        
Sets the element name.

Parameters:
elementName the new element name.
        public void setElementName(final String localName) {
            this. = localName;
        }

        
Gets the element name.

Returns:
the element name.
        public String getElementName() {
            return this.;
        }
    }

    
SAX handler for the xml helper.

Author(s):
mleduque
    static class CheckFirstElementHandler extends DefaultHandler {

        
        @Override
        public void startElement(final String urifinal String localNamefinal String qName,
                final org.xml.sax.Attributes attributesthrows SAXException {
                /* We've found the root element
                 * Abort the parsing. */
                throw new GetOutException(urilocalName);
        }
    }
New to GrepCode? Check out our FAQ X