Clover coverage report - XMLPolicy - 1.1
Coverage timestamp: Tue Dec 2 2003 20:21:22 EST
file stats: LOC: 202   Methods: 8
NCLOC: 117   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PolicyReader.java 21.4% 25.9% 50% 27.6%
coverage coverage
 1   
 /*
 2   
  * Copyright (C) The Spice Group. All rights reserved.
 3   
  *
 4   
  * This software is published under the terms of the Spice
 5   
  * Software License version 1.1, a copy of which has been included
 6   
  * with this distribution in the LICENSE.txt file.
 7   
  */
 8   
 package org.codehaus.spice.xmlpolicy.reader;
 9   
 
 10   
 import java.util.ArrayList;
 11   
 import org.codehaus.spice.xmlpolicy.metadata.GrantMetaData;
 12   
 import org.codehaus.spice.xmlpolicy.metadata.KeyStoreMetaData;
 13   
 import org.codehaus.spice.xmlpolicy.metadata.PermissionMetaData;
 14   
 import org.codehaus.spice.xmlpolicy.metadata.PolicyMetaData;
 15   
 import org.w3c.dom.Element;
 16   
 import org.w3c.dom.NodeList;
 17   
 
 18   
 /**
 19   
  * This class builds a {@link PolicyMetaData} object from
 20   
  * specified XML document.
 21   
  *
 22   
  * @author Peter Donald
 23   
  * @version $Revision: 1.1 $ $Date: 2003/12/02 09:16:06 $
 24   
  */
 25   
 public class PolicyReader
 26   
 {
 27   
     /**
 28   
      * Build ClassLoader MetaData from a DOM tree.
 29   
      *
 30   
      * @param element the root element
 31   
      * @return the meta data
 32   
      * @throws Exception if malformed DOM
 33   
      */
 34  2
     public PolicyMetaData readPolicy( final Element element )
 35   
         throws Exception
 36   
     {
 37  2
         final String version = element.getAttribute( "version" );
 38  2
         if( !"1.0".equals( version ) )
 39   
         {
 40  0
             final String message = "Bad version:" + version;
 41  0
             throw new Exception( message );
 42   
         }
 43   
 
 44  2
         final NodeList keyStoreConfigs = element.getElementsByTagName( "keystore" );
 45  2
         final KeyStoreMetaData[] keyStores = buildKeyStores( keyStoreConfigs );
 46   
 
 47  0
         final NodeList grantConfigs =
 48   
             element.getElementsByTagName( "grant" );
 49  0
         final GrantMetaData[] grants = buildGrants( grantConfigs );
 50   
 
 51  0
         return new PolicyMetaData( keyStores, grants );
 52   
     }
 53   
 
 54   
     /**
 55   
      * Build an array of GrantMetaDatas from node list.
 56   
      *
 57   
      * @param elements the nodes to process
 58   
      * @return the GrantMetaData
 59   
      */
 60  0
     private GrantMetaData[] buildGrants( final NodeList elements )
 61   
         throws Exception
 62   
     {
 63  0
         final ArrayList grants = new ArrayList();
 64  0
         final int length = elements.getLength();
 65   
 
 66  0
         for( int i = 0; i < length; i++ )
 67   
         {
 68  0
             final Element element = (Element)elements.item( i );
 69  0
             final GrantMetaData grant = buildGrant( element );
 70  0
             grants.add( grant );
 71   
         }
 72   
 
 73  0
         return (GrantMetaData[])grants.toArray( new GrantMetaData[ grants.size() ] );
 74   
     }
 75   
 
 76   
     /**
 77   
      * Build a GrantMetaData from an element.
 78   
      *
 79   
      * @param element the nodes to process
 80   
      * @return the GrantMetaData
 81   
      */
 82  0
     private GrantMetaData buildGrant( final Element element )
 83   
         throws Exception
 84   
     {
 85  0
         final String codeBase = getAttribute( element, "code-base" );
 86  0
         final String signedBy = getAttribute( element, "signed-by" );
 87  0
         String keyStore = getAttribute( element, "key-store" );
 88  0
         if( null != signedBy && null == keyStore )
 89   
         {
 90  0
             keyStore = "default";
 91   
         }
 92  0
         final NodeList permissionElements =
 93   
             element.getElementsByTagName( "permission" );
 94  0
         final PermissionMetaData[] permissions = buildPermissions( permissionElements );
 95  0
         return new GrantMetaData( codeBase, signedBy, keyStore, permissions );
 96   
     }
 97   
 
 98   
     /**
 99   
      * Build an array of PermissionMetaDatas from node list.
 100   
      *
 101   
      * @param elements the nodes to process
 102   
      * @return the PermissionMetaDatas
 103   
      */
 104  0
     private PermissionMetaData[] buildPermissions( final NodeList elements )
 105   
         throws Exception
 106   
     {
 107  0
         final ArrayList grants = new ArrayList();
 108  0
         final int length = elements.getLength();
 109   
 
 110  0
         for( int i = 0; i < length; i++ )
 111   
         {
 112  0
             final Element element = (Element)elements.item( i );
 113  0
             final PermissionMetaData permission = buildPermission( element );
 114  0
             grants.add( permission );
 115   
         }
 116   
 
 117  0
         return (PermissionMetaData[])grants.toArray( new PermissionMetaData[ grants.size() ] );
 118   
     }
 119   
 
 120   
     /**
 121   
      * Build a PermissionMetaData from an element.
 122   
      *
 123   
      * @param element the node to process
 124   
      * @return the PermissionMetaData
 125   
      */
 126  0
     private PermissionMetaData buildPermission( final Element element )
 127   
         throws Exception
 128   
     {
 129  0
         final String classname = getAttribute( element, "class" );
 130  0
         final String target = getAttribute( element, "target" );
 131  0
         final String action = getAttribute( element, "action" );
 132  0
         final String signedBy = getAttribute( element, "signed-by" );
 133  0
         String keyStore = getAttribute( element, "key-store" );
 134  0
         if( null != signedBy && null == keyStore )
 135   
         {
 136  0
             keyStore = "default";
 137   
         }
 138  0
         return new PermissionMetaData( classname, target, action,
 139   
                                        signedBy, keyStore );
 140   
     }
 141   
 
 142   
     /**
 143   
      * Build an array of KeyStore meta datas from node list.
 144   
      *
 145   
      * @param elements the nodes to process
 146   
      * @return the keyStores
 147   
      */
 148  2
     private KeyStoreMetaData[] buildKeyStores( final NodeList elements )
 149   
         throws Exception
 150   
     {
 151  2
         final ArrayList keyStores = new ArrayList();
 152  2
         final int length = elements.getLength();
 153   
 
 154  2
         for( int i = 0; i < length; i++ )
 155   
         {
 156  2
             final Element element = (Element)elements.item( i );
 157  2
             final KeyStoreMetaData keyStore = buildKeyStore( element );
 158  0
             keyStores.add( keyStore );
 159   
         }
 160   
 
 161  0
         return (KeyStoreMetaData[])keyStores.toArray( new KeyStoreMetaData[ keyStores.size() ] );
 162   
     }
 163   
 
 164   
     /**
 165   
      * Build a KeyStoreMetaData from an element.
 166   
      *
 167   
      * @param element the nodes to process
 168   
      * @return the keyStore
 169   
      */
 170  2
     private KeyStoreMetaData buildKeyStore( final Element element )
 171   
         throws Exception
 172   
     {
 173  2
         final String name = element.getAttribute( "name" );
 174  2
         final String location = getAttribute( element, "location" );
 175  0
         final String type = getAttribute( element, "type" );
 176  0
         return new KeyStoreMetaData( name, location, type );
 177   
     }
 178   
 
 179   
     /**
 180   
      * Utility method to get value of attribute. If attribute
 181   
      * has a empty/null value or does not appear in XML then return
 182   
      * null, elese return value.
 183   
      *
 184   
      * @param element the element
 185   
      * @param name the attribute name
 186   
      * @return the cleaned attribute value
 187   
      */
 188  2
     private String getAttribute( final Element element,
 189   
                                  final String name )
 190   
     {
 191  2
         final String value = element.getAttribute( name );
 192  2
         if( "".equals( value ) )
 193   
         {
 194  0
             return null;
 195   
         }
 196   
         else
 197   
         {
 198  2
             return value;
 199   
         }
 200   
     }
 201   
 }
 202