001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.books.publisher;
017:
018: import org.outerx.daisy.x10Bookpubspecs.PublicationSpecsDocument;
019: import org.apache.xmlbeans.XmlOptions;
020: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
021:
022: import java.io.InputStream;
023: import java.io.Reader;
024: import java.util.Map;
025: import java.util.Collections;
026: import java.util.LinkedHashMap;
027:
028: /**
029: * Builds {@link PublicationSpec}s from XML.
030: */
031: public class PublicationSpecBuilder {
032: public static PublicationSpec[] build(InputStream is)
033: throws Exception {
034: try {
035: XmlOptions xmlOptions = new XmlOptions()
036: .setLoadUseXMLReader(LocalSAXParserFactory
037: .newXmlReader());
038: PublicationSpecsDocument pubSpecsDoc = PublicationSpecsDocument.Factory
039: .parse(is, xmlOptions);
040: return build(pubSpecsDoc.getPublicationSpecs());
041: } finally {
042: if (is != null)
043: is.close();
044: }
045: }
046:
047: public static PublicationSpec[] build(Reader reader)
048: throws Exception {
049: try {
050: XmlOptions xmlOptions = new XmlOptions()
051: .setLoadUseXMLReader(LocalSAXParserFactory
052: .newXmlReader());
053: PublicationSpecsDocument pubSpecsDoc = PublicationSpecsDocument.Factory
054: .parse(reader, xmlOptions);
055: return build(pubSpecsDoc.getPublicationSpecs());
056: } finally {
057: if (reader != null)
058: reader.close();
059: }
060: }
061:
062: public static PublicationSpec[] build(
063: PublicationSpecsDocument.PublicationSpecs publicationSpecsXml) {
064: PublicationSpec[] publicationSpecs = new PublicationSpec[publicationSpecsXml
065: .sizeOfPublicationSpecArray()];
066: int i = 0;
067: for (PublicationSpecsDocument.PublicationSpecs.PublicationSpec publicationSpecXml : publicationSpecsXml
068: .getPublicationSpecList()) {
069: String type = publicationSpecXml.getType();
070: String name = publicationSpecXml.getName();
071: String label = publicationSpecXml.getLabel();
072: if (type == null || name == null
073: || type.trim().length() == 0
074: || name.trim().length() == 0)
075: continue;
076: Map<String, String> properties;
077: if (publicationSpecXml.isSetProperties()) {
078: properties = buildProperties(publicationSpecXml
079: .getProperties());
080: } else {
081: properties = Collections.emptyMap();
082: }
083: publicationSpecs[i++] = new PublicationSpec(type, name,
084: label, properties);
085: }
086: return publicationSpecs;
087: }
088:
089: private static Map<String, String> buildProperties(
090: PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties propertiesXml) {
091: Map<String, String> properties = new LinkedHashMap<String, String>();
092: for (PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties.Entry entryXml : propertiesXml
093: .getEntryList()) {
094: String key = entryXml.getKey();
095: if (key == null || key.trim().length() == 0)
096: continue;
097: String value = entryXml.getStringValue();
098: if (value == null)
099: value = "";
100: properties.put(key, value);
101: }
102: return properties;
103: }
104: }
|