themeinstaller/source/src/com/nokia/tools/themeinstaller/cssparser/CSSParser.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:  This class parses CSS file to DOM document
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.cssparser;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.FileReader;
       
    23 import java.io.IOException;
       
    24 import java.io.Reader;
       
    25 
       
    26 import org.w3c.css.sac.InputSource;
       
    27 import org.w3c.css.sac.Parser;
       
    28 import org.w3c.css.sac.helpers.ParserFactory;
       
    29 import org.w3c.dom.Document;
       
    30 
       
    31 import com.nokia.tools.themeinstaller.logger.LogWriter;
       
    32 import com.nokia.tools.themeinstaller.odtconverter.ParseOperation;
       
    33 import com.nokia.tools.themeinstaller.odtconverter.IParseOperationListener;
       
    34 import com.nokia.tools.themeinstaller.odtconverter.ODTException;
       
    35 
       
    36 /**
       
    37  * CSSParser parses the CSS File and uses CSSHandler to apply the
       
    38  * parsed style rules to DOM Document.
       
    39  */
       
    40 public class CSSParser extends ParseOperation implements Runnable
       
    41     {
       
    42 
       
    43     /** The Constant CSS_PARSER_SYSTEM_VALUE. */
       
    44     private static final String CSS_PARSER_SYSTEM_VALUE = "org.w3c.css.sac.parser";
       
    45 
       
    46     /** The Constant CSS_PARSER_SYSTEM_KEY. */
       
    47     private static final String CSS_PARSER_SYSTEM_KEY = "org.apache.batik.css.parser.Parser";
       
    48 
       
    49     /** CSS file name */
       
    50     private String iFileName;
       
    51 
       
    52     /** The CSS handler. */
       
    53     private CSSHandler iCSSHandler;
       
    54 
       
    55     /** The CSS parser. */
       
    56     private Parser iParser;
       
    57 
       
    58     /**
       
    59      * Instantiates a new CSS parser.
       
    60      *
       
    61      * @param aFileName The CSS file name
       
    62      */
       
    63     public CSSParser( String aFileName )
       
    64         {
       
    65         iFileName = aFileName;
       
    66         ParserFactory factory = new ParserFactory();
       
    67         iCSSHandler = new CSSHandler();
       
    68 
       
    69         // Store the value-key pair of the used parser in the System
       
    70         // environment(JVM)
       
    71         System.setProperty( CSS_PARSER_SYSTEM_VALUE, CSS_PARSER_SYSTEM_KEY );
       
    72         try
       
    73             {
       
    74             iParser = factory.makeParser();
       
    75             }
       
    76         catch ( Exception e )
       
    77             {
       
    78             throw new IllegalStateException( "Could not load CSS parser" );
       
    79             }
       
    80         }
       
    81 
       
    82     public void setDocument( Document aDocument )
       
    83         {
       
    84         iDOMDocument = aDocument;
       
    85         }
       
    86 
       
    87     /**
       
    88      * Run the CSS parse operation.
       
    89      */
       
    90     public void run()
       
    91         {
       
    92         int error = IParseOperationListener.OPERATION_SUCCESSFUL;
       
    93         String reason = "";
       
    94 
       
    95         try
       
    96             {
       
    97             iCSSHandler.setDocument( iDOMDocument );
       
    98 
       
    99             File mainCSS = new File( iFileName );
       
   100             Reader r = new FileReader( mainCSS );
       
   101             // Tell CSS handler the directory for relative import paths,
       
   102             // that is, the path of the Main CSS file
       
   103             iCSSHandler.setImportDirectory( mainCSS.getParent() );
       
   104             InputSource is = new InputSource( r );
       
   105             iParser.setDocumentHandler( iCSSHandler );
       
   106             LogWriter.getInstance().logInfo(
       
   107                     this.getClass().getSimpleName() + ": Parsing CSS "
       
   108                             + iFileName );
       
   109             iParser.parseStyleSheet( is );
       
   110             iDOMDocument = iCSSHandler.getDocument();
       
   111             }
       
   112         catch ( Exception e )
       
   113             {
       
   114             reason = e.getMessage();
       
   115             error = IParseOperationListener.CSS_PARSER_ERROR;
       
   116             }
       
   117 
       
   118         super.operationCompleted( error, reason );
       
   119         }
       
   120 
       
   121     /**
       
   122      * Starts CSS parsing in a new thread.
       
   123      */
       
   124     public void parse() throws IOException, ODTException
       
   125         {
       
   126         Thread thread = new Thread( this );
       
   127         thread.start();
       
   128         }
       
   129 
       
   130     }