1 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 ; 22 import org.outerj.daisy.xmlutil.LocalSAXParserFactory; 23 24 import java.io.InputStream ; 25 import java.io.Reader ; 26 import java.util.Map ; 27 import java.util.Collections ; 28 29 32 public class PublicationSpecBuilder { 33 public static PublicationSpec[] build(InputStream is) throws Exception { 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 reader) throws Exception { 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 type = publicationSpesXmlArr[i].getType(); 60 String name = publicationSpesXmlArr[i].getName(); 61 String 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 key = entriesXml[i].getKey(); 80 if (key == null || key.trim().length() == 0) 81 continue; 82 String value = entriesXml[i].getStringValue(); 83 if (value == null) 84 value = ""; 85 properties.put(key, value); 86 } 87 return properties; 88 } 89 } 90 | Popular Tags |