themeinstaller/source/src/com/nokia/tools/themeinstaller/xmlparser/XMLParser.java
branchRCL_3
changeset 17 fe49e33862e2
parent 16 b685c59de105
child 18 04b7640f6fb5
equal deleted inserted replaced
16:b685c59de105 17:fe49e33862e2
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Parses XML file to DOM Document
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.xmlparser;
       
    20 
       
    21 import java.io.IOException;
       
    22 
       
    23 import javax.xml.parsers.DocumentBuilder;
       
    24 import javax.xml.parsers.DocumentBuilderFactory;
       
    25 import javax.xml.parsers.ParserConfigurationException;
       
    26 
       
    27 import org.xml.sax.SAXException;
       
    28 
       
    29 import com.nokia.tools.themeinstaller.logger.LogWriter;
       
    30 import com.nokia.tools.themeinstaller.odtconverter.IParseOperationListener;
       
    31 import com.nokia.tools.themeinstaller.odtconverter.ParseOperation;
       
    32 
       
    33 /**
       
    34  * Parses XML File to DOM Document.
       
    35  */
       
    36 public class XMLParser extends ParseOperation implements Runnable
       
    37     {
       
    38 
       
    39     // CONSTANTS
       
    40     // Document Builder Factory and Parser configuration
       
    41     private static final String DBF_KEY =
       
    42             "javax.xml.parsers.DocumentBuilderFactory";
       
    43     private static final String DBF_VALUE =
       
    44             "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl";
       
    45     private static final String PARSER_CONFIG_KEY =
       
    46             "org.apache.xerces.xni.parser.XMLParserConfiguration";
       
    47     private static final String PARSER_CONFIG_VALUE =
       
    48             "org.apache.xerces.parsers.XIncludeAwareParserConfiguration";
       
    49 
       
    50     /** The Document Builder. */
       
    51     private DocumentBuilder iDocumentBuilder;
       
    52 
       
    53     /** XML file name */
       
    54     private String iFileName;
       
    55 
       
    56     /**
       
    57      * Static block.
       
    58      */
       
    59     static
       
    60         {
       
    61         // Configure the Document Builder Factory
       
    62         System.setProperty( DBF_KEY, DBF_VALUE );
       
    63 
       
    64         // Configure the XML Parser Configuration
       
    65         System.setProperty( PARSER_CONFIG_KEY, PARSER_CONFIG_VALUE );
       
    66         }
       
    67 
       
    68     /**
       
    69      * Instantiates a new XML parser without an external DTD file.
       
    70      *
       
    71      * @param aFileName the XML file name
       
    72      */
       
    73     public XMLParser( String aFileName )
       
    74         {
       
    75         this( aFileName, null );
       
    76         }
       
    77 
       
    78     /**
       
    79      * Instantiates a new XML parser. External DTD file can be used instead of
       
    80      * the one defined in the DOCTYPE.
       
    81      *
       
    82      * @param aFileName the XML file name
       
    83      * @param aExtDTD the DTD file name. If an external DTD is not specified,
       
    84      * Expand Entity References feature is disabled. This causes the
       
    85      * localisation to be left untouched.
       
    86      */
       
    87     public XMLParser( String aFileName, String aExtDTD )
       
    88         {
       
    89         super();
       
    90         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       
    91         dbf.setNamespaceAware( true );
       
    92 
       
    93         try
       
    94             {
       
    95             if( aExtDTD == null )
       
    96                 {
       
    97                 // Disable expanding of entity references to leave localisation
       
    98                 // untouched
       
    99                 dbf.setExpandEntityReferences( false );
       
   100 
       
   101                 // Disable the loading of external DTD file because expanding
       
   102                 // entity references (the localisation) is disabled anyway
       
   103                 dbf.setFeature(
       
   104                         "http://apache.org/xml/features/nonvalidating/load-external-dtd",
       
   105                         false );
       
   106                 }
       
   107 
       
   108             // Enable XIncludes in XML parsing
       
   109             dbf.setXIncludeAware( true );
       
   110 
       
   111             // Disable the adding of XInclude parsing related attributes:
       
   112             // xml:base and xml:lang
       
   113             dbf.setFeature(
       
   114                     "http://apache.org/xml/features/xinclude/fixup-base-uris",
       
   115                     false );
       
   116             dbf.setFeature(
       
   117                     "http://apache.org/xml/features/xinclude/fixup-language",
       
   118                     false );
       
   119 
       
   120             // Instantiate the Document Builder for parsing
       
   121             iDocumentBuilder = dbf.newDocumentBuilder();
       
   122 
       
   123             if( aExtDTD != null )
       
   124                 {
       
   125                 // Use entity resolver for using an external DTD file
       
   126                 iDocumentBuilder.setEntityResolver(
       
   127                         new DTDEntityResolver( aExtDTD ) );
       
   128                 }
       
   129             }
       
   130         catch ( ParserConfigurationException e )
       
   131             {
       
   132             throw new IllegalStateException( "Could not load XML parser: " + e.toString() );
       
   133             }
       
   134 
       
   135         iFileName = aFileName;
       
   136         }
       
   137 
       
   138     /**
       
   139      * Run the XML parse operation.
       
   140      */
       
   141     public void run()
       
   142         {
       
   143         // Messages for operation listener
       
   144         int error = IParseOperationListener.OPERATION_SUCCESSFUL;
       
   145         String reason = null;
       
   146         try
       
   147             {
       
   148             // Actual parsing
       
   149             LogWriter.getInstance().logInfo(
       
   150                     this.getClass().getSimpleName() + ": Parsing " + iFileName );
       
   151             iDOMDocument = iDocumentBuilder.parse( iFileName );
       
   152             }
       
   153         catch ( SAXException e )
       
   154             {
       
   155             reason = e.getMessage();
       
   156             error = IParseOperationListener.SAX_PARSE_ERROR;
       
   157             }
       
   158         catch ( IOException e )
       
   159             {
       
   160             reason = e.getMessage();
       
   161             error = IParseOperationListener.IO_ERROR;
       
   162             }
       
   163 
       
   164         super.operationCompleted( error, reason );
       
   165         }
       
   166 
       
   167     /**
       
   168      * Starts XML parsing in a new thread.
       
   169      */
       
   170     public void parse()
       
   171         {
       
   172         Thread thread = new Thread( this );
       
   173         thread.start();
       
   174         }
       
   175     }