KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > XMLUnmarshaller


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/XMLUnmarshaller.java,v 1.32.2.1 2004/08/18 08:37:24 luetzkendorf Exp $
3  * $Revision: 1.32.2.1 $
4  * $Date: 2004/08/18 08:37:24 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.io.IOException JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.util.Enumeration JavaDoc;
29
30 import javax.xml.parsers.SAXParser JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32
33 import org.apache.slide.content.NodeProperty;
34 import org.apache.slide.content.NodeRevisionContent;
35 import org.apache.slide.content.NodeRevisionDescriptor;
36 import org.apache.slide.content.NodeRevisionDescriptors;
37 import org.apache.slide.content.RevisionAlreadyExistException;
38 import org.apache.slide.content.RevisionContentNotFoundException;
39 import org.apache.slide.content.RevisionDescriptorNotFoundException;
40 import org.apache.slide.content.RevisionNotFoundException;
41 import org.apache.slide.event.VetoException;
42 import org.apache.slide.lock.ObjectLockedException;
43 import org.apache.slide.security.AccessDeniedException;
44 import org.apache.slide.security.NodePermission;
45 import org.apache.slide.structure.ActionNode;
46 import org.apache.slide.structure.LinkNode;
47 import org.apache.slide.structure.LinkedObjectNotFoundException;
48 import org.apache.slide.structure.ObjectAlreadyExistsException;
49 import org.apache.slide.structure.ObjectNode;
50 import org.apache.slide.structure.ObjectNotFoundException;
51 import org.apache.slide.structure.SubjectNode;
52 import org.apache.slide.util.conf.Configuration;
53 import org.apache.slide.util.conf.ConfigurationElement;
54 import org.apache.slide.util.conf.ConfigurationException;
55 import org.apache.slide.util.conf.Populate;
56 import org.apache.slide.util.logger.Logger;
57 import org.xml.sax.InputSource JavaDoc;
58 import org.xml.sax.SAXException JavaDoc;
59
60
61
62 /**
63  * XMLUnmarshaller class.
64  *
65  * @version $Revision: 1.32.2.1 $
66  */

67 public final class XMLUnmarshaller {
68     
69     private final static String JavaDoc LOG_CHANNEL = XMLUnmarshaller.class.getName();
70     
71     // --------------------------------------------------------- Public Methods
72

73     
74     /**
75      * Import data from Avalon configuration object.
76      *
77      * @param token CredentialsToken, used for access to the namespace
78      * @param dataConfiguration Configuration object
79      * @exception ConfigurationException Something went wrong during the
80      * reading of the XML
81      * @exception UnknownObjectClassException Object class not found
82      * @exception ServiceAccessException Error accessing service
83      */

84     public static void unmarshal(NamespaceAccessToken accessToken,
85                                  SlideToken token,
86                                  Configuration dataConfiguration)
87         throws ConfigurationException, UnknownObjectClassException,
88         ServiceAccessException {
89         
90         loadObjectNode(accessToken, token,
91                        dataConfiguration.getConfiguration("objectnode"));
92         
93     }
94     
95     
96     // -------------------------------------------------------- Private Methods
97

98     
99     /**
100      * Loads a Slide Object.
101      *
102      * @param token Credentials token
103      * @param objectDefinition Configuration object
104      * @exception ServiceAccessException Object creation failed because
105      * a data access error occured
106      * @exception UnknownObjectClassException Object class not found
107      * @exception ConfigurationException Something went wrong during the
108      * reading of the XML
109      */

110     private static void loadObjectNode
111         (NamespaceAccessToken accessToken, SlideToken token,
112          Configuration objectDefinition)
113         throws ServiceAccessException, ConfigurationException,
114         UnknownObjectClassException {
115         
116         String JavaDoc className = objectDefinition.getAttribute("classname");
117         String JavaDoc uri = objectDefinition.getAttribute("uri");
118         
119         accessToken.getLogger().log("Loading object " + uri,
120                                     LOG_CHANNEL,Logger.INFO);
121         
122         try {
123             
124             Class JavaDoc objectClass = null;
125             try {
126                 // First, load the object's class
127
objectClass = Class.forName(className);
128             } catch (ClassNotFoundException JavaDoc e) {
129                 // Class loading failed : The requested class was not found
130
// We throw an exception and interrupt the loading of the file.
131
throw new UnknownObjectClassException(className);
132             }
133             ObjectNode object = null;
134             try {
135                 // Get a new instance of the class
136
object = (ObjectNode) objectClass.newInstance();
137             } catch(InstantiationException JavaDoc e) {
138                 // Instantiation failed for some reason
139
throw new UnknownObjectClassException(className);
140             } catch(IllegalAccessException JavaDoc e) {
141                 // The initializer could not be called because
142
// of access restrictions
143
throw new UnknownObjectClassException(className);
144             }
145             
146             try {
147                 if (object instanceof LinkNode) {
148                     String JavaDoc linkedUri =
149                          objectDefinition.getAttribute("linkedUri");
150                     accessToken.getStructureHelper().createLink
151                             (token, (LinkNode) object, uri,
152                              new SubjectNode(linkedUri));
153                 } else {
154                      accessToken.getStructureHelper().create
155                             (token, object, uri);
156                 }
157             } catch (ObjectAlreadyExistsException e) {
158                  // Ignore, log and continue
159
accessToken.getLogger().log
160                         ("Object already exists at " + uri,
161                          LOG_CHANNEL, Logger.INFO);
162             }
163             
164             // Retrieving the list of permissions on the object
165
Enumeration JavaDoc permissionDefinitions =
166                 objectDefinition.getConfigurations("permission");
167             
168             // We've made sure that the object exists.
169
// We now parse the permissions definition list, adding each
170
// permission to the object's permission list.
171
while (permissionDefinitions.hasMoreElements()) {
172                 
173                 Configuration permissionDefinition =
174                     (Configuration) permissionDefinitions.nextElement();
175                 // Create the NodePermission object matching the Castor object
176

177                 String JavaDoc subjectUri =
178                     permissionDefinition.getAttribute("subject");
179                 
180                 if (accessToken.getNamespaceConfig().getUsersPath().equals(subjectUri)) {
181                     subjectUri = SubjectNode.ALL_URI;
182                 }
183                 
184                 String JavaDoc actionUri =
185                     permissionDefinition.getAttribute("action");
186                 
187                 if (accessToken.getNamespaceConfig().getActionsPath().equals(actionUri)) {
188                     actionUri = ActionNode.ALL_URI;
189                 }
190                 
191                 boolean inheritable = true;
192                 
193                 try {
194                     if (permissionDefinition.getAttribute("inheritable")
195                         .equals("false")) {
196                         inheritable = false;
197                     }
198                 } catch (ConfigurationException e) {
199                 }
200                 
201                 boolean negative = false;
202                 
203                 try {
204                     if (permissionDefinition.getAttribute("negative")
205                         .equals("true")) {
206                         negative = true;
207                     }
208                 } catch (ConfigurationException e) {
209                 }
210                 
211                 NodePermission permission = new NodePermission
212                     (uri, subjectUri, actionUri, inheritable, negative);
213                 
214                 // Adding the NodePermission to the ObjectNode
215
accessToken.getSecurityHelper()
216                     .grantPermission(token, permission);
217                 
218             }
219             
220             // Retrieve the list of revisions of the object
221
Enumeration JavaDoc revisionDefinitions =
222                 objectDefinition.getConfigurations("revision");
223             boolean revisionDefinitionsFound = false;
224             
225             // We parse the revision definition list
226
while (revisionDefinitions.hasMoreElements()) {
227                 
228                 revisionDefinitionsFound = true;
229                 Configuration revisionDefinition =
230                     (Configuration) revisionDefinitions.nextElement();
231                 loadObjectRevision(accessToken, token, uri,
232                                    revisionDefinition);
233                 
234             }
235             
236             if (!revisionDefinitionsFound) {
237                 loadDefaultObjectRevision(accessToken, token, uri);
238             }
239             
240             
241         } catch (ObjectNotFoundException e) {
242             // Should NEVER happen
243
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
244             accessToken.getLogger().log
245                 (e.toString(),LOG_CHANNEL,Logger.WARNING);
246         } catch (VetoException e) {
247             accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
248             accessToken.getLogger().log(e.toString(),LOG_CHANNEL,Logger.WARNING);
249         } catch (LinkedObjectNotFoundException e) {
250             // Icorrect link
251
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
252             accessToken.getLogger().log
253                 ("Incorrect link found while creating " + uri,
254                  LOG_CHANNEL,Logger.WARNING);
255         } catch (AccessDeniedException e) {
256             // Security exception
257
accessToken.getLogger().log
258                 ("Insufficient credentials to create object",
259                  LOG_CHANNEL,Logger.INFO);
260         } catch (ObjectLockedException e) {
261             // Lock exception
262
accessToken.getLogger().log
263                 ("Lock-Token required",
264                  LOG_CHANNEL,Logger.INFO);
265         }
266         
267         
268         Enumeration JavaDoc childConfigurations =
269             objectDefinition.getConfigurations("objectnode");
270         
271         while(childConfigurations.hasMoreElements()) {
272             Configuration childConfiguration =
273                 (Configuration) childConfigurations.nextElement();
274             loadObjectNode(accessToken, token, childConfiguration);
275         }
276         
277     }
278     
279     
280     
281     /**
282      * Create the SlideProperties object associated with a ObjectNode.
283      *
284      * @param slideObject ObjectNode
285      * @param propertiesDef Castor object describing the properties associated
286      * with the ObjectNode
287      * @exception SlideException A data access error occured
288      */

289     private static void loadDefaultObjectRevision(NamespaceAccessToken accessToken,
290                                                   SlideToken token, String JavaDoc uri)
291         throws ServiceAccessException, ConfigurationException,
292         AccessDeniedException, ObjectNotFoundException,
293         LinkedObjectNotFoundException, VetoException {
294         
295         
296         try {
297             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
298             factory.setNamespaceAware(false);
299             factory.setValidating(false);
300             SAXParser JavaDoc parser = factory.newSAXParser();
301             Populate pop = new Populate();
302             Configuration slideConfiguration =
303                 new ConfigurationElement(
304                 pop.load(
305                                             new InputSource JavaDoc(
306                             new StringReader JavaDoc("<revision/>")), parser.getXMLReader()));
307             loadObjectRevision(accessToken, token, uri, slideConfiguration);
308         }
309         catch (IOException JavaDoc e) { e.printStackTrace(); }
310         catch (SAXException JavaDoc e) { e.printStackTrace(); }
311         catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {e.printStackTrace(); }
312     }
313     
314     
315     
316     /**
317      * Create the SlideProperties object associated with a ObjectNode.
318      *
319      * @param slideObject ObjectNode
320      * @param propertiesDef Castor object describing the properties associated
321      * with the ObjectNode
322      * @exception SlideException A data access error occured
323      */

324     private static void loadObjectRevision(NamespaceAccessToken accessToken,
325                                            SlideToken token, String JavaDoc uri,
326                                            Configuration revisionDefinition)
327         throws ServiceAccessException, ConfigurationException,
328         AccessDeniedException, ObjectNotFoundException,
329         LinkedObjectNotFoundException, VetoException {
330         
331         // Retrieving the list of properties
332
Enumeration JavaDoc propertyDefinitions =
333             revisionDefinition.getConfigurations("property");
334         
335         // Now creating the new revision descriptor object
336
NodeRevisionDescriptor revisionDescriptor = null;
337         
338         NodeRevisionDescriptors revisionDescriptors = null;
339         try {
340             revisionDescriptors = accessToken.getContentHelper().retrieve
341                 (token, uri);
342         } catch (ObjectLockedException e) {
343             // Ignore
344
}
345         if ((revisionDescriptors != null)
346             && (revisionDescriptors.hasRevisions())) {
347             try {
348                 revisionDescriptor = accessToken.getContentHelper().retrieve
349                     (token, revisionDescriptors);
350             } catch (RevisionDescriptorNotFoundException e) {
351                 // Ignore
352
} catch (ObjectLockedException e) {
353                 // Ignore
354
}
355         }
356         if (revisionDescriptor == null) {
357             revisionDescriptor = new NodeRevisionDescriptor(0);
358         }
359         
360         while (propertyDefinitions.hasMoreElements()) {
361             Configuration propertyDefinition =
362                 (Configuration) propertyDefinitions.nextElement();
363             String JavaDoc propertyName = propertyDefinition.getAttribute("name");
364             String JavaDoc propertyValue = propertyDefinition.getValue();
365             String JavaDoc propertyNamespace = propertyDefinition.getAttribute
366                 ("namespace", NodeProperty.DEFAULT_NAMESPACE);
367             NodeProperty property = revisionDescriptor.getProperty
368                 (propertyName, propertyNamespace);
369             if (property == null)
370                 revisionDescriptor.setProperty(propertyName, propertyNamespace,
371                                                propertyValue);
372         }
373         
374         NodeRevisionContent revisionContent = null;
375         
376         if ((revisionDescriptors != null)
377             && (revisionDescriptors.hasRevisions())) {
378             
379             try {
380                 revisionContent = accessToken.getContentHelper()
381                     .retrieve(token, uri, revisionDescriptor);
382             } catch (RevisionContentNotFoundException e) {
383                 // Ignore
384
} catch (ObjectLockedException e) {
385                 // Ignore
386
} catch (RevisionNotFoundException e) {
387                 // No content for this revision
388
}
389             try {
390                 accessToken.getContentHelper().store
391                     (token, uri, revisionDescriptor, revisionContent);
392             } catch (RevisionDescriptorNotFoundException e) {
393                 // Should not happen
394
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
395                 accessToken.getLogger().log
396                     (e.toString(),LOG_CHANNEL,Logger.WARNING);
397             } catch (RevisionNotFoundException e) {
398                 // Should not happen
399
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
400                 accessToken.getLogger().log
401                     (e.toString(),LOG_CHANNEL,Logger.WARNING);
402             } catch (ObjectLockedException e) {
403                 // Ignore
404
}
405             
406         } else {
407             
408             try {
409                 accessToken.getContentHelper().create
410                     (token, uri, revisionDescriptor, revisionContent);
411             } catch(ObjectLockedException e) {
412                 // Should not happen
413
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
414                 accessToken.getLogger().log
415                     (e.toString(),LOG_CHANNEL,Logger.WARNING);
416             } catch(RevisionAlreadyExistException e) {
417                 // Should not happen
418
accessToken.getLogger().log(e,LOG_CHANNEL,Logger.WARNING);
419                 accessToken.getLogger().log
420                     (e.toString(),LOG_CHANNEL,Logger.WARNING);
421             }
422         }
423     }
424 }
425
Popular Tags