KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > properties > PropertyFactory


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/properties/PropertyFactory.java,v 1.1.2.2 2004/10/08 15:04:36 luetzkendorf Exp $
3  * $Revision: 1.1.2.2 $
4  * $Date: 2004/10/08 15:04:36 $
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.webdav.lib.properties;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.webdav.lib.BaseProperty;
31 import org.apache.webdav.lib.Property;
32 import org.apache.webdav.lib.ResponseEntity;
33 import org.apache.webdav.lib.WebdavException;
34 import org.w3c.dom.Element JavaDoc;
35
36
37 /**
38  * Factory for instanciating {@link org.apache.webdav.lib.Property}s.
39  */

40 public class PropertyFactory
41 {
42     private static final Class JavaDoc[] CTOR_PARAMS = { ResponseEntity.class, Element JavaDoc.class };
43     
44     /**
45      * Maps namespace URIs to maps, that map names to constructors.
46      */

47     private static Map JavaDoc propertyClasses = new HashMap JavaDoc();
48     
49     static {
50         try {
51             PropertyFactory.register("DAV:", AclProperty.TAG_NAME, AclProperty.class);
52             PropertyFactory.register("DAV:", CheckedinProperty.TAG_NAME, CheckedinProperty.class);
53             PropertyFactory.register("DAV:", CheckedoutProperty.TAG_NAME, CheckedoutProperty.class);
54             PropertyFactory.register("DAV:", CreationDateProperty.TAG_NAME, CreationDateProperty.class);
55             PropertyFactory.register("DAV:", CurrentUserPrivilegeSetProperty.TAG_NAME, CurrentUserPrivilegeSetProperty.class);
56             PropertyFactory.register("DAV:", GetContentLengthProperty.TAG_NAME, GetContentLengthProperty.class);
57             PropertyFactory.register("DAV:", GetLastModifiedProperty.TAG_NAME, GetLastModifiedProperty.class);
58             PropertyFactory.register("DAV:", LockDiscoveryProperty.TAG_NAME, LockDiscoveryProperty.class);
59             PropertyFactory.register("DAV:", ModificationDateProperty.TAG_NAME, ModificationDateProperty.class);
60             PropertyFactory.register("DAV:", OwnerProperty.TAG_NAME, OwnerProperty.class);
61             PropertyFactory.register("DAV:", PrincipalCollectionSetProperty.TAG_NAME, PrincipalCollectionSetProperty.class);
62             PropertyFactory.register("DAV:", ResourceTypeProperty.TAG_NAME, ResourceTypeProperty.class);
63             PropertyFactory.register("DAV:", SupportedLockProperty.TAG_NAME, SupportedLockProperty.class);
64         } catch (Exception JavaDoc e) {
65             throw new WebdavException(e);
66         }
67     }
68     
69     /**
70      * Creates a new property from an xml element provided in an WebDAV response.
71      *
72      * <p>If no property class was registered a {@link BaseProperty} will returned.
73      *
74      * @see Property
75      * @see BaseProperty
76      */

77     public static Property create(ResponseEntity response, Element JavaDoc element)
78     {
79         // look for the namespace
80
Map JavaDoc nsMap = (Map JavaDoc)propertyClasses.get(element.getNamespaceURI());
81         if (nsMap != null) {
82             // look up for name
83
Constructor JavaDoc ctor = (Constructor JavaDoc)nsMap.get(element.getLocalName());
84             if (ctor != null) {
85                 try {
86                     // try to create
87
Object JavaDoc[] params = {response, element};
88                     return (Property)ctor.newInstance(params);
89                 } catch (Exception JavaDoc e) {
90                     throw new WebdavException(e);
91                 }
92             }
93         }
94         
95         return new BaseProperty(response, element);
96     };
97     
98     /**
99      * Registers a new property.
100      *
101      * @param namespaceUri namespace of the property
102      * @param elmentName name of the property
103      * @param cls class that implements the property. Must have a constructor that
104      * takes two parameters of type ResponseEntity and Element.
105      *
106      * @throws NoSuchMethodException if cls does not implement the required ctor.
107      */

108     public static void register(String JavaDoc namespaceUri, String JavaDoc elementName, Class JavaDoc cls)
109         throws NoSuchMethodException JavaDoc, SecurityException JavaDoc
110     {
111         Constructor JavaDoc ctor = cls.getConstructor(CTOR_PARAMS);
112         
113         Map JavaDoc nsMap = (Map JavaDoc)propertyClasses.get(namespaceUri);
114         if (nsMap == null) {
115             nsMap = new HashMap JavaDoc();
116             propertyClasses.put(namespaceUri, nsMap);
117         }
118         nsMap.put(elementName, ctor);
119     }
120 }
121
Popular Tags