KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > sg > impl > JaxMeSGFactory


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
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  */

17 package org.apache.ws.jaxme.generator.sg.impl;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.ws.jaxme.generator.Generator;
25 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBAppinfoImpl;
26 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBXsObjectFactoryImpl;
27 import org.apache.ws.jaxme.xs.xml.XsEAppinfo;
28 import org.apache.ws.jaxme.xs.xml.XsObject;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.Locator JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33
34 /**
35  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
36  */

37 public class JaxMeSGFactory extends JAXBSGFactory {
38   public class JaxMeAppinfoImpl extends JAXBAppinfoImpl {
39     JaxMeAppinfoImpl(XsObject pParent) {
40       super(pParent);
41     }
42
43     public ContentHandler JavaDoc getChildHandler(String JavaDoc pQName, String JavaDoc pNamespaceURI, String JavaDoc pLocalName) throws SAXException JavaDoc {
44       List JavaDoc myAnnotationClasses = getAnnotationClasses();
45       for (int i = 0; i < myAnnotationClasses.size(); i +=3) {
46         if (((String JavaDoc) myAnnotationClasses.get(i)).equals(pNamespaceURI) &&
47             ((String JavaDoc) myAnnotationClasses.get(i+1)).equals(pLocalName)) {
48           Class JavaDoc c = (Class JavaDoc) myAnnotationClasses.get(i+2);
49           Object JavaDoc o;
50           try {
51             Constructor JavaDoc con = c.getConstructor(new Class JavaDoc[]{JaxMeXsObjectFactory.class, Locator JavaDoc.class});
52             o = con.newInstance(new Object JavaDoc[]{this, getLocator()});
53           } catch (NoSuchMethodException JavaDoc e) {
54             throw new SAXException JavaDoc("Invalid appinfo class " + c.getClass().getName() +
55                                     ": Doesn't have a constructor taking the arguments " +
56                                     JaxMeXsObjectFactory.class.getName() + " and " +
57                                     Locator JavaDoc.class.getName());
58           } catch (InvocationTargetException JavaDoc e) {
59             Throwable JavaDoc t = e.getTargetException();
60             String JavaDoc msg = "Failed to create appinfo element of class " + c.getClass().getName() +
61                          ": " + t.getClass().getName() + ", " + t.getMessage();
62             if (t instanceof Exception JavaDoc) {
63               throw new SAXException JavaDoc(msg, (Exception JavaDoc) t);
64             } else {
65               throw new SAXException JavaDoc(msg, e);
66             }
67           } catch (Exception JavaDoc e) {
68             throw new SAXException JavaDoc("Failed to create appinfo element of class " + c.getClass().getName() +
69                                     ": " + e.getClass().getName());
70           }
71           return getObjectFactory().newXsSAXParser(o);
72         }
73       }
74       return super.getChildHandler(pQName, pNamespaceURI, pLocalName);
75     }
76   }
77
78   public class JaxMeXsObjectFactory extends JAXBXsObjectFactoryImpl {
79     public XsEAppinfo newXsEAppinfo(XsObject pParent) {
80       return new JaxMeAppinfoImpl(pParent);
81     }
82   }
83
84   public JaxMeSGFactory(Generator pGenerator) {
85     super(pGenerator);
86   }
87
88   private List JavaDoc annotationClasses = new ArrayList JavaDoc();
89   protected List JavaDoc getAnnotationClasses() {
90     return annotationClasses;
91   }
92
93   public void addAnnotationClass(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, Class JavaDoc pClass) {
94     if (pNamespaceURI == null || pNamespaceURI.length() == 0) {
95       throw new NullPointerException JavaDoc("A nonempty namespace URI must be given.");
96     }
97     if (pLocalName == null || pLocalName.length() == 0) {
98       throw new NullPointerException JavaDoc("A nonempty local name must be given.");
99     }
100     if (pClass == null) {
101       throw new NullPointerException JavaDoc("An annotation class must be given.");
102     }
103     annotationClasses.add(pNamespaceURI);
104     annotationClasses.add(pLocalName);
105     annotationClasses.add(pClass);
106   }
107 }
108
Popular Tags