1 16 package org.outerj.daisy.frontend.editor; 17 18 import org.apache.cocoon.forms.formmodel.Form; 19 import org.apache.cocoon.forms.formmodel.Field; 20 import org.apache.avalon.framework.service.ServiceManager; 21 import org.apache.avalon.framework.context.Context; 22 import org.apache.xmlbeans.XmlOptions; 23 import org.outerj.daisy.repository.schema.PartTypeUse; 24 import org.outerj.daisy.repository.Part; 25 import org.outerj.daisy.repository.Document; 26 import org.outerj.daisy.repository.Repository; 27 import org.outerj.daisy.frontend.util.FormHelper; 28 import org.outerj.daisy.publisher.Publisher; 29 import org.outerj.daisy.xmlutil.LocalSAXParserFactory; 30 import org.outerx.daisy.x10Bookdef.BookDocument; 31 import org.outerx.daisy.x10Bookdef.SectionDocument; 32 import org.outerx.daisy.x10Publisher.PublisherRequestDocument; 33 import org.outerx.daisy.x10Publisher.ResolveDocumentIdsDocument; 34 import org.xml.sax.helpers.DefaultHandler ; 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.SAXException ; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.StringReader ; 40 import java.io.ByteArrayOutputStream ; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.util.Map ; 44 45 public class BookPartEditor implements PartEditor { 46 private ServiceManager serviceManager; 47 48 private BookPartEditor(ServiceManager serviceManager) { 49 this.serviceManager = serviceManager; 50 } 51 52 public static class Factory implements PartEditorFactory { 53 public PartEditor getPartEditor(Map properties, ServiceManager serviceManager, Context context) { 54 return new BookPartEditor(serviceManager); 55 } 56 } 57 58 public Form getForm(PartTypeUse partTypeUse, DocumentEditorForm documentEditorForm, Repository repository) throws Exception { 59 Form form = FormHelper.createForm(serviceManager, "resources/form/parteditor_book_definition.xml"); 60 form.getChild("book").addValidator(new PartRequiredValidator(documentEditorForm, partTypeUse.isRequired(), false)); 61 return form; 62 } 63 64 public String getFormTemplate() { 65 return "resources/form/parteditor_book_template.xml"; 66 } 67 68 public void load(Form form, Document document, Part part, Repository repository) throws Exception { 69 byte[] data = part.getData(); 71 String result = annotateWithDocumentTitles(data, document, repository); 72 Field field = (Field)form.getChild("book"); 73 if (result != null) { 74 field.setValue(result); 75 } else { 76 field.setValue(new String (data, "UTF-8")); 77 } 78 } 79 80 public void save(Form form, Document document) throws Exception { 81 Field field = (Field)form.getChild("book"); 82 String value = (String )field.getValue(); 83 byte[] data = null; 84 if (value != null) { 85 try { 87 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader()); 88 BookDocument bookDocument = BookDocument.Factory.parse(new StringReader (value), xmlOptions); 89 removeTitles(bookDocument.getBook().getContent().getSectionArray()); 90 ByteArrayOutputStream os = new ByteArrayOutputStream (value.length() + 300); 91 bookDocument.save(os); 92 data = os.toByteArray(); 93 } catch (Throwable e) { 94 } 96 } 97 PartEditorHelper.save(form, document, "book", data, "text/xml"); 98 } 99 100 private String annotateWithDocumentTitles(byte[] data, Document document, Repository repository) { 101 try { 102 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader()); 103 BookDocument bookDocument = BookDocument.Factory.parse(new ByteArrayInputStream (data), xmlOptions); 104 PublisherRequestDocument publisherRequestDocument = PublisherRequestDocument.Factory.newInstance(); 105 PublisherRequestDocument.PublisherRequest publisherRequest = publisherRequestDocument.addNewPublisherRequest(); 106 ResolveDocumentIdsDocument.ResolveDocumentIds resolveDocIds = publisherRequest.addNewResolveDocumentIds(); 107 resolveDocIds.setBranch(String.valueOf(document.getBranchId())); 108 resolveDocIds.setLanguage(String.valueOf(document.getLanguageId())); 109 110 SectionDocument.Section[] sections = bookDocument.getBook().getContent().getSectionArray(); 111 buildRequest(sections, resolveDocIds); 112 Publisher publisher = (Publisher)repository.getExtension("Publisher"); 113 ResolvedIdCollector collector = new ResolvedIdCollector(); 114 publisher.processRequest(publisherRequestDocument, collector); 115 annotateSections(sections, collector.getNames(), -1); 116 117 return bookDocument.xmlText(); 118 } catch (Throwable e) { 119 return null; 121 } 122 } 123 124 private void buildRequest(SectionDocument.Section[] sections, ResolveDocumentIdsDocument.ResolveDocumentIds resolveDocIds) { 125 for (int i = 0; i < sections.length; i++) { 126 if (sections[i].isSetDocumentId()) { 127 ResolveDocumentIdsDocument.ResolveDocumentIds.Document document = resolveDocIds.addNewDocument(); 128 document.setId(sections[i].getDocumentId()); 129 if (sections[i].isSetBranch()) 130 document.setBranch(sections[i].getBranch()); 131 if (sections[i].isSetLanguage()) 132 document.setLanguage(sections[i].getLanguage()); 133 if (sections[i].isSetVersion()) 134 document.setVersion(sections[i].getVersion()); 135 } 136 buildRequest(sections[i].getSectionArray(), resolveDocIds); 137 } 138 } 139 140 private int annotateSections(SectionDocument.Section[] sections, List names, int pos) { 141 for (int i = 0; i < sections.length; i++) { 142 if (sections[i].isSetDocumentId()) { 143 pos++; 144 sections[i].setTitle((String )names.get(pos)); 145 } 146 pos = annotateSections(sections[i].getSectionArray(), names, pos); 147 } 148 return pos; 149 } 150 151 static class ResolvedIdCollector extends DefaultHandler { 152 private List documentNames = new ArrayList (50); 153 154 public List getNames() { 155 return documentNames; 156 } 157 158 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 159 if (uri.equals("http://outerx.org/daisy/1.0#publisher") && localName.equals("document")) { 160 documentNames.add(attributes.getValue("name")); 161 } 162 } 163 } 164 165 private void removeTitles(SectionDocument.Section[] sections) { 166 for (int i = 0; i < sections.length; i++) { 167 if (sections[i].isSetDocumentId() && sections[i].isSetTitle()) { 168 sections[i].unsetTitle(); 169 } 170 removeTitles(sections[i].getSectionArray()); 171 } 172 } 173 174 } 175 | Popular Tags |