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.cocoon.components.elementprocessor.impl;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.InvocationTargetException;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.component.Component;
025: import org.apache.avalon.framework.logger.AbstractLogEnabled;
026:
027: import org.apache.cocoon.components.elementprocessor.CannotCreateElementProcessorException;
028: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
029: import org.apache.cocoon.components.elementprocessor.ElementProcessorFactory;
030:
031: /**
032: * Create instances of specific ElementProcessor implementations to
033: * handle specific XML elements and their content.
034: *
035: * @author Marc Johnson (marc_johnson27591@hotmail.com)
036: * @version CVS $Id: AbstractElementProcessorFactory.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public abstract class AbstractElementProcessorFactory extends
039: AbstractLogEnabled implements ElementProcessorFactory,
040: Component {
041:
042: // uses XML element names as keys and ElementProcessor progenitors
043: // as values. An ElementProcessor progenitor is an Object that can
044: // be used to create a new ElementProcessor instance that is
045: // specific to a particular XML element. A progenitor may be a
046: // Constructor or Class that can construct a new instance of an
047: // appropriate ElementProcessor implementation, or any other
048: // Object that an extension of AbstractElementProcessorFactory finds
049: // useful to create new ElementProcessor instances.
050: private Map _element_processor_map;
051:
052: /**
053: * Protected default constructor
054: */
055:
056: protected AbstractElementProcessorFactory() {
057: _element_processor_map = new HashMap();
058: }
059:
060: /**
061: * Given an XML element name, create and return an appropriate
062: * ElementProcessor.
063: *
064: * @param name element name
065: *
066: * @return the specified ElementProcessor
067: *
068: * @exception CannotCreateElementProcessorException if there is no
069: * ElementProcessor available for the specified name
070: */
071:
072: public ElementProcessor createElementProcessor(final String name)
073: throws CannotCreateElementProcessorException {
074: Object progenitor = lookupElementProcessorProgenitor(name);
075:
076: if (progenitor == null) {
077: CannotCreateElementProcessorException exception = new CannotCreateElementProcessorException(
078: "Cannot find progenitor for that name");
079: exception.setElementName(name);
080: throw exception;
081: }
082: ElementProcessor processor = null;
083:
084: try {
085: processor = doCreateElementProcessor(progenitor);
086: } catch (CannotCreateElementProcessorException e) {
087: e.setElementName(name);
088: throw e;
089: }
090: return processor;
091: }
092:
093: /**
094: * A method for extending classes to populate the map.
095: *
096: * @param name the element name for this progenitor; cannot be
097: * null ot empty
098: * @param progenitor an object that can be used to generate an
099: * appropriate ElementProcessor; cannot be nukk
100: *
101: * @exception IllegalArgumentException if name is already in the
102: * map or progenitor is null.
103: */
104:
105: protected void addElementProcessorProgenitor(final String name,
106: final Object progenitor) {
107: if (name == null || name.length() == 0) {
108: throw new IllegalArgumentException(
109: "Cannot use null or empty name as a key");
110: }
111: if (progenitor == null) {
112: throw new IllegalArgumentException(
113: "Cannot add null progenitor to the map");
114: }
115: if (_element_processor_map.put(name, progenitor) != null) {
116: throw new IllegalArgumentException(name
117: + " is already in use in the map");
118: }
119: }
120:
121: /**
122: * A method to get the progenitor value associated with a
123: * specified element name.
124: *
125: * @param name the element name
126: *
127: * @return the associated ElementProcessor progenitor; will be
128: * null if the element name has not yet been associated
129: * with a progenitor.
130: */
131:
132: protected Object lookupElementProcessorProgenitor(final String name) {
133: Object obj = _element_processor_map.get(name);
134: if (obj == null && !name.equals("*")) {
135: obj = lookupElementProcessorProgenitor("*");
136: }
137: return obj;
138: }
139:
140: /**
141: * The method that a concrete extension of AbstractElementProcessorFactory
142: * must implement. When this method is called, the element name
143: * has already been looked up in the map and a progenitor Object
144: * has been acquired. The progenitor is guaranteed not to be null.
145: *
146: * @param progenitor the object from which to create an
147: * ElementProcessor
148: *
149: * @return freshly created ElementProcessor
150: *
151: * @exception CannotCreateElementProcessorException if the
152: * specified ElementProcessor cannot be created.
153: */
154:
155: protected abstract ElementProcessor doCreateElementProcessor(
156: final Object progenitor)
157: throws CannotCreateElementProcessorException;
158:
159: /**
160: * A reference implementation of doCreateElementProcessor that can
161: * be used by an extending class whose progenitors are Class
162: * objects for ElementProcessor implementations.
163: *
164: * @param progenitor a Class representing an ElementProcessor
165: *
166: * @return the new ElementProcessor instance
167: *
168: * @exception CannotCreateElementProcessorException if the
169: * ElementProcessor cannot be created.
170: */
171:
172: protected ElementProcessor createNewElementProcessorInstance(
173: final Class progenitor)
174: throws CannotCreateElementProcessorException {
175: ElementProcessor rval = null;
176:
177: try {
178: rval = (ElementProcessor) progenitor.newInstance();
179: if (rval instanceof AbstractLogEnabled) {
180: ((AbstractLogEnabled) rval).enableLogging(getLogger());
181: }
182: } catch (ExceptionInInitializerError e) {
183: throw new CannotCreateElementProcessorException(
184: "an exception ("
185: + e
186: + ") occurred in initializing the associated ElementProcessor class");
187: } catch (SecurityException e) {
188: throw new CannotCreateElementProcessorException(
189: "a security exception was caught while creating the associated ElementProcessor");
190: } catch (InstantiationException e) {
191: throw new CannotCreateElementProcessorException(
192: "associated ElementProcessor is an interface or abstract class or has no zero-parameter constructor");
193: } catch (IllegalAccessException e) {
194: throw new CannotCreateElementProcessorException(
195: "cannot access ElementProcessor class or its zero-parameter constructor");
196: } catch (ClassCastException e) {
197: throw new CannotCreateElementProcessorException(
198: "object created does not implement ElementProcessor");
199: } catch (Exception e) {
200: throw new CannotCreateElementProcessorException(
201: "exception ("
202: + e
203: + ") occured while creating new instance of ElementProcessor");
204: }
205: if (rval == null) {
206: throw new CannotCreateElementProcessorException(
207: "somehow generated a null ElementProcessor");
208: }
209: return rval;
210: }
211:
212: /**
213: * A reference implementation of doCreateElementProcessor that can
214: * be used by an extending class whose progenitors are Constructor
215: * objects that can create new instances of ElementProcessor
216: * implementations.
217: *
218: * @param progenitor a Constructor of an ElementProcessor
219: *
220: * @return the newly created ElementProcessor
221: *
222: * @exception CannotCreateElementProcessorException if the
223: * ElementProcessor cannot be created.
224: */
225:
226: protected ElementProcessor constructElementProcessor(
227: final Constructor progenitor)
228: throws CannotCreateElementProcessorException {
229: ElementProcessor rval = null;
230:
231: try {
232: rval = (ElementProcessor) progenitor
233: .newInstance(new Object[0]);
234: if (rval instanceof AbstractLogEnabled) {
235: ((AbstractLogEnabled) rval).enableLogging(getLogger());
236: }
237: } catch (ExceptionInInitializerError e) {
238: throw new CannotCreateElementProcessorException(
239: "an exception ("
240: + e
241: + ")occurred in initializing the associated ElementProcessor class");
242: } catch (IllegalArgumentException e) {
243: throw new CannotCreateElementProcessorException(
244: "the ElementProcessor constructor apparently needs parameters");
245: } catch (InstantiationException e) {
246: throw new CannotCreateElementProcessorException(
247: "associated ElementProcessor is an interface or abstract class");
248: } catch (IllegalAccessException e) {
249: throw new CannotCreateElementProcessorException(
250: "cannot access ElementProcessor class or its zero-parameter constructor");
251: } catch (InvocationTargetException e) {
252: throw new CannotCreateElementProcessorException(
253: "ElementProcessor constructor threw an exception ["
254: + e.toString() + "]");
255: } catch (ClassCastException e) {
256: throw new CannotCreateElementProcessorException(
257: "object created does not implement ElementProcessor");
258: }
259: if (rval == null) {
260: throw new CannotCreateElementProcessorException(
261: "somehow generated a null ElementProcessor");
262: }
263: return rval;
264: }
265:
266: } // end public abstract class AbstractElementProcessorFactory
|