View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.page.document.psml;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.jetspeed.page.document.DocumentHandler;
24  import org.apache.jetspeed.page.document.DocumentHandlerFactory;
25  import org.apache.jetspeed.page.document.DocumentTypeAlreadyRegisteredException;
26  import org.apache.jetspeed.page.document.UnsupportedDocumentTypeException;
27  import org.apache.jetspeed.util.ArgUtil;
28  
29  import org.apache.jetspeed.page.document.Node;
30  
31  /***
32   * <p>
33   * DocumentHandlerFactoryImpl
34   * </p>
35   * <p>
36   * 
37   * </p>
38   * 
39   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
40   * @version $Id: DocumentHandlerFactoryImpl.java 517124 2007-03-12 08:10:25Z ate $
41   *  
42   */
43  public class DocumentHandlerFactoryImpl implements DocumentHandlerFactory
44  {
45      private Map handlers;
46  
47      private boolean permissionsEnabled;
48  
49      private boolean constraintsEnabled;
50  
51  
52      /***
53       *  
54       */
55      public DocumentHandlerFactoryImpl( Map handlers )
56      {
57          super();
58          
59          ArgUtil.assertNotNull(Map.class, handlers, this);        
60          
61          this.handlers = handlers;        
62  
63          // register this with handlers
64          Iterator handlersIter = handlers.values().iterator();
65          while (handlersIter.hasNext())
66          {
67              ((DocumentHandler)handlersIter.next()).setHandlerFactory(this);
68          }
69      }
70      
71      public DocumentHandlerFactoryImpl()
72      {
73          this(new HashMap());
74               
75      }
76  
77      /***
78       * <p>
79       * getDocumentHandler
80       * </p>
81       * 
82       * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getDocumentHandler(java.lang.String)
83       * @param documentType
84       * @return
85       */
86      public DocumentHandler getDocumentHandler( String documentType ) throws UnsupportedDocumentTypeException
87      {        
88          if(handlers.containsKey(documentType))
89          {
90              return (DocumentHandler)handlers.get(documentType);
91          }
92          else
93          {
94              throw new UnsupportedDocumentTypeException("There are no DocumentHandlers defined for the type: "+documentType);
95          }
96      }
97  
98      /***
99       * <p>
100      * registerDocumentHandler
101      * </p>
102      *
103      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#registerDocumentHandler(org.apache.jetspeed.page.documenthandler.DocumentHandler)
104      * @param documentHandler
105      * @throws DocumentTypeAlreadyRegisteredException
106      */
107     public void registerDocumentHandler( DocumentHandler documentHandler ) throws DocumentTypeAlreadyRegisteredException
108     {
109         if(handlers.containsKey(documentHandler.getType()))
110         {
111             throw new DocumentTypeAlreadyRegisteredException(documentHandler.getType()+" has already been registered.");
112         }
113 
114         // register handler and this with handlers
115         documentHandler.setHandlerFactory(this);
116         handlers.put(documentHandler.getType(), documentHandler);
117     }
118 
119     /***
120      * <p>
121      * getDocumentHandlerForPath
122      * </p>
123      *
124      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getDocumentHandlerForPath(java.lang.String)
125      * @param documentPath
126      * @return
127      */
128     public DocumentHandler getDocumentHandlerForPath( String documentPath ) throws UnsupportedDocumentTypeException
129     {
130         int dotIndex = documentPath.indexOf('.');
131         
132         if(dotIndex > -1)
133         {
134             try
135             {
136                 return getDocumentHandler(documentPath.substring(dotIndex));
137             }
138             catch (UnsupportedDocumentTypeException e)
139             {
140                 int lastSlash = documentPath.lastIndexOf(Node.PATH_SEPARATOR);
141                 if(lastSlash < 0)
142                 {
143                     lastSlash = 0;
144                 }
145                 return getDocumentHandler(documentPath.substring(lastSlash));
146             }
147         }
148         else
149         {
150             throw new UnsupportedDocumentTypeException("The path provided has no extension and may be a folder.");
151         }
152     }
153 
154     /***
155      * <p>
156      * getPermissionsEnabled
157      * </p>
158      *
159      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getPermissionsEnabled()
160      * @return
161      */
162     public boolean getPermissionsEnabled()
163     {
164         return permissionsEnabled;
165     }
166 
167     /***
168      * <p>
169      * setPermissionsEnabled
170      * </p>
171      *
172      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#setPermissionsEnabled(boolean)
173      * @return
174      */
175     public void setPermissionsEnabled(boolean enabled)
176     {
177         permissionsEnabled = enabled;
178     }
179 
180     /***
181      * <p>
182      * getConstraintsEnabled
183      * </p>
184      *
185      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getConstraintsEnabled()
186      * @return
187      */
188     public boolean getConstraintsEnabled()
189     {
190         return constraintsEnabled;
191     }
192 
193     /***
194      * <p>
195      * setConstraintsEnabled
196      * </p>
197      *
198      * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#setConstraintsEnabled(boolean)
199      * @return
200      */
201     public void setConstraintsEnabled(boolean enabled)
202     {
203         constraintsEnabled = enabled;
204     }
205 }