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.validation.ValidationError;
019: import org.apache.cocoon.forms.datatype.convertor.Convertor;
020: import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
021: import org.apache.xmlbeans.XmlOptions;
022: import org.apache.xmlbeans.XmlError;
023: import org.apache.xmlbeans.XmlException;
024: import org.apache.xmlbeans.XmlObject;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027: import org.outerj.daisy.frontend.util.TaggedMessage;
028: import org.outerj.daisy.frontend.util.MultiMessage;
029:
030: import java.io.StringReader;
031: import java.util.*;
032: import java.lang.reflect.Method;
033: import java.lang.reflect.InvocationTargetException;
034:
035: public class XmlBeansConvertor implements Convertor {
036: private Method parseMethod;
037:
038: public XmlBeansConvertor(Method parseMethod) {
039: this .parseMethod = parseMethod;
040: }
041:
042: public ConversionResult convertFromString(String value,
043: Locale locale, FormatCache formatCache) {
044: List errors = new ArrayList();
045: boolean valid;
046:
047: XmlObject document;
048: try {
049: XmlOptions xmlOptions = new XmlOptions();
050: xmlOptions.setDocumentSourceName("edited xml");
051: xmlOptions.setErrorListener(errors);
052: xmlOptions.setLoadLineNumbers();
053: document = (XmlObject) parseMethod.invoke(null,
054: new StringReader(value), xmlOptions);
055:
056: xmlOptions = new XmlOptions();
057: xmlOptions.setErrorListener(errors);
058: valid = document.validate(xmlOptions);
059: } catch (InvocationTargetException e) {
060: Throwable targetException = e.getTargetException();
061: if (targetException instanceof XmlException) {
062: return new ConversionResult(
063: validationErrorForXmlErrors(((XmlException) targetException)
064: .getErrors()));
065: } else {
066: return new ConversionResult(new ValidationError(
067: "Problem parsing navigtion tree: "
068: + e.getMessage(), false));
069: }
070: } catch (Exception e) {
071: return new ConversionResult(
072: new ValidationError(
073: "Problem parsing navigtion tree: "
074: + e.getMessage(), false));
075: }
076:
077: if (!valid) {
078: return new ConversionResult(
079: validationErrorForXmlErrors(errors));
080: } else {
081: return new ConversionResult(document.toString());
082: }
083: }
084:
085: private ValidationError validationErrorForXmlErrors(
086: Collection xmlErrors) {
087: MultiMessage message = new MultiMessage();
088: message.addMessage(new TaggedMessage("title",
089: "Error parsing or validating XML:"));
090: Iterator errorsIt = xmlErrors.iterator();
091: while (errorsIt.hasNext()) {
092: XmlError error = (XmlError) errorsIt.next();
093: message.addMessage(new TaggedMessage("error", error
094: .getMessage()));
095: }
096: return new ValidationError(message);
097: }
098:
099: public String convertToString(Object o, Locale locale,
100: Convertor.FormatCache formatCache) {
101: return (String) o;
102: }
103:
104: public Class getTypeClass() {
105: return java.lang.String.class;
106: }
107:
108: public void generateSaxFragment(ContentHandler contentHandler,
109: Locale locale) throws SAXException {
110: }
111:
112: }
|