KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > txfile > AbstractXMLResourceDescriptor


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/txfile/AbstractXMLResourceDescriptor.java,v 1.3.2.2 2004/11/08 10:49:34 luetzkendorf Exp $
3  * $Revision: 1.3.2.2 $
4  * $Date: 2004/11/08 10:49:34 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 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.store.txfile;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.lang.reflect.Constructor JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 import org.apache.slide.common.*;
39 import org.apache.slide.lock.LockTokenNotFoundException;
40 import org.apache.slide.lock.NodeLock;
41 import org.apache.slide.security.NodePermission;
42 import org.apache.slide.structure.*;
43 import org.apache.slide.content.*;
44
45 import org.jdom.*;
46 import org.jdom.input.*;
47 import org.jdom.output.*;
48
49 /**
50  * Abstract class for encode all meta info of a resource into XML.
51  *
52  * Takes over very much code from UriProperties and AbstractUriProperties, that's why Marc is listed as author as well.
53  *
54  * @see FileResourceManager
55  * @see TxXMLFileDescriptorsStore
56  */

57 public abstract class AbstractXMLResourceDescriptor {
58
59     protected final Format JavaDoc outputFormat;
60     protected Object JavaDoc txId;
61     protected String JavaDoc uri;
62     protected SimpleDateFormat JavaDoc dateFormat;
63     protected boolean registeredForSaving = false;
64
65     /** Stored object.*/
66     protected ObjectNode object;
67
68     /** Permissions vector. */
69     protected Vector JavaDoc permissions;
70
71     /** Locks vector.*/
72     protected Vector JavaDoc locks;
73
74     /** Revision descriptors.*/
75     protected NodeRevisionDescriptors revisionDescriptors;
76
77     /** Revision descriptor hashtable.*/
78     protected Hashtable JavaDoc descriptor;
79
80
81     protected static String JavaDoc booleanToString(boolean aBoolean) {
82         return aBoolean ? "true" : "false";
83     }
84
85     protected static Element createBindings(String JavaDoc aParent, String JavaDoc aChild, Enumeration JavaDoc aEnum) {
86         Element aElement = new Element(aParent);
87         Element childNode;
88         ObjectNode.Binding binding;
89         while (aEnum.hasMoreElements()) {
90             binding = (ObjectNode.Binding) aEnum.nextElement();
91             childNode = new Element(aChild);
92             childNode.setAttribute(new Attribute("name", binding.getName()));
93             childNode.setAttribute(new Attribute("uuri", binding.getUuri()));
94             aElement.addContent(childNode);
95         }
96         return aElement;
97     }
98
99     protected static Element createElements(String JavaDoc aParent, String JavaDoc aChild, Enumeration JavaDoc aEnum) {
100         Element aElement = new Element(aParent);
101         while (aEnum.hasMoreElements()) {
102             Object JavaDoc aObject = aEnum.nextElement();
103             Element aItem = new Element(aChild);
104             aItem.setAttribute("val", aObject.toString());
105             aElement.addContent(aItem);
106         }
107         return aElement;
108     }
109
110     protected static NodePermission decodePermission(Element aElement, String JavaDoc aUri) {
111         String JavaDoc aRevisionNumber = aElement.getAttributeValue("revisionNumber");
112         String JavaDoc aSubject = aElement.getAttributeValue("subjectUri");
113         String JavaDoc aAction = aElement.getAttributeValue("actionUri");
114         boolean aInheritable = new Boolean JavaDoc(aElement.getAttributeValue("inheritable")).booleanValue();
115         boolean aNegative = new Boolean JavaDoc(aElement.getAttributeValue("negative")).booleanValue();
116         return new NodePermission(aUri, aRevisionNumber, aSubject, aAction, aInheritable, aNegative);
117
118     }
119
120     protected static Element encodeNodePermission(NodePermission aPermission) {
121         Element aElementPermission = new Element("permission");
122         NodeRevisionNumber aRevisionNumber = aPermission.getRevisionNumber();
123         if (aRevisionNumber != null) {
124             aElementPermission.setAttribute("revisionNumber", encodeRevisionNumber(aRevisionNumber));
125         }
126         aElementPermission.setAttribute("subjectUri", aPermission.getSubjectUri());
127         aElementPermission.setAttribute("actionUri", aPermission.getActionUri());
128         aElementPermission.setAttribute("inheritable", booleanToString(aPermission.isInheritable()));
129         aElementPermission.setAttribute("negative", booleanToString(aPermission.isNegative()));
130         return aElementPermission;
131     }
132
133     protected static Element encodeRevisionDescriptor(NodeRevisionDescriptor aDescriptor) {
134         Element aRevisions = new Element("revisions");
135         aRevisions.setAttribute("branchName", aDescriptor.getBranchName());
136         aRevisions.setAttribute("number", encodeRevisionNumber(aDescriptor.getRevisionNumber()));
137         aRevisions.addContent(createElements("labels", "label", aDescriptor.enumerateLabels()));
138         Element aProperties = new Element("properties");
139
140         for (Enumeration JavaDoc aEnum = aDescriptor.enumerateProperties(); aEnum.hasMoreElements();) {
141             Object JavaDoc aObject = aEnum.nextElement();
142             // System.out.println("---------- encodeRevisionDescriptor aObject="+aObject+" "+aObject.getClass().getName());
143
NodeProperty aProp = (NodeProperty) aObject;
144             aProperties.addContent(encodeNodeProperty(aProp));
145         }
146         aRevisions.addContent(aProperties);
147         return aRevisions;
148     }
149
150     protected static Element encodeNodeProperty(NodeProperty aProp) {
151         Element aElement = new Element("property");
152         aElement.setAttribute("name", aProp.getName());
153         aElement.setAttribute("namespace", aProp.getNamespace());
154         aElement.setAttribute("value", aProp.getValue().toString());
155         aElement.setAttribute("type", aProp.getType());
156         aElement.setAttribute("protected", booleanToString(aProp.isProtected()));
157         Element aPermissions = new Element("permissions");
158
159         for (Enumeration JavaDoc aEnum = aProp.enumeratePermissions(); aEnum.hasMoreElements();) {
160             NodePermission aPermission = (NodePermission) aEnum.nextElement();
161             aPermissions.addContent(encodeNodePermission(aPermission));
162         }
163         aElement.addContent(aPermissions);
164         return aElement;
165     }
166
167     protected static Vector JavaDoc createVector(Element aElement, String JavaDoc aParentName, String JavaDoc aChildName) {
168         Element aParent = aElement.getChild(aParentName);
169         Vector JavaDoc aRet = new Vector JavaDoc();
170         // System.out.println("--------- createVector aParentName="+aParentName+" aChildName="+aChildName);
171
List JavaDoc aList = aParent.getChildren(aChildName);
172         // System.out.println("--------- createVector aList="+aList);
173
for (int i = 0; i < aList.size(); i++) {
174             Element aChild = (Element) aList.get(i);
175             aRet.addElement(aChild.getAttributeValue("val"));
176         }
177         return aRet;
178     }
179
180     protected static Vector JavaDoc createBindingVector(
181         Element aElement,
182         String JavaDoc aParentName,
183         String JavaDoc aChildName,
184         boolean parentBindings) {
185         Element aParent = aElement.getChild(aParentName);
186         Vector JavaDoc aRet = new Vector JavaDoc();
187         // System.out.println("--------- createVector aParentName="+aParentName+" aChildName="+aChildName);
188
Iterator JavaDoc it = aParent.getChildren().iterator();
189         while (it.hasNext()) {
190             Element aChild = (Element) it.next();
191             String JavaDoc name = aChild.getAttributeValue("name");
192             String JavaDoc uuri = aChild.getAttributeValue("uuri");
193             if (parentBindings) {
194                 aRet.add(new ObjectNode.ParentBinding(name, uuri));
195             } else {
196                 aRet.add(new ObjectNode.Binding(name, uuri));
197             }
198         }
199         return aRet;
200     }
201
202     protected static String JavaDoc encodeRevisionNumber(NodeRevisionNumber aRevisionNumber) {
203         return aRevisionNumber.getMajor() + "." + aRevisionNumber.getMinor();
204     }
205
206     protected static Object JavaDoc createObject(String JavaDoc aNomClasse, Class JavaDoc aTypes[], Object JavaDoc aArgs[])
207         throws UnknownObjectClassException {
208         Class JavaDoc aClasse = null;
209         try {
210             // First, load the object's class
211
aClasse = Class.forName(aNomClasse);
212             Constructor JavaDoc aConstructor = aClasse.getConstructor(aTypes);
213             if (aConstructor == null)
214                 aConstructor = aClasse.getSuperclass().getConstructor(aTypes);
215             return aConstructor.newInstance(aArgs);
216
217         } catch (Exception JavaDoc e) {
218             throw new UnknownObjectClassException(aNomClasse);
219         }
220     }
221
222     protected static NodeRevisionNumber decodeRevisionNumber(Element aElement) {
223         Element aElementRevision = aElement.getChild("revision");
224         return new NodeRevisionNumber(
225             Integer.parseInt(aElementRevision.getAttributeValue("major")),
226             Integer.parseInt(aElementRevision.getAttributeValue("minor")));
227     }
228
229     protected static NodeRevisionNumber decodeRevisionNumber(String JavaDoc aStr) {
230         return (aStr == null ? null : new NodeRevisionNumber(aStr));
231     }
232
233     protected static NodeProperty decodeNodeProperty(Element aElement, String JavaDoc aUri) {
234         String JavaDoc aName = aElement.getAttributeValue("name");
235         String JavaDoc aValue = aElement.getAttributeValue("value");
236         String JavaDoc aNamespace = aElement.getAttributeValue("namespace");
237         String JavaDoc aType = aElement.getAttributeValue("type");
238         boolean aProtected = new Boolean JavaDoc(aElement.getAttributeValue("protected")).booleanValue();
239
240         Element aPermisionsElement = aElement.getChild("permissions");
241         List JavaDoc aList = aPermisionsElement.getChildren();
242         Vector JavaDoc aPermission = new Vector JavaDoc();
243         for (int i = 0; i < aList.size(); i++) {
244             Element aChild = (Element) aList.get(i);
245             aPermission.addElement(decodePermission(aChild, aUri));
246         }
247         return new NodeProperty(aName, aValue, aNamespace, aType, aProtected);
248     }
249
250     /**
251      * Creates an XML descriptor resource.
252      *
253      * @param uri uri of the resource
254      * @param txId identifier for the transaction in which the descriptor is to be managed
255      * @param characterEncoding charcter enconding used to store this descriptor in XML
256      * @throws ServiceAccessException if anything goes wrong at system level
257      */

258     public AbstractXMLResourceDescriptor(
259         Uri uri,
260         Object JavaDoc txId,
261         String JavaDoc characterEncoding)
262         throws ServiceAccessException {
263
264         outputFormat = Format.getPrettyFormat();
265         outputFormat.setEncoding(characterEncoding);
266
267         dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yyyy HH:mm:ss z");
268
269         this.txId = txId;
270
271         if (uri == null) {
272             throw new ServiceAccessException(null, "Trying to initialize XMLResourceDescriptor with null URI");
273         }
274         this.uri = uri.toString();
275     }
276
277     // -------------- PART TAKE OVER FROM AbstractUriProperties START --------------
278

279     /**
280      * Retrive an object from the Descriptors Store.
281      *
282      * @exception ServiceAccessException Error accessing the Descriptors Store
283      * @exception ObjectNotFoundException The object to retrieve was not found
284      */

285     public ObjectNode retrieveObject() throws ServiceAccessException, ObjectNotFoundException {
286         if (object == null) {
287             throw new ObjectNotFoundException(uri);
288         }
289         return object.cloneObject();
290     }
291
292     /**
293     * Store an object in the Descriptors Store.
294     *
295     * @param object Object to update
296     * @exception ServiceAccessException Error accessing the Descriptors Store
297     * @exception ObjectNotFoundException The object to update was not found
298     */

299     public void storeObject(ObjectNode aObject) throws ServiceAccessException, ObjectNotFoundException {
300         object = aObject.cloneObject();
301     }
302
303     /**
304     * Remove an object from the Descriptors Store.
305     *
306     * @param object Object to remove
307     * @exception ServiceAccessException Error accessing the Descriptors Store
308     * @exception ObjectNotFoundException The object to remove was not found
309     */

310     public void removeObject(ObjectNode aObject) throws ServiceAccessException, ObjectNotFoundException {
311         object = null;
312     }
313
314     /**
315     * Store an object permissions in the Descriptors Store.
316     *
317     * @param permission Permission we want to create
318     * @exception ServiceAccessException Error accessing the Descriptors Store
319     */

320     public void grantPermission(NodePermission permission) throws ObjectNotFoundException, ServiceAccessException {
321         if (permissions == null)
322             permissions = new Vector JavaDoc();
323         permissions.addElement(permission.cloneObject());
324     }
325
326     /**
327     * Store an object permissions in the Descriptors Store.
328     *
329     * @param permission Permission we want to create
330     * @exception ServiceAccessException Error accessing the Descriptors Store
331     */

332     public void revokePermission(NodePermission permission) throws ObjectNotFoundException, ServiceAccessException {
333         if (permissions != null)
334             permissions.removeElement(permission);
335     }
336
337     /**
338     * Revoke all the permissions on the object .
339     *
340     * @param permission Permission we want to create
341     * @exception ServiceAccessException Error accessing the Descriptors Store
342     */

343     public void revokePermissions() throws ObjectNotFoundException, ServiceAccessException {
344         if (permissions != null)
345             permissions.removeAllElements();
346     }
347
348     /**
349     * Store an object permissions in the Descriptors Store.
350     *
351     * @param permission Permission we want to create
352     * @exception ServiceAccessException Error accessing the Descriptors Store
353     */

354     public Enumeration JavaDoc enumeratePermissions() throws ServiceAccessException {
355         if (permissions == null)
356             permissions = new Vector JavaDoc();
357         return permissions.elements();
358     }
359
360     /**
361     * Puts a lock on a subject.
362     *
363     * @param lock Lock token
364     * @exception ServiceAccessException Service access error
365     */

366     public void putLock(NodeLock lock) throws ObjectNotFoundException, ServiceAccessException {
367         if (locks == null)
368             locks = new Vector JavaDoc();
369         locks.addElement(lock.cloneObject());
370     }
371
372     /**
373     * Renews a lock.
374     *
375     * @param lock Token to renew
376     * @exception ServiceAccessException Service access error
377     * @exception LockTokenNotFoundException Lock token was not found
378     */

379     public void renewLock(NodeLock lock) throws LockTokenNotFoundException, ObjectNotFoundException, ServiceAccessException {
380         if (locks == null)
381             locks = new Vector JavaDoc();
382         boolean wasPresent = locks.removeElement(lock);
383         if (!wasPresent) {
384             throw new LockTokenNotFoundException(lock);
385         }
386         locks.addElement(lock.cloneObject());
387     }
388
389     /**
390     * Removes (cancels) a lock.
391     *
392     * @param lock Token to remove
393     * @exception ServiceAccessException Service access error
394     * @exception LockTokenNotFoundException Lock token was not found
395     */

396     public void removeLock(NodeLock lock) throws LockTokenNotFoundException, ObjectNotFoundException, ServiceAccessException {
397
398         if (locks == null) {
399             throw new LockTokenNotFoundException(lock);
400         }
401         boolean wasPresent = locks.removeElement(lock);
402         if (!wasPresent) {
403             throw new LockTokenNotFoundException(lock);
404         }
405     }
406
407     /**
408     * Returns the list of locks put on a subject.
409     *
410     * @param subject Subject
411     * @return Enumeration List of locks which have been put on the subject
412     * @exception ServiceAccessException Service access error
413     */

414     public Enumeration JavaDoc enumerateLocks() throws ServiceAccessException {
415         if (locks == null)
416             locks = new Vector JavaDoc();
417         return locks.elements();
418     }
419
420     /**
421     * Retrieve a revision descriptors.
422     *
423     * @exception ServiceAccessException Service access error
424     * @exception RevisionDescriptorNotFoundException Revision descriptor
425     * was not found
426     */

427     public NodeRevisionDescriptors retrieveRevisionDescriptors()
428         throws ServiceAccessException, RevisionDescriptorNotFoundException {
429         if (revisionDescriptors == null) {
430             throw new RevisionDescriptorNotFoundException(uri.toString());
431         }
432         return revisionDescriptors.cloneObject();
433     }
434
435     /**
436     * Create new revision descriptors.
437     *
438     * @param revisionDescriptors Node revision descriptors
439     * @exception ServiceAccessException Service access error
440     */

441     public void createRevisionDescriptors(NodeRevisionDescriptors aRevisionDescriptors)
442         throws ObjectNotFoundException, ServiceAccessException {
443         revisionDescriptors = aRevisionDescriptors.cloneObject();
444     }
445
446     /**
447     * Update revision descriptors.
448     *
449     * @param revisionDescriptors Node revision descriptors
450     * @exception ServiceAccessException Service access error
451     * @exception RevisionDescriptorNotFoundException Revision descriptor
452     * was not found
453     */

454     public void storeRevisionDescriptors(NodeRevisionDescriptors aRevisionDescriptors)
455         throws RevisionDescriptorNotFoundException, ObjectNotFoundException, ServiceAccessException {
456         if (!revisionDescriptors.getUri().equals(uri.toString())) {
457             throw new RevisionDescriptorNotFoundException(uri.toString());
458         }
459         revisionDescriptors = aRevisionDescriptors.cloneObject();
460     }
461
462     /**
463     * Remove revision descriptors.
464     *
465     * @exception ServiceAccessException Service access error
466     */

467     public void removeRevisionDescriptors() throws ObjectNotFoundException, ServiceAccessException {
468         revisionDescriptors = null;
469     }
470
471     /**
472     * Retrieve revision descriptor.
473     *
474     * @param revisionNumber Node revision number
475     */

476     public NodeRevisionDescriptor retrieveRevisionDescriptor(NodeRevisionNumber revisionNumber)
477         throws ServiceAccessException, RevisionDescriptorNotFoundException {
478         Object JavaDoc result = null;
479
480         if (descriptor != null && revisionNumber != null)
481             result = descriptor.get(revisionNumber.toString());
482
483         if (result == null) {
484             throw new RevisionDescriptorNotFoundException(uri.toString());
485         }
486         return ((NodeRevisionDescriptor) result).cloneObject();
487     }
488
489     /**
490     * Create new revision descriptor.
491     *
492     * @param revisionDescriptor Node revision descriptor
493     * @exception ServiceAccessException Service access error
494     */

495     public void createRevisionDescriptor(NodeRevisionDescriptor aRevisionDescriptor)
496         throws ObjectNotFoundException, ServiceAccessException {
497         if (descriptor == null)
498             descriptor = new Hashtable JavaDoc();
499
500         descriptor.put(aRevisionDescriptor.getRevisionNumber().toString(), aRevisionDescriptor.cloneObject());
501     }
502
503     /**
504     * Update revision descriptor.
505     *
506     * @param revisionDescriptors Node revision descriptor
507     * @exception ServiceAccessException Service access error
508     * @exception RevisionDescriptorNotFoundException Revision descriptor
509     * was not found
510     */

511     public void storeRevisionDescriptor(NodeRevisionDescriptor aRevisionDescriptor)
512         throws RevisionDescriptorNotFoundException, ObjectNotFoundException, ServiceAccessException {
513         String JavaDoc key = aRevisionDescriptor.getRevisionNumber().toString();
514         if (descriptor == null || !descriptor.containsKey(key)) {
515             throw new RevisionDescriptorNotFoundException(uri.toString());
516         }
517         descriptor.put(key, aRevisionDescriptor.cloneObject());
518     }
519
520     /**
521      * Remove revision descriptor.
522      *
523      * @param revisionNumber Revision number
524      * @exception ServiceAccessException Service access error
525      */

526     public void removeRevisionDescriptor(NodeRevisionNumber number) throws ObjectNotFoundException, ServiceAccessException {
527         if (descriptor == null)
528             return;
529
530         descriptor.remove(number.toString());
531     }
532
533     // -------------- PART TAKE OVER FROM AbstractUriProperties END --------------
534

535     public void registerForSaving() {
536         registeredForSaving = true;
537     }
538
539     public boolean isRegisteredForSaving() {
540         return registeredForSaving;
541     }
542
543     /**
544      * Stores this descriptor to the resource manager.
545      *
546      * @throws ServiceAccessException if anything goes wrong at system level
547      * @throws ObjectNotFoundException if the descriptor has not been created before
548      */

549     public abstract void save() throws ServiceAccessException, ObjectNotFoundException;
550
551     /**
552      * Loads this descriptor from the resource manager.
553      *
554      * @throws ServiceAccessException if anything goes wrong at system level
555      * @throws ObjectNotFoundException if the descriptor does not exist
556      */

557     public abstract void load() throws ServiceAccessException, ObjectNotFoundException;
558
559     /**
560      * Creates this descriptor in the resource manager.
561      *
562      * @throws ServiceAccessException if anything goes wrong at system level
563      * @throws ObjectAlreadyExistsException if the descriptor already exists
564      */

565     public abstract void create() throws ServiceAccessException, ObjectAlreadyExistsException;
566
567     /**
568      * Deletes this descriptor from the resource manager.
569      *
570      * @throws ServiceAccessException if anything goes wrong at system level
571      * @throws ObjectNotFoundException if the descriptor does not exist
572      */

573     public abstract void delete() throws ServiceAccessException, ObjectNotFoundException;
574
575     /**
576      * Gets the URI of this descriptor.
577      *
578      * @return the URI
579      */

580     public String JavaDoc getUri() {
581         return uri;
582     }
583
584     /**
585      * Gets the transaction this descriptor lives in.
586      *
587      * @return the transaction identifier
588      */

589     public Object JavaDoc getTxId() {
590         return txId;
591     }
592
593     /**
594      * Checks if the specified object is a descriptor with the same URI in the same transaction.
595      *
596      *
597      * @param o object to compare this descriptor to
598      * @return <code>true</code> if object is equal as described above
599      */

600     public boolean equals(Object JavaDoc o) {
601         return (
602             this == o
603                 || (o != null
604                     && o instanceof XMLResourceDescriptor
605                     && ((XMLResourceDescriptor) o).uri.equals(uri)
606                     && ((XMLResourceDescriptor) o).txId.equals(txId)));
607     }
608
609     public String JavaDoc toString() {
610         return txId + ": " + uri;
611     }
612
613     protected void save(OutputStream JavaDoc os) throws ServiceAccessException, IOException JavaDoc {
614         Element aRoot = encode();
615         Document aDocument = new Document(aRoot);
616
617         XMLOutputter aOutputter = new XMLOutputter(outputFormat);
618         aOutputter.output(aDocument, os);
619         os.flush();
620     }
621
622     protected void load(InputStream JavaDoc is) throws ServiceAccessException, JDOMException, IOException JavaDoc {
623         SAXBuilder aBuilder = new SAXBuilder();
624         Document aDocument = aBuilder.build(is);
625         decode(aDocument.getRootElement());
626     }
627
628     protected void init() throws ServiceAccessException {
629         // need to set this null, as AbstractUriProperties.retrieveObject relies on it
630
object = null;
631         permissions = new Vector JavaDoc();
632         locks = new Vector JavaDoc();
633         revisionDescriptors =
634             new NodeRevisionDescriptors(uri, null, new Hashtable JavaDoc(), new Hashtable JavaDoc(), new Hashtable JavaDoc(), false);
635
636         descriptor = new Hashtable JavaDoc();
637     }
638
639     protected Element encode() throws ServiceAccessException {
640         Element aRoot = new Element("data");
641         aRoot.addContent(encodeObject());
642         aRoot.addContent(encodePermissions());
643         aRoot.addContent(encodeLocks());
644         aRoot.addContent(encodeRevisionDescriptors());
645         aRoot.addContent(encodeRevisionDescriptor());
646         return aRoot;
647     }
648
649     protected Element encodeObject() {
650         Element aElementObjectNode = new Element("objectnode");
651         if (object != null) {
652             aElementObjectNode.setAttribute("classname", object.getClass().getName());
653             aElementObjectNode.setAttribute("uri", object.getUri());
654             if (object instanceof LinkNode) {
655                 aElementObjectNode.setAttribute("linkTo", ((LinkNode) object).getLinkedUri());
656             }
657             aElementObjectNode.addContent(createBindings("children", "child", object.enumerateBindings()));
658             aElementObjectNode.addContent(createBindings("parents", "parent", object.enumerateParentBindings()));
659             aElementObjectNode.addContent(createElements("links", "link", object.enumerateLinks()));
660         } else {
661             // for null locks
662
aElementObjectNode.setAttribute("classname", "null");
663             aElementObjectNode.setAttribute("uri", uri.toString());
664         }
665         return aElementObjectNode;
666     }
667
668     protected Element encodePermissions() {
669         Element aPermissions = new Element("permissions");
670         if (permissions == null)
671             return aPermissions;
672
673         for (int aSize = permissions.size(), i = 0; i < aSize; i++) {
674             NodePermission aPermission = (NodePermission) permissions.elementAt(i);
675             aPermissions.addContent(encodeNodePermission(aPermission));
676         }
677         return aPermissions;
678     }
679
680     protected Element encodeLocks() {
681         Element aElementLocks = new Element("locks");
682         if (locks == null)
683             return aElementLocks;
684
685         for (int aSize = locks.size(), i = 0; i < aSize; i++) {
686             NodeLock aLock = (NodeLock) locks.elementAt(i);
687             Element aElementLock = new Element("lock");
688             aElementLock.setAttribute("subjectUri", aLock.getSubjectUri());
689             aElementLock.setAttribute("typeUri", aLock.getTypeUri());
690             aElementLock.setAttribute("date", dateFormat.format(aLock.getExpirationDate()));
691             aElementLock.setAttribute("inheritance", booleanToString(aLock.isInheritable()));
692             aElementLock.setAttribute("exclusive", booleanToString(aLock.isExclusive()));
693             aElementLock.setAttribute("lockId", aLock.getLockId());
694             aElementLock.setAttribute("owner",
695                   aLock.getOwnerInfo() == null ? "" : aLock.getOwnerInfo());
696             aElementLocks.addContent(aElementLock);
697         }
698         return aElementLocks;
699     }
700
701     protected Element encodeRevisionDescriptors() {
702
703         Element aRevisionsHistory = new Element("revisionsHistory");
704         if (revisionDescriptors == null)
705             return aRevisionsHistory;
706
707         aRevisionsHistory.setAttribute(
708             "initialRevision",
709             encodeRevisionNumber(revisionDescriptors.getInitialRevision()));
710         aRevisionsHistory.setAttribute("useVersioning", booleanToString(revisionDescriptors.isVersioned()));
711
712         // System.out.println("---------- encodeRevisionDescriptors getLatestRevision="+
713
// revisionDescriptors.getLatestRevision());
714

715         Element aBranchesElement = new Element("branches");
716         Enumeration JavaDoc aBranches = revisionDescriptors.enumerateBranchNames();
717         while (aBranches.hasMoreElements()) {
718             String JavaDoc aBranchName = (String JavaDoc) aBranches.nextElement();
719             Element aElementBranch = new Element("branch");
720             aElementBranch.setAttribute("name", aBranchName);
721             NodeRevisionNumber aRevisionNumber = revisionDescriptors.getLatestRevision(aBranchName);
722             aElementBranch.setAttribute("lastestRevision", encodeRevisionNumber(aRevisionNumber));
723             aBranchesElement.addContent(aElementBranch);
724         }
725         aRevisionsHistory.addContent(aBranchesElement);
726
727         Element aRevisionsElement = new Element("revisions");
728         Enumeration JavaDoc aRevisions = revisionDescriptors.enumerateRevisionNumbers();
729         while (aRevisions.hasMoreElements()) {
730             NodeRevisionNumber aRevisionNumber = (NodeRevisionNumber) aRevisions.nextElement();
731             Element aRevisionElement = new Element("branch");
732             aRevisionElement.setAttribute("start", encodeRevisionNumber(aRevisionNumber));
733
734             Enumeration JavaDoc aSuccessors = revisionDescriptors.getSuccessors(aRevisionNumber);
735             while (aSuccessors.hasMoreElements()) {
736                 NodeRevisionNumber aSuccessorRevisionNumber = (NodeRevisionNumber) aSuccessors.nextElement();
737                 Element aSuccessorRevisionElement = new Element("revision");
738                 aSuccessorRevisionElement.setAttribute("number", encodeRevisionNumber(aSuccessorRevisionNumber));
739                 aRevisionElement.addContent(aSuccessorRevisionElement);
740             }
741             aRevisionsElement.addContent(aRevisionElement);
742         }
743         aRevisionsHistory.addContent(aRevisionsElement);
744
745         return aRevisionsHistory;
746     }
747
748     protected Element encodeRevisionDescriptor() {
749         Element aRet = new Element("descriptor");
750         if (descriptor == null)
751             return aRet;
752
753         for (Enumeration JavaDoc aEnum = descriptor.elements(); aEnum.hasMoreElements();) {
754             NodeRevisionDescriptor aRevisionDescriptor = (NodeRevisionDescriptor) aEnum.nextElement();
755             aRet.addContent(encodeRevisionDescriptor(aRevisionDescriptor));
756         }
757         return aRet;
758     }
759
760     protected void decode(Element aRoot) throws ServiceAccessException {
761         decodeObject(aRoot);
762         decodePermissions(aRoot);
763         decodeLocks(aRoot);
764         decodeRevisionDescriptors(aRoot);
765         decodeRevisionDescriptor(aRoot);
766     }
767
768     protected void decodeObject(Element aElement) throws ServiceAccessException {
769         Element aElementObjectNode = aElement.getChild("objectnode");
770         String JavaDoc aClasseName = aElementObjectNode.getAttributeValue("classname");
771         if (!"null".equals(aClasseName)) {
772             try {
773                 String JavaDoc aUri = aElementObjectNode.getAttributeValue("uri");
774                 Vector JavaDoc aChilds = createBindingVector(aElementObjectNode, "children", "child", false);
775                 Vector JavaDoc aParents = createBindingVector(aElementObjectNode, "parents", "parent", true);
776                 Vector JavaDoc aLinks = createVector(aElementObjectNode, "links", "link");
777                 // System.out.println("--------- decodeObject aChilds="+aChilds);
778
// System.out.println("--------- decodeObject aLinks="+aLinks);
779
Class JavaDoc aTypes[] = null;
780                 Object JavaDoc aArgs[] = null;
781
782                 if (aClasseName.equals(LinkNode.class.getName())) {
783                     String JavaDoc aLinkTo = aElementObjectNode.getAttributeValue("linkTo");
784                     aTypes = new Class JavaDoc[] { String JavaDoc.class, Vector JavaDoc.class, Vector JavaDoc.class, String JavaDoc.class };
785                     aArgs = new Object JavaDoc[] { aUri, aChilds, aLinks, aLinkTo };
786                 } else {
787                     aTypes = new Class JavaDoc[] { String JavaDoc.class, Vector JavaDoc.class, Vector JavaDoc.class, Vector JavaDoc.class };
788                     aArgs = new Object JavaDoc[] { aUri, aChilds, aParents, aLinks };
789                 }
790                 object = (ObjectNode) createObject(aClasseName, aTypes, aArgs);
791                 object.setUri(object.getUuri());
792             } catch (Exception JavaDoc e) {
793                 e.printStackTrace();
794                 throw new ServiceAccessException(null, e);
795             }
796             uri = object.getUri();
797         } else {
798             object = null;
799             uri = aElementObjectNode.getAttributeValue("uri");
800         }
801     }
802
803     protected void decodePermissions(Element aElement) {
804         permissions = new Vector JavaDoc();
805         Element aPermissions = aElement.getChild("permissions");
806         List JavaDoc aList = aPermissions.getChildren();
807         for (int i = 0; i < aList.size(); i++) {
808             Element aChild = (Element) aList.get(i);
809             permissions.addElement(decodePermission(aChild, uri));
810         }
811     }
812
813     protected void decodeLocks(Element aElement) throws ServiceAccessException {
814         try {
815             locks = new Vector JavaDoc();
816             Element aElementLocks = aElement.getChild("locks");
817             List JavaDoc aList = aElementLocks.getChildren();
818
819             for (int i = 0; i < aList.size(); i++) {
820                 Element aChild = (Element) aList.get(i);
821                 String JavaDoc aSubject = aChild.getAttributeValue("subjectUri");
822                 String JavaDoc aType = aChild.getAttributeValue("typeUri");
823                 Date JavaDoc aDateExpiration = dateFormat.parse(aChild.getAttributeValue("date"));
824                 boolean aInheritable = new Boolean JavaDoc(aChild.getAttributeValue("inheritance")).booleanValue();
825                 boolean aNegative = new Boolean JavaDoc(aChild.getAttributeValue("exclusive")).booleanValue();
826                 String JavaDoc aLockId = aChild.getAttributeValue("lockId");
827                 String JavaDoc ownerInfo = aChild.getAttributeValue("owner");
828
829                 locks.addElement(
830                     new NodeLock(aLockId, uri, aSubject, aType, aDateExpiration, aInheritable, aNegative, ownerInfo));
831             }
832         } catch (Exception JavaDoc e) {
833             e.printStackTrace();
834             throw new ServiceAccessException(null, e);
835         }
836
837     }
838
839     protected void decodeRevisionDescriptors(Element aElement) {
840         Element aRevisionsHistory = aElement.getChild("revisionsHistory");
841
842         NodeRevisionNumber aInitialRevision =
843             decodeRevisionNumber(aRevisionsHistory.getAttributeValue("initialRevision"));
844         boolean aUseVersionning = new Boolean JavaDoc(aRevisionsHistory.getAttributeValue("useVersioning")).booleanValue();
845
846         Element aBranchesElement = aRevisionsHistory.getChild("branches");
847         if (aBranchesElement == null) {
848             revisionDescriptors =
849                 new NodeRevisionDescriptors(
850                     uri,
851                     aInitialRevision,
852                     new Hashtable JavaDoc(),
853                     new Hashtable JavaDoc(),
854                     new Hashtable JavaDoc(),
855                     aUseVersionning);
856             return;
857         }
858
859         List JavaDoc aList = aBranchesElement.getChildren();
860         Hashtable JavaDoc aLastestRevisions = new Hashtable JavaDoc();
861         for (int i = 0; i < aList.size(); i++) {
862             Element aChild = (Element) aList.get(i);
863             String JavaDoc aName = aChild.getAttributeValue("name");
864             NodeRevisionNumber aRevisionNumber = decodeRevisionNumber(aChild.getAttributeValue("lastestRevision"));
865             aLastestRevisions.put(aName, aRevisionNumber);
866         }
867         Hashtable JavaDoc aBranches = new Hashtable JavaDoc();
868         Element aRevisionsElement = aRevisionsHistory.getChild("revisions");
869         aList = aRevisionsElement.getChildren();
870         for (int i = 0; i < aList.size(); i++) {
871             Element aChild = (Element) aList.get(i);
872             NodeRevisionNumber aStartNumber = decodeRevisionNumber(aChild.getAttributeValue("start"));
873             List JavaDoc aSuccessors = aChild.getChildren();
874             Vector JavaDoc aSuccessorsNumbers = new Vector JavaDoc();
875             for (int k = 0; k < aSuccessors.size(); k++) {
876                 Element aSuccessor = (Element) aSuccessors.get(k);
877                 NodeRevisionNumber aRevisionNumber = decodeRevisionNumber(aSuccessor.getAttributeValue("number"));
878                 aSuccessorsNumbers.addElement(aRevisionNumber);
879             }
880             aBranches.put(aStartNumber, aSuccessorsNumbers);
881         }
882         revisionDescriptors =
883             new NodeRevisionDescriptors(
884                 uri,
885                 aInitialRevision,
886                 new Hashtable JavaDoc(),
887                 aLastestRevisions,
888                 aBranches,
889                 aUseVersionning);
890     }
891
892     protected void decodeRevisionDescriptor(Element aParent) {
893         descriptor = new Hashtable JavaDoc();
894
895         Element aElement = aParent.getChild("descriptor");
896         if (aElement == null)
897             return;
898
899         List JavaDoc aList = aElement.getChildren();
900
901         for (int i = 0; i < aList.size(); i++) {
902             Element aChild = (Element) aList.get(i);
903             String JavaDoc aBranchName = aChild.getAttributeValue("branchName");
904             NodeRevisionNumber aRevisionNumber = decodeRevisionNumber(aChild.getAttributeValue("number"));
905
906             Vector JavaDoc aLabels = new Vector JavaDoc();
907             Element aLabelsElement = (Element) aChild.getChild("labels");
908             List JavaDoc aLabelList = aLabelsElement.getChildren();
909             for (int k = 0; k < aLabelList.size(); k++) {
910                 Element aLabel = (Element) aLabelList.get(k);
911                 aLabels.addElement(aLabel.getAttributeValue("val"));
912             }
913
914             Hashtable JavaDoc aProperties = new Hashtable JavaDoc();
915             Element aPropertiesElement = (Element) aChild.getChild("properties");
916             List JavaDoc aPropertiesList = aPropertiesElement.getChildren();
917             for (int k = 0; k < aPropertiesList.size(); k++) {
918                 Element aProperty = (Element) aPropertiesList.get(k);
919                 NodeProperty aProp = decodeNodeProperty(aProperty, uri);
920                 String JavaDoc key = aProperty.getAttributeValue("namespace") + aProperty.getAttributeValue("name");
921                 aProperties.put(key, aProp);
922             }
923             NodeRevisionDescriptor aNode =
924                 new NodeRevisionDescriptor(aRevisionNumber, aBranchName, aLabels, aProperties);
925             descriptor.put(aRevisionNumber.toString(), aNode);
926         }
927     }
928
929 }
930
Popular Tags