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.frontend.editor;
017:
018: import org.apache.cocoon.forms.formmodel.Form;
019: import org.apache.cocoon.forms.formmodel.Field;
020: import org.apache.cocoon.forms.formmodel.Repeater;
021: import org.apache.cocoon.forms.validation.ValidationErrorAware;
022: import org.apache.cocoon.forms.validation.ValidationError;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.xmlbeans.XmlOptions;
026: import org.outerj.daisy.repository.schema.PartTypeUse;
027: import org.outerj.daisy.repository.Document;
028: import org.outerj.daisy.repository.Part;
029: import org.outerj.daisy.repository.Repository;
030: import org.outerj.daisy.frontend.util.FormHelper;
031: import org.outerj.daisy.books.frontend.BookAclEditorApple;
032: import org.outerj.daisy.books.store.BookAclBuilder;
033: import org.outerj.daisy.books.store.BookAcl;
034:
035: import java.io.StringReader;
036: import java.util.Map;
037:
038: public class BookAclPartEditor implements PartEditor {
039: private ServiceManager serviceManager;
040:
041: private BookAclPartEditor(ServiceManager serviceManager) {
042: this .serviceManager = serviceManager;
043: }
044:
045: public static class Factory implements PartEditorFactory {
046: public PartEditor getPartEditor(Map properties,
047: ServiceManager serviceManager, Context context) {
048: return new BookAclPartEditor(serviceManager);
049: }
050: }
051:
052: public Form getForm(PartTypeUse partTypeUse,
053: DocumentEditorForm documentEditorForm,
054: final Repository repository) throws Exception {
055: final Form form = FormHelper.createForm(serviceManager,
056: "books/resources/form/bookacl_definition.xml");
057:
058: Field editMode = (Field) form.getChild("editmode");
059: editMode.setValue("gui");
060: editMode.addValueChangedListener(new EditModeListener(
061: new EditModeListener.EditModeListenerConfig() {
062: public String getXmlFieldPath() {
063: return "editors/xml/xmlText";
064: }
065:
066: public String getXmlFromGuiEditor() {
067: return getBookAclXml(form);
068: }
069:
070: public boolean loadGui(String xml) {
071: BookAcl bookAcl;
072: try {
073: bookAcl = BookAclBuilder
074: .build(new StringReader(xml));
075: } catch (Throwable e) {
076: // TODO nicer error message?
077: ((ValidationErrorAware) form
078: .lookupWidget(getXmlFieldPath()))
079: .setValidationError(new ValidationError(
080: e.toString(), false));
081: return false;
082: }
083: try {
084: BookAclEditorApple.load(form, bookAcl);
085: BookAclEditorApple
086: .annotateAclSubjectValues(form,
087: repository);
088: } catch (Throwable e) {
089: // TODO nicer error message?
090: ((ValidationErrorAware) form
091: .lookupWidget(getXmlFieldPath()))
092: .setValidationError(new ValidationError(
093: e.toString(), false));
094: return false;
095: }
096: return true;
097: }
098:
099: public void clearGui() {
100: Repeater repeater = (Repeater) form
101: .lookupWidget("editors/gui/entries");
102: repeater.clear();
103: }
104: }));
105:
106: return form;
107: }
108:
109: public String getFormTemplate() {
110: return "resources/form/parteditor_bookacl_template.xml";
111: }
112:
113: public void load(Form form, Document document, Part part,
114: Repository repository) throws Exception {
115: byte[] data = part.getData();
116:
117: BookAcl bookAcl = null;
118: try {
119: // Note: BookAclBuilder closes the input stream for us
120: bookAcl = BookAclBuilder.build(part.getDataStream());
121: } catch (Throwable e) {
122: // ignore
123: }
124:
125: if (bookAcl != null) {
126: try {
127: BookAclEditorApple.load(form, bookAcl);
128: BookAclEditorApple.annotateAclSubjectValues(form,
129: repository);
130: } catch (Throwable e) {
131: // TODO nicer error message?
132: ((ValidationErrorAware) form
133: .lookupWidget("editors/xml/xmlText"))
134: .setValidationError(new ValidationError(e
135: .toString(), false));
136: bookAcl = null;
137: }
138: }
139:
140: if (bookAcl == null) {
141: // parsing failed
142: form.setAttribute("ignore-editmode-change", Boolean.TRUE);
143: form.getChild("editmode").setValue("xml");
144: // TODO might want to do smarter encoding detection
145: form.lookupWidget("editors/xml/xmlText").setValue(
146: new String(data, "UTF-8"));
147: }
148: }
149:
150: private String getBookAclXml(Form form) {
151: BookAcl bookAcl = BookAclEditorApple.getBookAcl(form);
152: XmlOptions xmlOptions = new XmlOptions();
153: xmlOptions.setSavePrettyPrint();
154: xmlOptions.setCharacterEncoding("UTF-8");
155: return bookAcl.getXml().xmlText(xmlOptions);
156: }
157:
158: public void save(Form form, Document document) throws Exception {
159: Field editMode = (Field) form.getChild("editmode");
160: if (editMode.getValue().equals("xml")) {
161: PartEditorHelper.save(form, document,
162: "editors/xml/xmlText", "text/xml");
163: } else if (editMode.getValue().equals("gui")) {
164: String xml = getBookAclXml(form);
165: PartEditorHelper.save(form, document, null, xml
166: .getBytes("UTF-8"), "text/xml");
167: }
168: }
169: }
|