001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.page.document.psml;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.jetspeed.page.document.DocumentHandler;
024: import org.apache.jetspeed.page.document.DocumentHandlerFactory;
025: import org.apache.jetspeed.page.document.DocumentTypeAlreadyRegisteredException;
026: import org.apache.jetspeed.page.document.UnsupportedDocumentTypeException;
027: import org.apache.jetspeed.util.ArgUtil;
028:
029: import org.apache.jetspeed.page.document.Node;
030:
031: /**
032: * <p>
033: * DocumentHandlerFactoryImpl
034: * </p>
035: * <p>
036: *
037: * </p>
038: *
039: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
040: * @version $Id: DocumentHandlerFactoryImpl.java 517124 2007-03-12 08:10:25Z ate $
041: *
042: */
043: public class DocumentHandlerFactoryImpl implements
044: DocumentHandlerFactory {
045: private Map handlers;
046:
047: private boolean permissionsEnabled;
048:
049: private boolean constraintsEnabled;
050:
051: /**
052: *
053: */
054: public DocumentHandlerFactoryImpl(Map handlers) {
055: super ();
056:
057: ArgUtil.assertNotNull(Map.class, handlers, this );
058:
059: this .handlers = handlers;
060:
061: // register this with handlers
062: Iterator handlersIter = handlers.values().iterator();
063: while (handlersIter.hasNext()) {
064: ((DocumentHandler) handlersIter.next())
065: .setHandlerFactory(this );
066: }
067: }
068:
069: public DocumentHandlerFactoryImpl() {
070: this (new HashMap());
071:
072: }
073:
074: /**
075: * <p>
076: * getDocumentHandler
077: * </p>
078: *
079: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getDocumentHandler(java.lang.String)
080: * @param documentType
081: * @return
082: */
083: public DocumentHandler getDocumentHandler(String documentType)
084: throws UnsupportedDocumentTypeException {
085: if (handlers.containsKey(documentType)) {
086: return (DocumentHandler) handlers.get(documentType);
087: } else {
088: throw new UnsupportedDocumentTypeException(
089: "There are no DocumentHandlers defined for the type: "
090: + documentType);
091: }
092: }
093:
094: /**
095: * <p>
096: * registerDocumentHandler
097: * </p>
098: *
099: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#registerDocumentHandler(org.apache.jetspeed.page.documenthandler.DocumentHandler)
100: * @param documentHandler
101: * @throws DocumentTypeAlreadyRegisteredException
102: */
103: public void registerDocumentHandler(DocumentHandler documentHandler)
104: throws DocumentTypeAlreadyRegisteredException {
105: if (handlers.containsKey(documentHandler.getType())) {
106: throw new DocumentTypeAlreadyRegisteredException(
107: documentHandler.getType()
108: + " has already been registered.");
109: }
110:
111: // register handler and this with handlers
112: documentHandler.setHandlerFactory(this );
113: handlers.put(documentHandler.getType(), documentHandler);
114: }
115:
116: /**
117: * <p>
118: * getDocumentHandlerForPath
119: * </p>
120: *
121: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getDocumentHandlerForPath(java.lang.String)
122: * @param documentPath
123: * @return
124: */
125: public DocumentHandler getDocumentHandlerForPath(String documentPath)
126: throws UnsupportedDocumentTypeException {
127: int dotIndex = documentPath.indexOf('.');
128:
129: if (dotIndex > -1) {
130: try {
131: return getDocumentHandler(documentPath
132: .substring(dotIndex));
133: } catch (UnsupportedDocumentTypeException e) {
134: int lastSlash = documentPath
135: .lastIndexOf(Node.PATH_SEPARATOR);
136: if (lastSlash < 0) {
137: lastSlash = 0;
138: }
139: return getDocumentHandler(documentPath
140: .substring(lastSlash));
141: }
142: } else {
143: throw new UnsupportedDocumentTypeException(
144: "The path provided has no extension and may be a folder.");
145: }
146: }
147:
148: /**
149: * <p>
150: * getPermissionsEnabled
151: * </p>
152: *
153: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getPermissionsEnabled()
154: * @return
155: */
156: public boolean getPermissionsEnabled() {
157: return permissionsEnabled;
158: }
159:
160: /**
161: * <p>
162: * setPermissionsEnabled
163: * </p>
164: *
165: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#setPermissionsEnabled(boolean)
166: * @return
167: */
168: public void setPermissionsEnabled(boolean enabled) {
169: permissionsEnabled = enabled;
170: }
171:
172: /**
173: * <p>
174: * getConstraintsEnabled
175: * </p>
176: *
177: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#getConstraintsEnabled()
178: * @return
179: */
180: public boolean getConstraintsEnabled() {
181: return constraintsEnabled;
182: }
183:
184: /**
185: * <p>
186: * setConstraintsEnabled
187: * </p>
188: *
189: * @see org.apache.jetspeed.page.document.DocumentHandlerFactory#setConstraintsEnabled(boolean)
190: * @return
191: */
192: public void setConstraintsEnabled(boolean enabled) {
193: constraintsEnabled = enabled;
194: }
195: }
|