KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > xmlpolicy > reader > PolicyReader


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.loom.xmlpolicy.reader;
9
10 import java.util.ArrayList JavaDoc;
11
12 import org.codehaus.loom.xmlpolicy.metadata.GrantMetaData;
13 import org.codehaus.loom.xmlpolicy.metadata.KeyStoreMetaData;
14 import org.codehaus.loom.xmlpolicy.metadata.PermissionMetaData;
15 import org.codehaus.loom.xmlpolicy.metadata.PolicyMetaData;
16 import org.w3c.dom.Element JavaDoc;
17 import org.w3c.dom.NodeList JavaDoc;
18
19 /**
20  * This class builds a {@link PolicyMetaData} object from
21  * specified XML document.
22  *
23  * @author Peter Donald
24  * @version $Revision: 1.1 $ $Date: 2004/04/19 22:20:27 $
25  */

26 public class PolicyReader
27 {
28     /**
29      * Build ClassLoader MetaData from a DOM tree.
30      *
31      * @param element the root element
32      * @return the meta data
33      * @throws Exception if malformed DOM
34      */

35     public PolicyMetaData readPolicy( final Element JavaDoc element )
36         throws Exception JavaDoc
37     {
38         final String JavaDoc version = element.getAttribute( "version" );
39         if( !"1.0".equals( version ) )
40         {
41             final String JavaDoc message = "Bad version:" + version;
42             throw new Exception JavaDoc( message );
43         }
44
45         final NodeList JavaDoc keyStoreConfigs = element.getElementsByTagName( "keystore" );
46         final KeyStoreMetaData[] keyStores = buildKeyStores( keyStoreConfigs );
47
48         final NodeList JavaDoc grantConfigs =
49             element.getElementsByTagName( "grant" );
50         final GrantMetaData[] grants = buildGrants( grantConfigs );
51
52         return new PolicyMetaData( keyStores, grants );
53     }
54
55     /**
56      * Build an array of GrantMetaDatas from node list.
57      *
58      * @param elements the nodes to process
59      * @return the GrantMetaData
60      */

61     private GrantMetaData[] buildGrants( final NodeList JavaDoc elements )
62         throws Exception JavaDoc
63     {
64         final ArrayList JavaDoc grants = new ArrayList JavaDoc();
65         final int length = elements.getLength();
66
67         for( int i = 0; i < length; i++ )
68         {
69             final Element JavaDoc element = (Element JavaDoc)elements.item( i );
70             final GrantMetaData grant = buildGrant( element );
71             grants.add( grant );
72         }
73
74         return (GrantMetaData[])grants.toArray( new GrantMetaData[ grants.size() ] );
75     }
76
77     /**
78      * Build a GrantMetaData from an element.
79      *
80      * @param element the nodes to process
81      * @return the GrantMetaData
82      */

83     private GrantMetaData buildGrant( final Element JavaDoc element )
84         throws Exception JavaDoc
85     {
86         final String JavaDoc codeBase = getAttribute( element, "code-base" );
87         final String JavaDoc signedBy = getAttribute( element, "signed-by" );
88         String JavaDoc keyStore = getAttribute( element, "key-store" );
89         if( null != signedBy && null == keyStore )
90         {
91             keyStore = "default";
92         }
93         final NodeList JavaDoc permissionElements =
94             element.getElementsByTagName( "permission" );
95         final PermissionMetaData[] permissions = buildPermissions( permissionElements );
96         return new GrantMetaData( codeBase, signedBy, keyStore, permissions );
97     }
98
99     /**
100      * Build an array of PermissionMetaDatas from node list.
101      *
102      * @param elements the nodes to process
103      * @return the PermissionMetaDatas
104      */

105     private PermissionMetaData[] buildPermissions( final NodeList JavaDoc elements )
106         throws Exception JavaDoc
107     {
108         final ArrayList JavaDoc grants = new ArrayList JavaDoc();
109         final int length = elements.getLength();
110
111         for( int i = 0; i < length; i++ )
112         {
113             final Element JavaDoc element = (Element JavaDoc)elements.item( i );
114             final PermissionMetaData permission = buildPermission( element );
115             grants.add( permission );
116         }
117
118         return (PermissionMetaData[])grants.toArray( new PermissionMetaData[ grants.size() ] );
119     }
120
121     /**
122      * Build a PermissionMetaData from an element.
123      *
124      * @param element the node to process
125      * @return the PermissionMetaData
126      */

127     private PermissionMetaData buildPermission( final Element JavaDoc element )
128         throws Exception JavaDoc
129     {
130         final String JavaDoc classname = getAttribute( element, "class" );
131         final String JavaDoc target = getAttribute( element, "target" );
132         final String JavaDoc action = getAttribute( element, "action" );
133         final String JavaDoc signedBy = getAttribute( element, "signed-by" );
134         String JavaDoc keyStore = getAttribute( element, "key-store" );
135         if( null != signedBy && null == keyStore )
136         {
137             keyStore = "default";
138         }
139         return new PermissionMetaData( classname, target, action,
140                                        signedBy, keyStore );
141     }
142
143     /**
144      * Build an array of KeyStore meta datas from node list.
145      *
146      * @param elements the nodes to process
147      * @return the keyStores
148      */

149     private KeyStoreMetaData[] buildKeyStores( final NodeList JavaDoc elements )
150         throws Exception JavaDoc
151     {
152         final ArrayList JavaDoc keyStores = new ArrayList JavaDoc();
153         final int length = elements.getLength();
154
155         for( int i = 0; i < length; i++ )
156         {
157             final Element JavaDoc element = (Element JavaDoc)elements.item( i );
158             final KeyStoreMetaData keyStore = buildKeyStore( element );
159             keyStores.add( keyStore );
160         }
161
162         return (KeyStoreMetaData[])keyStores.toArray( new KeyStoreMetaData[ keyStores.size() ] );
163     }
164
165     /**
166      * Build a KeyStoreMetaData from an element.
167      *
168      * @param element the nodes to process
169      * @return the keyStore
170      */

171     private KeyStoreMetaData buildKeyStore( final Element JavaDoc element )
172         throws Exception JavaDoc
173     {
174         final String JavaDoc name = element.getAttribute( "name" );
175         final String JavaDoc location = getAttribute( element, "location" );
176         final String JavaDoc type = getAttribute( element, "type" );
177         return new KeyStoreMetaData( name, location, type );
178     }
179
180     /**
181      * Utility method to get value of attribute. If attribute
182      * has a empty/null value or does not appear in XML then return
183      * null, elese return value.
184      *
185      * @param element the element
186      * @param name the attribute name
187      * @return the cleaned attribute value
188      */

189     private String JavaDoc getAttribute( final Element JavaDoc element,
190                                  final String JavaDoc name )
191     {
192         final String JavaDoc value = element.getAttribute( name );
193         if( "".equals( value ) )
194         {
195             return null;
196         }
197         else
198         {
199             return value;
200         }
201     }
202 }
203
Popular Tags