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