01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.datatype.convertor.ConvertorBuilder;
19: import org.apache.cocoon.forms.datatype.convertor.Convertor;
20: import org.apache.cocoon.forms.util.DomHelper;
21: import org.apache.xmlbeans.XmlOptions;
22: import org.w3c.dom.Element;
23:
24: import java.lang.reflect.Method;
25: import java.io.Reader;
26:
27: public class XmlBeansConvertorBuilder implements ConvertorBuilder {
28: public Convertor build(Element element) throws Exception {
29: String xmlbeansClassName = DomHelper.getAttribute(element,
30: "class");
31: Class xmlbeansClass = this .getClass().getClassLoader()
32: .loadClass(xmlbeansClassName);
33: Class[] factoryClasses = xmlbeansClass.getDeclaredClasses();
34: Class factoryClass = null;
35: for (int i = 0; i < factoryClasses.length; i++) {
36: if (factoryClasses[i].getName().endsWith("$Factory")) {
37: factoryClass = factoryClasses[i];
38: break;
39: }
40: }
41: if (factoryClass == null) {
42: throw new Exception(
43: "Could not find Factory inner class in XmlBeans class "
44: + xmlbeansClassName);
45: }
46:
47: Method parseMethod = factoryClass.getMethod("parse",
48: Reader.class, XmlOptions.class);
49: return new XmlBeansConvertor(parseMethod);
50: }
51: }
|