01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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:
18: /* $Id$ */
19:
20: package org.apache.xmlgraphics.ps.dsc;
21:
22: import java.util.Map;
23:
24: import org.apache.xmlgraphics.ps.DSCConstants;
25: import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
26: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginResource;
27: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources;
28: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentSuppliedResources;
29: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndComments;
30: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel;
31: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage;
32: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageResources;
33: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages;
34: import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndOfFile;
35:
36: /**
37: * Factory for DSCComment subclasses.
38: */
39: public class DSCCommentFactory {
40:
41: private static final Map DSC_FACTORIES = new java.util.HashMap();
42:
43: static {
44: DSC_FACTORIES.put(DSCConstants.END_COMMENTS,
45: DSCCommentEndComments.class);
46: DSC_FACTORIES.put(DSCConstants.BEGIN_RESOURCE,
47: DSCCommentBeginResource.class);
48: DSC_FACTORIES.put(DSCConstants.PAGE_RESOURCES,
49: DSCCommentPageResources.class);
50: DSC_FACTORIES.put(DSCConstants.PAGE, DSCCommentPage.class);
51: DSC_FACTORIES.put(DSCConstants.PAGES, DSCCommentPages.class);
52: DSC_FACTORIES.put(DSCConstants.LANGUAGE_LEVEL,
53: DSCCommentLanguageLevel.class);
54: DSC_FACTORIES.put(DSCConstants.DOCUMENT_NEEDED_RESOURCES,
55: DSCCommentDocumentNeededResources.class);
56: DSC_FACTORIES.put(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES,
57: DSCCommentDocumentSuppliedResources.class);
58: DSC_FACTORIES.put(DSCConstants.EOF, DSCCommentEndOfFile.class);
59: //TODO Add additional implementations as needed
60: }
61:
62: /**
63: * Creates and returns new instances for DSC comments with a given name.
64: * @param name the name of the DSCComment (without the "%%" prefix)
65: * @return the new instance or null if no particular subclass registered for the given
66: * DSC comment.
67: */
68: public static DSCComment createDSCCommentFor(String name) {
69: Class clazz = (Class) DSC_FACTORIES.get(name);
70: if (clazz == null) {
71: return null;
72: }
73: try {
74: return (DSCComment) clazz.newInstance();
75: } catch (InstantiationException e) {
76: throw new RuntimeException(
77: "Error instantiating instance for '" + name + "': "
78: + e.getMessage());
79: } catch (IllegalAccessException e) {
80: throw new RuntimeException(
81: "Illegal Access error while instantiating instance for '"
82: + name + "': " + e.getMessage());
83: }
84: }
85:
86: }
|