Home | History | Annotate | Download | only in dom
      1 /*
      2 * Copyright (C) 2012 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *      http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 package com.motorola.studio.android.model.manifest.dom;
     17 
     18 import java.util.HashMap;
     19 import java.util.List;
     20 import java.util.Map;
     21 
     22 import org.eclipse.core.runtime.IStatus;
     23 
     24 /**
     25  * Class that represents a <permission> node on AndroidManifest.xml file
     26  */
     27 public class PermissionNode extends AbstractIconLabelNameNode
     28 {
     29     static
     30     {
     31         defaultProperties.add(PROP_DESCRIPTION);
     32         defaultProperties.add(PROP_PERMISSIONGROUP);
     33         defaultProperties.add(PROP_PROTECTIONLEVEL);
     34     }
     35 
     36     /**
     37      * Enumeration for protectionLevel property
     38      */
     39     public enum ProtectionLevel
     40     {
     41         normal, dangerous, signature, signatureOrSystem
     42     }
     43 
     44     /**
     45      * Map to resolve the string<->enumeration association of protectionLevel property
     46      */
     47     private static Map<String, ProtectionLevel> protectionLevelMap;
     48 
     49     static
     50     {
     51         // Loads the map for protectionLevel
     52         protectionLevelMap = new HashMap<String, ProtectionLevel>();
     53         protectionLevelMap.put(ProtectionLevel.normal.toString().toLowerCase(),
     54                 ProtectionLevel.normal);
     55         protectionLevelMap.put(ProtectionLevel.dangerous.toString().toLowerCase(),
     56                 ProtectionLevel.dangerous);
     57         protectionLevelMap.put(ProtectionLevel.signature.toString().toLowerCase(),
     58                 ProtectionLevel.signature);
     59         protectionLevelMap.put(ProtectionLevel.signatureOrSystem.toString().toLowerCase(),
     60                 ProtectionLevel.signatureOrSystem);
     61     }
     62 
     63     /**
     64      * Gets the enumeration value from the ProtectionLevel enumeration from a given name
     65      *
     66      * @param name the protectionLevel name
     67      * @return the enumeration value from ProtectionLevel enumeration
     68      */
     69     public static ProtectionLevel getProtectionLevel(String name)
     70     {
     71         ProtectionLevel protectionLevel = null;
     72 
     73         if (name != null)
     74         {
     75             String pl = name.trim().toLowerCase();
     76             protectionLevel = protectionLevelMap.get(pl);
     77         }
     78 
     79         return protectionLevel;
     80     }
     81 
     82     /**
     83      * Gets the protectionLevel parameter name from a given ProtectionLevel enumeration value
     84      *
     85      * @param protectionLevel the enumeration value
     86      * @return the parameter name
     87      */
     88     public static String getProtectionLevelName(ProtectionLevel protectionLevel)
     89     {
     90         String name = "";
     91 
     92         if (protectionLevel != null)
     93         {
     94             name = protectionLevel.toString();
     95         }
     96 
     97         return name;
     98     }
     99 
    100     /**
    101      * The description property
    102      */
    103     private String propDescription = null;
    104 
    105     /**
    106      * The permissionGroup property
    107      */
    108     private String propPermissionGroup = null;
    109 
    110     /**
    111      * The protectionLevel property
    112      */
    113     private ProtectionLevel propProtectionLevel = null;
    114 
    115     /**
    116      * Default constructor
    117      *
    118      * @param name the name property. It must not be set to null
    119      */
    120     public PermissionNode(String name)
    121     {
    122         super(name);
    123     }
    124 
    125     /* (non-Javadoc)
    126      * @see com.motorola.studio.android.model.manifest.dom.AndroidManifestNode#canContains(com.motorola.studio.android.model.manifest.dom.AndroidManifestNode.NodeType)
    127      */
    128     @Override
    129     protected boolean canContains(NodeType nodeType)
    130     {
    131         // Always returns false. This node can not contain children.
    132         return false;
    133     }
    134 
    135     /* (non-Javadoc)
    136      * @see com.motorola.studio.android.model.manifest.dom.AbstractIconLabelNameNode#addAdditionalProperties()
    137      */
    138     @Override
    139     protected void addAdditionalProperties()
    140     {
    141         String protectionLevelName = getProtectionLevelName(propProtectionLevel);
    142         properties.put(PROP_PROTECTIONLEVEL, protectionLevelName);
    143 
    144         if ((propDescription != null) && (propDescription.length() > 0))
    145         {
    146             properties.put(PROP_DESCRIPTION, propDescription);
    147         }
    148 
    149         if ((propPermissionGroup != null) && (propPermissionGroup.trim().length() > 0))
    150         {
    151             properties.put(PROP_PERMISSIONGROUP, propPermissionGroup);
    152         }
    153     }
    154 
    155     /* (non-Javadoc)
    156      * @see com.motorola.studio.android.model.manifest.dom.AndroidManifestNode#getNodeType()
    157      */
    158     @Override
    159     public NodeType getNodeType()
    160     {
    161         return NodeType.Permission;
    162     }
    163 
    164     /* (non-Javadoc)
    165      * @see com.motorola.studio.android.model.manifest.dom.AndroidManifestNode#isNodeValid()
    166      */
    167     @Override
    168     protected boolean isNodeValid()
    169     {
    170         boolean isProtectionLevelValid = propProtectionLevel != null;
    171 
    172         return super.isNodeValid() && isProtectionLevelValid;
    173     }
    174 
    175     /**
    176      * Gets the description property value
    177      *
    178      * @return the description property value
    179      */
    180     public String getDescription()
    181     {
    182         return propDescription;
    183     }
    184 
    185     /**
    186      * Sets the description property value. Set it to null to remove it.
    187      *
    188      * @param description the description property value
    189      */
    190     public void setDescription(String description)
    191     {
    192         this.propDescription = description;
    193     }
    194 
    195     /**
    196      * Gets the permissionGroup property value
    197      *
    198      * @return the permissionGroup property value
    199      */
    200     public String getPermissionGroup()
    201     {
    202         return propPermissionGroup;
    203     }
    204 
    205     /**
    206      * Sets the permissionGroup property value. Set it to null to remove it.
    207      *
    208      * @param % the permissionGroup property value
    209      */
    210     public void setPermissionGroup(String permissionGroup)
    211     {
    212         this.propPermissionGroup = permissionGroup;
    213     }
    214 
    215     /**
    216      * Gets the protectionLevel property value
    217      *
    218      * @return the protectionLevel property value
    219      */
    220     public ProtectionLevel getProtectionLevel()
    221     {
    222         return propProtectionLevel;
    223     }
    224 
    225     /**
    226      * Sets the protectionLevel property value. Set it to null to remove it.
    227      *
    228      * @param % the protectionLevel property value
    229      */
    230     public void setProtectionLevel(ProtectionLevel protectionLevel)
    231     {
    232         this.propProtectionLevel = protectionLevel;
    233     }
    234 
    235     /* (non-Javadoc)
    236      * @see com.motorola.studio.android.model.manifest.dom.AndroidManifestNode#getSpecificNodeErrors()
    237      */
    238     @Override
    239     protected List<IStatus> getSpecificNodeProblems()
    240     {
    241         return null;
    242     }
    243 }
    244