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 public PolicyMetaData readPolicy( final Element element )
35 throws Exception
36 {
37 final String version = element.getAttribute( "version" );
38 if( !"1.0".equals( version ) )
39 {
40 final String message = "Bad version:" + version;
41 throw new Exception( message );
42 }
43
44 final NodeList keyStoreConfigs = element.getElementsByTagName( "keystore" );
45 final KeyStoreMetaData[] keyStores = buildKeyStores( keyStoreConfigs );
46
47 final NodeList grantConfigs =
48 element.getElementsByTagName( "grant" );
49 final GrantMetaData[] grants = buildGrants( grantConfigs );
50
51 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 private GrantMetaData[] buildGrants( final NodeList elements )
61 throws Exception
62 {
63 final ArrayList grants = new ArrayList();
64 final int length = elements.getLength();
65
66 for( int i = 0; i < length; i++ )
67 {
68 final Element element = (Element)elements.item( i );
69 final GrantMetaData grant = buildGrant( element );
70 grants.add( grant );
71 }
72
73 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 private GrantMetaData buildGrant( final Element element )
83 throws Exception
84 {
85 final String codeBase = getAttribute( element, "code-base" );
86 final String signedBy = getAttribute( element, "signed-by" );
87 String keyStore = getAttribute( element, "key-store" );
88 if( null != signedBy && null == keyStore )
89 {
90 keyStore = "default";
91 }
92 final NodeList permissionElements =
93 element.getElementsByTagName( "permission" );
94 final PermissionMetaData[] permissions = buildPermissions( permissionElements );
95 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 private PermissionMetaData[] buildPermissions( final NodeList elements )
105 throws Exception
106 {
107 final ArrayList grants = new ArrayList();
108 final int length = elements.getLength();
109
110 for( int i = 0; i < length; i++ )
111 {
112 final Element element = (Element)elements.item( i );
113 final PermissionMetaData permission = buildPermission( element );
114 grants.add( permission );
115 }
116
117 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 private PermissionMetaData buildPermission( final Element element )
127 throws Exception
128 {
129 final String classname = getAttribute( element, "class" );
130 final String target = getAttribute( element, "target" );
131 final String action = getAttribute( element, "action" );
132 final String signedBy = getAttribute( element, "signed-by" );
133 String keyStore = getAttribute( element, "key-store" );
134 if( null != signedBy && null == keyStore )
135 {
136 keyStore = "default";
137 }
138 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 private KeyStoreMetaData[] buildKeyStores( final NodeList elements )
149 throws Exception
150 {
151 final ArrayList keyStores = new ArrayList();
152 final int length = elements.getLength();
153
154 for( int i = 0; i < length; i++ )
155 {
156 final Element element = (Element)elements.item( i );
157 final KeyStoreMetaData keyStore = buildKeyStore( element );
158 keyStores.add( keyStore );
159 }
160
161 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 private KeyStoreMetaData buildKeyStore( final Element element )
171 throws Exception
172 {
173 final String name = element.getAttribute( "name" );
174 final String location = getAttribute( element, "location" );
175 final String type = getAttribute( element, "type" );
176 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 private String getAttribute( final Element element,
189 final String name )
190 {
191 final String value = element.getAttribute( name );
192 if( "".equals( value ) )
193 {
194 return null;
195 }
196 else
197 {
198 return value;
199 }
200 }
201 }
This page was automatically generated by Maven