KickJava   Java API By Example, From Geeks To Geeks.

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


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.cocoon.forms.formmodel.Repeater;
21 import org.apache.cocoon.forms.validation.ValidationErrorAware;
22 import org.apache.cocoon.forms.validation.ValidationError;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.xmlbeans.XmlOptions;
26 import org.outerj.daisy.repository.schema.PartTypeUse;
27 import org.outerj.daisy.repository.Document;
28 import org.outerj.daisy.repository.Part;
29 import org.outerj.daisy.repository.Repository;
30 import org.outerj.daisy.frontend.util.FormHelper;
31 import org.outerj.daisy.books.frontend.BookAclEditorApple;
32 import org.outerj.daisy.books.store.BookAclBuilder;
33 import org.outerj.daisy.books.store.BookAcl;
34
35 import java.io.StringReader JavaDoc;
36 import java.util.Map JavaDoc;
37
38 public class BookAclPartEditor implements PartEditor {
39     private ServiceManager serviceManager;
40
41     private BookAclPartEditor(ServiceManager serviceManager) {
42         this.serviceManager = serviceManager;
43     }
44
45     public static class Factory implements PartEditorFactory {
46         public PartEditor getPartEditor(Map JavaDoc properties, ServiceManager serviceManager, Context context) {
47             return new BookAclPartEditor(serviceManager);
48         }
49     }
50
51     public Form getForm(PartTypeUse partTypeUse, DocumentEditorForm documentEditorForm, final Repository repository) throws Exception JavaDoc {
52         final Form form = FormHelper.createForm(serviceManager, "books/resources/form/bookacl_definition.xml");
53
54         Field editMode = (Field)form.getChild("editmode");
55         editMode.setValue("gui");
56         editMode.addValueChangedListener(new EditModeListener(new EditModeListener.EditModeListenerConfig() {
57             public String JavaDoc getXmlFieldPath() {
58                 return "editors/xml/xmlText";
59             }
60
61             public String JavaDoc getXmlFromGuiEditor() {
62                 return getBookAclXml(form);
63             }
64
65             public boolean loadGui(String JavaDoc xml) {
66                 BookAcl bookAcl;
67                 try {
68                     bookAcl = BookAclBuilder.build(new StringReader JavaDoc(xml));
69                 } catch (Throwable JavaDoc e) {
70                     // TODO nicer error message?
71
((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false));
72                     return false;
73                 }
74                 try {
75                     BookAclEditorApple.load(form, bookAcl);
76                     BookAclEditorApple.annotateAclSubjectValues(form, repository);
77                 } catch (Throwable JavaDoc e) {
78                     // TODO nicer error message?
79
((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false));
80                     return false;
81                 }
82                 return true;
83             }
84
85             public void clearGui() {
86                 Repeater repeater = (Repeater)form.lookupWidget("editors/gui/entries");
87                 repeater.clear();
88             }
89         }));
90
91         return form;
92     }
93
94     public String JavaDoc getFormTemplate() {
95         return "resources/form/parteditor_bookacl_template.xml";
96     }
97
98     public void load(Form form, Document document, Part part, Repository repository) throws Exception JavaDoc {
99         byte[] data = part.getData();
100
101         BookAcl bookAcl = null;
102         try {
103             // Note: BookAclBuilder closes the input stream for us
104
bookAcl = BookAclBuilder.build(part.getDataStream());
105         } catch (Throwable JavaDoc e) {
106             // ignore
107
}
108
109         if (bookAcl != null) {
110             try {
111                 BookAclEditorApple.load(form, bookAcl);
112                 BookAclEditorApple.annotateAclSubjectValues(form, repository);
113             } catch (Throwable JavaDoc e) {
114                 // TODO nicer error message?
115
((ValidationErrorAware)form.lookupWidget("editors/xml/xmlText")).setValidationError(new ValidationError(e.toString(), false));
116                 bookAcl = null;
117             }
118         }
119
120         if (bookAcl == null) {
121             // parsing failed
122
form.setAttribute("ignore-editmode-change", Boolean.TRUE);
123             form.getChild("editmode").setValue("xml");
124             // TODO might want to do smarter encoding detection
125
form.lookupWidget("editors/xml/xmlText").setValue(new String JavaDoc(data, "UTF-8"));
126         }
127     }
128
129     private String JavaDoc getBookAclXml(Form form) {
130         BookAcl bookAcl = BookAclEditorApple.getBookAcl(form);
131         XmlOptions xmlOptions = new XmlOptions();
132         xmlOptions.setSavePrettyPrint();
133         xmlOptions.setCharacterEncoding("UTF-8");
134         return bookAcl.getXml().xmlText(xmlOptions);
135     }
136
137     public void save(Form form, Document document) throws Exception JavaDoc {
138         Field editMode = (Field)form.getChild("editmode");
139         if (editMode.getValue().equals("xml")) {
140             PartEditorHelper.save(form, document, "editors/xml/xmlText", "text/xml");
141         } else if (editMode.getValue().equals("gui")) {
142             String JavaDoc xml = getBookAclXml(form);
143             PartEditorHelper.save(form, document, null, xml.getBytes("UTF-8"), "text/xml");
144         }
145     }
146 }
147
Popular Tags