KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > PublicationSpecBuilder


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;
17
18 import org.outerx.daisy.x10Bookpubspecs.PublicationSpecsDocument;
19 import org.apache.commons.collections.map.ListOrderedMap;
20 import org.apache.xmlbeans.XmlOptions;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Collections JavaDoc;
28
29 /**
30  * Builds {@link PublicationSpec}s from XML.
31  */

32 public class PublicationSpecBuilder {
33     public static PublicationSpec[] build(InputStream JavaDoc is) throws Exception JavaDoc {
34         try {
35             XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
36             PublicationSpecsDocument pubSpecsDoc = PublicationSpecsDocument.Factory.parse(is, xmlOptions);
37             return build(pubSpecsDoc.getPublicationSpecs());
38         } finally {
39             if (is != null)
40                 is.close();
41         }
42     }
43
44     public static PublicationSpec[] build(Reader JavaDoc reader) throws Exception JavaDoc {
45         try {
46             XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
47             PublicationSpecsDocument pubSpecsDoc = PublicationSpecsDocument.Factory.parse(reader, xmlOptions);
48             return build(pubSpecsDoc.getPublicationSpecs());
49         } finally {
50             if (reader != null)
51                 reader.close();
52         }
53     }
54
55     public static PublicationSpec[] build(PublicationSpecsDocument.PublicationSpecs publicationSpecsXml) {
56         PublicationSpecsDocument.PublicationSpecs.PublicationSpec[] publicationSpesXmlArr = publicationSpecsXml.getPublicationSpecArray();
57         PublicationSpec[] publicationSpecs = new PublicationSpec[publicationSpesXmlArr.length];
58         for (int i = 0; i < publicationSpesXmlArr.length; i++) {
59             String JavaDoc type = publicationSpesXmlArr[i].getType();
60             String JavaDoc name = publicationSpesXmlArr[i].getName();
61             String JavaDoc label = publicationSpesXmlArr[i].getLabel();
62             if (type == null || name == null || type.trim().length() == 0 || name.trim().length() == 0)
63                 continue;
64             Map properties;
65             if (publicationSpesXmlArr[i].isSetProperties()) {
66                 properties = buildProperties(publicationSpesXmlArr[i].getProperties());
67             } else {
68                 properties = Collections.EMPTY_MAP;
69             }
70             publicationSpecs[i] = new PublicationSpec(type, name, label, properties);
71         }
72         return publicationSpecs;
73     }
74
75     private static Map buildProperties(PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties propertiesXml) {
76         PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties.Entry[] entriesXml = propertiesXml.getEntryArray();
77         Map properties = new ListOrderedMap();
78         for (int i = 0; i < entriesXml.length; i++) {
79             String JavaDoc key = entriesXml[i].getKey();
80             if (key == null || key.trim().length() == 0)
81                 continue;
82             String JavaDoc value = entriesXml[i].getStringValue();
83             if (value == null)
84                 value = "";
85             properties.put(key, value);
86         }
87         return properties;
88     }
89 }
90
Popular Tags