themeinstaller/source/src/com/nokia/tools/themeinstaller/cssparser/CSSSelectorParser.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 SAC selector to find out its selector type
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.tools.themeinstaller.cssparser;
       
    20 
       
    21 import org.w3c.css.sac.CSSException;
       
    22 import org.w3c.css.sac.CombinatorCondition;
       
    23 import org.w3c.css.sac.Condition;
       
    24 import org.w3c.css.sac.ConditionalSelector;
       
    25 import org.w3c.css.sac.DescendantSelector;
       
    26 import org.w3c.css.sac.Selector;
       
    27 
       
    28 /**
       
    29  * Parses SAC Selector in order to find out if selector has pseudo condition.
       
    30 
       
    31  * For example selector "element.class:pseudo" is recognized as Conditional
       
    32  * Selector and the Selector's condition is ".class:pseudo" is recognized as
       
    33  * SAC_AND_CONDITION.
       
    34  *
       
    35  * After SAC_AND_CONDITION is parsed to two different conditions : ".class" and
       
    36  * ":pseudo", it is possible to tell that the original selector is Pseudo
       
    37  * Selector
       
    38  *
       
    39  */
       
    40 public class CSSSelectorParser
       
    41     {
       
    42 
       
    43     /**
       
    44      * Checks if Selector is pseudo selector.
       
    45      *
       
    46      * @param aSelector The selector
       
    47      *
       
    48      * @return true, if is pseudo selector
       
    49      */
       
    50     public static boolean isPseudo( Selector aSelector )
       
    51         {
       
    52         switch ( aSelector.getSelectorType() )
       
    53             {
       
    54             case Selector.SAC_ELEMENT_NODE_SELECTOR:
       
    55                 {
       
    56                 return false;
       
    57                 }
       
    58             case Selector.SAC_CONDITIONAL_SELECTOR:
       
    59                 {
       
    60                 ConditionalSelector cSelector = ( ConditionalSelector ) aSelector;
       
    61                 return isPseudoCondition( cSelector.getCondition() );
       
    62                 }
       
    63             case Selector.SAC_CHILD_SELECTOR:
       
    64                 {
       
    65                 DescendantSelector dSelector = ( DescendantSelector ) aSelector;
       
    66                 return isPseudo( dSelector.getSimpleSelector() );
       
    67                 }
       
    68             case Selector.SAC_DESCENDANT_SELECTOR:
       
    69                 {
       
    70                 DescendantSelector dSelector = ( DescendantSelector ) aSelector;
       
    71                 return isPseudo( dSelector.getSimpleSelector() );
       
    72                 }
       
    73             case Selector.SAC_ANY_NODE_SELECTOR:
       
    74             case Selector.SAC_DIRECT_ADJACENT_SELECTOR:
       
    75             case Selector.SAC_CDATA_SECTION_NODE_SELECTOR:
       
    76             case Selector.SAC_COMMENT_NODE_SELECTOR:
       
    77             case Selector.SAC_NEGATIVE_SELECTOR:
       
    78             case Selector.SAC_PROCESSING_INSTRUCTION_NODE_SELECTOR:
       
    79             case Selector.SAC_PSEUDO_ELEMENT_SELECTOR:
       
    80             case Selector.SAC_ROOT_NODE_SELECTOR:
       
    81             case Selector.SAC_TEXT_NODE_SELECTOR:
       
    82                 {
       
    83                 throw new CSSException( "Selector : "
       
    84                         + aSelector.getSelectorType() + " not supported" );
       
    85                 }
       
    86             default:
       
    87                 throw new CSSException( "Unknown selector : "
       
    88                         + aSelector.getSelectorType() );
       
    89             }
       
    90         }
       
    91 
       
    92     /**
       
    93      * Checks if SAC Condition is pseudo condition.
       
    94      *
       
    95      * @param aCondition The condition
       
    96      *
       
    97      * @return true, if is pseudo condition
       
    98      */
       
    99     private static boolean isPseudoCondition( Condition aCondition )
       
   100         {
       
   101         switch ( aCondition.getConditionType() )
       
   102             {
       
   103             case Condition.SAC_ID_CONDITION:
       
   104                 {
       
   105                 return false;
       
   106                 }
       
   107             case Condition.SAC_CLASS_CONDITION:
       
   108                 {
       
   109                 return false;
       
   110                 }
       
   111             case Condition.SAC_AND_CONDITION:
       
   112                 {
       
   113                 CombinatorCondition combCondition = ( CombinatorCondition ) aCondition;
       
   114                 return isPseudoCondition( combCondition.getFirstCondition() )
       
   115                         || isPseudoCondition( combCondition
       
   116                                 .getSecondCondition() );
       
   117                 }
       
   118             case Condition.SAC_PSEUDO_CLASS_CONDITION:
       
   119                 {
       
   120                 return true;
       
   121                 }
       
   122             case Condition.SAC_ATTRIBUTE_CONDITION:
       
   123             case Condition.SAC_ONE_OF_ATTRIBUTE_CONDITION:
       
   124             case Condition.SAC_BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
       
   125             case Condition.SAC_OR_CONDITION:
       
   126             case Condition.SAC_NEGATIVE_CONDITION:
       
   127             case Condition.SAC_POSITIONAL_CONDITION:
       
   128             case Condition.SAC_LANG_CONDITION:
       
   129             case Condition.SAC_ONLY_CHILD_CONDITION:
       
   130             case Condition.SAC_ONLY_TYPE_CONDITION:
       
   131             case Condition.SAC_CONTENT_CONDITION:
       
   132                 {
       
   133                 throw new CSSException( "condition : "
       
   134                         + aCondition.getConditionType() + " not supported" );
       
   135 
       
   136                 }
       
   137             default:
       
   138                 throw new CSSException( "Unknown condition : "
       
   139                         + aCondition.getConditionType() );
       
   140             }
       
   141         }
       
   142     }