themeinstaller/source/src/com/nokia/tools/themeinstaller/cssparser/CSSMatchMaker.java
branchRCL_3
changeset 18 04b7640f6fb5
parent 0 05da4621cfb2
equal deleted inserted replaced
17:fe49e33862e2 18:04b7640f6fb5
       
     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:  Compares W3C DOM Elements and CSS Selectors
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.cssparser;
       
    20 
       
    21 import java.util.Collection;
       
    22 import java.util.Iterator;
       
    23 
       
    24 import org.w3c.css.sac.AttributeCondition;
       
    25 import org.w3c.css.sac.CSSException;
       
    26 import org.w3c.css.sac.CombinatorCondition;
       
    27 import org.w3c.css.sac.Condition;
       
    28 import org.w3c.css.sac.ConditionalSelector;
       
    29 import org.w3c.css.sac.DescendantSelector;
       
    30 import org.w3c.css.sac.ElementSelector;
       
    31 import org.w3c.css.sac.Selector;
       
    32 import org.w3c.dom.Element;
       
    33 import org.w3c.dom.Node;
       
    34 
       
    35 /**
       
    36  * The Class CSSMatchMaker compares W3C DOM Elements and CSS Selectors. Match
       
    37  * method returns true if CSS Selector has style rules for the given DOM Element
       
    38  */
       
    39 public class CSSMatchMaker
       
    40     {
       
    41 
       
    42     /** The Constant STRING_CLASS. */
       
    43     private static final String STRING_CLASS = "class";
       
    44 
       
    45     /** The Constant STRING_ID. */
       
    46     private static final String STRING_ID = "id";
       
    47 
       
    48     /** The Constant STRING_SPACE. */
       
    49     private static final String STRING_SPACE = " ";
       
    50 
       
    51     /** The pseudo class resolver. */
       
    52     private PseudoClassResolver iPseudoClassResolver;
       
    53 
       
    54     /**
       
    55      * Instantiates a new CSS Match Maker.
       
    56      */
       
    57     public CSSMatchMaker()
       
    58         {
       
    59         iPseudoClassResolver = new PseudoClassResolver();
       
    60         }
       
    61 
       
    62     /**
       
    63      * Compares if CSS rule with it's selector has style instructions for given
       
    64      * DOM Element.
       
    65      *
       
    66      * @param aElement W3C DOM Element
       
    67      * @param aRule The style rule
       
    68      *
       
    69      * @return true, if CSS Selector matches DOM Element
       
    70      */
       
    71     public boolean match( CSSRule aRule, Element aElement ){
       
    72         aRule.resetSpecificity();
       
    73         return match( aRule.getSelector(), aRule.getSpecificity(), aElement );
       
    74 
       
    75     }
       
    76 
       
    77     /**
       
    78      * Compares if CSS rule's selector has style instructions for given DOM
       
    79      * Element.
       
    80      *
       
    81      * @param aSelector the a selector
       
    82      * @param aSpecificity CSSRules specificity table to be updated during the
       
    83      *            comparison
       
    84      * @param aElement the a element
       
    85      *
       
    86      * @return true, if given selector match DOM Element
       
    87      */
       
    88     private boolean match( Selector aSelector, CSSSpecificity aSpecificity, Element aElement )
       
    89         {
       
    90         switch ( aSelector.getSelectorType() )
       
    91             {
       
    92             case Selector.SAC_ELEMENT_NODE_SELECTOR:
       
    93                 {
       
    94                 ElementSelector eSelector = ( ElementSelector ) aSelector;
       
    95                 String name = eSelector.getLocalName();
       
    96 //                if("box".equals(aElement.getLocalName())&& "box".equals(name) ){
       
    97 //                	System.out.println("!!!");
       
    98 //                }
       
    99                 if ( name == null || name.equals( aElement.getLocalName() ) )
       
   100                     {
       
   101                     aSpecificity.incElement();
       
   102                     return true;
       
   103                     }
       
   104                 return false;
       
   105                 }
       
   106 
       
   107             case Selector.SAC_CONDITIONAL_SELECTOR:
       
   108                 {
       
   109                 ConditionalSelector cSelector = ( ConditionalSelector ) aSelector;
       
   110                 if ( !match( cSelector.getSimpleSelector(), aSpecificity,
       
   111                         aElement ) )
       
   112                     {
       
   113                     return false;
       
   114                     }
       
   115                 return matchCondition( cSelector.getCondition(), aSpecificity,
       
   116                         aElement );
       
   117                 }
       
   118 
       
   119             case Selector.SAC_CHILD_SELECTOR:
       
   120                 {
       
   121                 DescendantSelector dSelector = ( DescendantSelector ) aSelector;
       
   122                 if ( !match( dSelector.getSimpleSelector(), aSpecificity,
       
   123                         aElement ) )
       
   124                     {
       
   125                     return false;
       
   126                     }
       
   127                 return match( dSelector.getAncestorSelector(), aSpecificity,
       
   128                         ( Element ) aElement.getParentNode() );
       
   129                 }
       
   130             case Selector.SAC_DESCENDANT_SELECTOR:
       
   131                 {
       
   132                 DescendantSelector dSelector = ( DescendantSelector ) aSelector;
       
   133                 if ( !match( dSelector.getSimpleSelector(), aSpecificity,
       
   134                         aElement ) )
       
   135                     {
       
   136                     return false;
       
   137                     }
       
   138 
       
   139                 Node ancestor = aElement;
       
   140                 while ( ( ancestor = ancestor.getParentNode() ) != null )
       
   141                     {
       
   142                     if ( ancestor.getNodeType() == Node.ELEMENT_NODE )
       
   143                         {
       
   144                         if ( match( dSelector.getAncestorSelector(),
       
   145                                 aSpecificity, ( Element ) ancestor ) )
       
   146                             {
       
   147                             return true;
       
   148                             }
       
   149                         }
       
   150                     }
       
   151                 return false;
       
   152                 }
       
   153             case Selector.SAC_ANY_NODE_SELECTOR:
       
   154             case Selector.SAC_DIRECT_ADJACENT_SELECTOR:
       
   155             case Selector.SAC_CDATA_SECTION_NODE_SELECTOR:
       
   156             case Selector.SAC_COMMENT_NODE_SELECTOR:
       
   157             case Selector.SAC_NEGATIVE_SELECTOR:
       
   158             case Selector.SAC_PROCESSING_INSTRUCTION_NODE_SELECTOR:
       
   159             case Selector.SAC_PSEUDO_ELEMENT_SELECTOR:
       
   160             case Selector.SAC_ROOT_NODE_SELECTOR:
       
   161             case Selector.SAC_TEXT_NODE_SELECTOR:
       
   162                 {
       
   163                 throw new CSSException( "Selector : "
       
   164                         + aSelector.getSelectorType() + " not supported" );
       
   165                 }
       
   166             default:
       
   167                 throw new CSSException( "Unknown selector : "
       
   168                         + aSelector.getSelectorType() );
       
   169             }
       
   170         }
       
   171 
       
   172     /**
       
   173      * For Conditional Selectors, it is also necessary to check if selectors
       
   174      * conditions match with the DOM Element.
       
   175      *
       
   176      * @param aCondition The condition of the conditional selector
       
   177      * @param aElement The DOM element
       
   178      * @param aSpecificity CSSRules specificity table to be updated during the
       
   179      *            comparison
       
   180      *
       
   181      * @return true, if given Condition match DOM Element
       
   182      */
       
   183     private boolean matchCondition( Condition aCondition,
       
   184             CSSSpecificity aSpecificity, Element aElement )
       
   185         {
       
   186         switch ( aCondition.getConditionType() )
       
   187             {
       
   188             case Condition.SAC_ID_CONDITION:
       
   189                 {
       
   190                 AttributeCondition idCondition = ( AttributeCondition ) aCondition;
       
   191                 String idAttribute = aElement.getAttribute( STRING_ID );
       
   192 
       
   193                 String[] idTexts = idAttribute.split( STRING_SPACE );
       
   194 
       
   195                 for ( int i = 0; i < idTexts.length; i++ )
       
   196                     {
       
   197                     String idText = idTexts[ i ];
       
   198                     if ( idAttribute != null
       
   199                             && idText.equals( idCondition.getValue() ) )
       
   200                         {
       
   201                         aSpecificity.incID();
       
   202                         return true;
       
   203                         }
       
   204                     }
       
   205                 return false;
       
   206                 }
       
   207 
       
   208             case Condition.SAC_CLASS_CONDITION:
       
   209                 {
       
   210                 AttributeCondition classCondition = ( AttributeCondition ) aCondition;
       
   211                 String classAttribute = aElement.getAttribute( STRING_CLASS );
       
   212 
       
   213                 String[] classTexts = classAttribute.split( STRING_SPACE );
       
   214 
       
   215                 for ( int i = 0; i < classTexts.length; i++ )
       
   216                     {
       
   217                     String classText = classTexts[ i ];
       
   218                     if ( classAttribute != null
       
   219                             && classText.equals( classCondition.getValue() ) )
       
   220                         {
       
   221                         aSpecificity.incAttribute();
       
   222                         return true;
       
   223                         }
       
   224                     }
       
   225                 return false;
       
   226                 }
       
   227 
       
   228             case Condition.SAC_AND_CONDITION:
       
   229                 {
       
   230                 CombinatorCondition combCondition = ( CombinatorCondition ) aCondition;
       
   231                 return matchCondition( combCondition.getFirstCondition(),
       
   232                         aSpecificity, aElement )
       
   233                         && matchCondition( combCondition.getSecondCondition(),
       
   234                                 aSpecificity, aElement );
       
   235                 }
       
   236 
       
   237             case Condition.SAC_PSEUDO_CLASS_CONDITION:
       
   238                 {
       
   239                 Collection pseudoTypes = iPseudoClassResolver
       
   240                         .getPseudoTypes();
       
   241 
       
   242                 for ( Iterator it = pseudoTypes.iterator(); it
       
   243                         .hasNext(); )
       
   244                     {
       
   245                     if ( it.next()
       
   246                             .equals( aCondition.toString().substring( 1 ) ) )
       
   247                         {
       
   248                         aSpecificity.incAttribute();
       
   249                         return true;
       
   250                         }
       
   251                     }
       
   252                 return false;
       
   253                 }
       
   254             case Condition.SAC_ATTRIBUTE_CONDITION:
       
   255             case Condition.SAC_ONE_OF_ATTRIBUTE_CONDITION:
       
   256             case Condition.SAC_BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
       
   257             case Condition.SAC_OR_CONDITION:
       
   258             case Condition.SAC_NEGATIVE_CONDITION:
       
   259             case Condition.SAC_POSITIONAL_CONDITION:
       
   260             case Condition.SAC_LANG_CONDITION:
       
   261             case Condition.SAC_ONLY_CHILD_CONDITION:
       
   262             case Condition.SAC_ONLY_TYPE_CONDITION:
       
   263             case Condition.SAC_CONTENT_CONDITION:
       
   264                 {
       
   265                 throw new CSSException( "condition : "
       
   266                         + aCondition.getConditionType() + " not supported" );
       
   267                 }
       
   268             default:
       
   269                 throw new CSSException( "Unknown condition : "
       
   270                         + aCondition.getConditionType() );
       
   271             }
       
   272         }
       
   273     }