KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > BookPartEditor


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.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 JavaDoc;
35 import org.xml.sax.Attributes JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.StringReader JavaDoc;
40 import java.io.ByteArrayOutputStream JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Map JavaDoc;
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 JavaDoc 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 JavaDoc {
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 JavaDoc 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 JavaDoc {
69         // annotate book tree XML with document names
70
byte[] data = part.getData();
71         String JavaDoc 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 JavaDoc(data, "UTF-8"));
77         }
78     }
79
80     public void save(Form form, Document document) throws Exception JavaDoc {
81         Field field = (Field)form.getChild("book");
82         String JavaDoc value = (String JavaDoc)field.getValue();
83         byte[] data = null;
84         if (value != null) {
85             // remove (auto-added) titles again from section nodes which have an ID
86
try {
87                 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
88                 BookDocument bookDocument = BookDocument.Factory.parse(new StringReader JavaDoc(value), xmlOptions);
89                 removeTitles(bookDocument.getBook().getContent().getSectionArray());
90                 ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc(value.length() + 300);
91                 bookDocument.save(os);
92                 data = os.toByteArray();
93             } catch (Throwable JavaDoc e) {
94                 // don't fail on this
95
}
96         }
97         PartEditorHelper.save(form, document, "book", data, "text/xml");
98     }
99
100     private String JavaDoc 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 JavaDoc(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 JavaDoc e) {
119             // resolving IDs failed, don't care
120
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 JavaDoc names, int pos) {
141         for (int i = 0; i < sections.length; i++) {
142             if (sections[i].isSetDocumentId()) {
143                 pos++;
144                 sections[i].setTitle((String JavaDoc)names.get(pos));
145             }
146             pos = annotateSections(sections[i].getSectionArray(), names, pos);
147         }
148         return pos;
149     }
150
151     static class ResolvedIdCollector extends DefaultHandler JavaDoc {
152         private List JavaDoc documentNames = new ArrayList JavaDoc(50);
153
154         public List JavaDoc getNames() {
155             return documentNames;
156         }
157
158         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
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