KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationtype > PublicationTypeBuilder


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.publicationtype;
17
18 import org.outerx.daisy.x10Bookpubtype.PublicationTypeDocument;
19 import org.outerj.daisy.books.publisher.impl.publicationprocess.PublicationProcessBuilder;
20 import org.outerj.daisy.books.publisher.impl.publicationprocess.PublicationProcess;
21 import org.outerj.daisy.books.publisher.impl.util.XMLPropertiesHelper;
22 import org.outerj.daisy.books.publisher.impl.util.CustomImplementationHelper;
23 import org.outerj.daisy.books.publisher.impl.dataretrieval.PartDecider;
24 import org.outerj.daisy.books.store.impl.XmlUtil;
25 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.excalibur.source.SourceResolver;
28 import org.apache.excalibur.source.Source;
29 import org.apache.commons.collections.map.ListOrderedMap;
30 import org.apache.xmlbeans.XmlOptions;
31
32 import java.io.InputStream JavaDoc;
33 import java.io.BufferedInputStream JavaDoc;
34 import java.util.*;
35
36 public class PublicationTypeBuilder {
37     public static PublicationType build(String JavaDoc publicationTypeName, String JavaDoc daisyContextPath, ServiceManager serviceManager) throws Exception JavaDoc {
38         SourceResolver sourceResolver = null;
39         Source source = null;
40         Source commonPropertiesSource = null;
41         Source propertiesSource = null;
42         try {
43             sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
44
45             commonPropertiesSource = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/common/properties.xml");
46             Map commonProperties = loadProperties(commonPropertiesSource, null);
47
48             propertiesSource = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/" + publicationTypeName + "/properties.xml");
49             Map properties = loadProperties(propertiesSource, commonProperties);
50
51             source = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/" + publicationTypeName + "/publicationtype.xml");
52
53             return build(publicationTypeName, source.getInputStream(), properties);
54         } finally {
55             if (source != null)
56                 sourceResolver.release(source);
57             if (propertiesSource != null)
58                 sourceResolver.release(propertiesSource);
59             if (commonPropertiesSource != null)
60                 sourceResolver.release(commonPropertiesSource);
61             if (sourceResolver != null)
62                 serviceManager.release(sourceResolver);
63         }
64     }
65
66     public static Map loadProperties(String JavaDoc publicationTypeName, String JavaDoc daisyContextPath, ServiceManager serviceManager) throws Exception JavaDoc {
67         SourceResolver sourceResolver = null;
68         Source publicationTypeSource = null;
69         Source commonPropertiesSource = null;
70         Source propertiesSource = null;
71         try {
72             sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
73
74             // Check the publication type exist
75
publicationTypeSource = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/" + publicationTypeName + "/publicationtype.xml");
76             if (!publicationTypeSource.exists())
77                 throw new Exception JavaDoc("A publication type named \"" + publicationTypeName + "\" does not exist.");
78
79             commonPropertiesSource = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/common/properties.xml");
80             Map commonProperties = loadProperties(commonPropertiesSource, null);
81
82             propertiesSource = sourceResolver.resolveURI(daisyContextPath + "/books/publicationtypes/" + publicationTypeName + "/properties.xml");
83             return loadProperties(propertiesSource, commonProperties);
84         } finally {
85             if (publicationTypeSource != null)
86                 sourceResolver.release(publicationTypeSource);
87             if (propertiesSource != null)
88                 sourceResolver.release(propertiesSource);
89             if (commonPropertiesSource != null)
90                 sourceResolver.release(commonPropertiesSource);
91             if (sourceResolver != null)
92                 serviceManager.release(sourceResolver);
93         }
94     }
95
96     private static Map loadProperties(Source source, Map defaults) throws Exception JavaDoc {
97         Map properties;
98         if (source.exists()) {
99             InputStream JavaDoc propertiesIs = null;
100             try {
101                 propertiesIs = new BufferedInputStream JavaDoc(source.getInputStream());
102                 properties = XMLPropertiesHelper.load(propertiesIs, defaults);
103             } finally {
104                 if (propertiesIs != null)
105                     propertiesIs.close();
106             }
107         } else {
108             if (defaults != null) {
109                 properties = new ListOrderedMap();
110                 properties.putAll(defaults);
111             } else {
112                 return new ListOrderedMap();
113             }
114         }
115         return properties;
116     }
117
118     public static PublicationType build(String JavaDoc publicationTypeName, InputStream JavaDoc is, Map properties) throws Exception JavaDoc {
119         PublicationTypeDocument publicationTypeDocument;
120         try {
121             XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
122             publicationTypeDocument = PublicationTypeDocument.Factory.parse(is, xmlOptions);
123         } finally {
124             is.close();
125         }
126         return build(publicationTypeName, publicationTypeDocument.getPublicationType(), properties);
127     }
128
129     public static PublicationType build(String JavaDoc publicationTypeName, PublicationTypeDocument.PublicationType publicationTypeXml, Map properties) throws Exception JavaDoc {
130         String JavaDoc errors = XmlUtil.validate(publicationTypeXml);
131         if (errors != null)
132             throw new Exception JavaDoc("The publication type XML is not valid according to its XML Schema, encountered errors: " + errors);
133
134         PublicationProcess publicationProcess = PublicationProcessBuilder.build(publicationTypeXml.getPublicationProcess());
135
136         PublicationTypeDocument.PublicationType.RequiredParts requiredPartsXml = publicationTypeXml.getRequiredParts();
137         PartDecider partDecider;
138         if (requiredPartsXml != null) {
139             Class JavaDoc partDeciderClass = DefaultPartDecider.class;
140             if (requiredPartsXml.isSetClass1()) {
141                 try {
142                     partDeciderClass = PublicationTypeBuilder.class.getClassLoader().loadClass(requiredPartsXml.getClass1());
143                 } catch (ClassNotFoundException JavaDoc e) {
144                     throw new Exception JavaDoc("Publication type: specified class for requiredParts not found: " + requiredPartsXml.getClass1(), e);
145                 }
146                 if (!PartDecider.class.isAssignableFrom(partDeciderClass)) {
147                     throw new Exception JavaDoc("Publication type: specified class does for requiredParts not implement PartDecider interface: " + requiredPartsXml.getClass1());
148                 }
149             }
150             partDecider = (PartDecider)CustomImplementationHelper.instantiateComponent(partDeciderClass, requiredPartsXml);
151         } else {
152             partDecider = new DefaultPartDecider(Collections.EMPTY_MAP);
153         }
154
155         PublicationType publicationType = new PublicationType(publicationTypeName, publicationTypeXml.getLabel(),
156                 publicationTypeXml.getStartResource(), publicationProcess, partDecider, properties);
157         return publicationType;
158     }
159 }
160
Popular Tags