KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > parser > impl > TextSetterImpl


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.xs.parser.impl;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
22
23 import org.apache.ws.jaxme.xs.XSParser;
24 import org.apache.ws.jaxme.xs.parser.XSContext;
25 import org.apache.ws.jaxme.xs.parser.TextSetter;
26 import org.apache.ws.jaxme.xs.parser.XsSAXParser;
27 import org.xml.sax.SAXException JavaDoc;
28
29
30 /**
31  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
32  */

33 public class TextSetterImpl implements TextSetter {
34   private static final Class JavaDoc[] ONE_STRING_CLASS = new Class JavaDoc[]{String JavaDoc.class};
35
36   protected XSContext getData() {
37     return XSParser.getRunningInstance().getContext();
38   }
39
40   protected boolean isIgnorable(String JavaDoc pText) {
41     for (int i = 0; i < pText.length(); i++) {
42       if (!Character.isWhitespace(pText.charAt(i))) {
43         return false;
44       }
45     }
46     return true;
47   }
48
49   public void addText(String JavaDoc pText) throws SAXException JavaDoc {
50     XsSAXParser handler = (XsSAXParser) getData().getCurrentContentHandler();
51     Object JavaDoc bean = handler.getBean();
52     try {
53       Method JavaDoc m = bean.getClass().getMethod("addText", ONE_STRING_CLASS);
54       m.invoke(bean, new Object JavaDoc[]{pText});
55     } catch (NoSuchMethodException JavaDoc f) {
56       if (isIgnorable(pText)) {
57         return;
58       }
59       throw new IllegalStateException JavaDoc("Embedded text is not supported in the '" + handler.getQName() + "' element:" + pText);
60     } catch (IllegalAccessException JavaDoc e) {
61       throw new IllegalStateException JavaDoc("Embedded text is not supported in the '" + handler.getQName() + "' element:" + pText);
62     } catch (InvocationTargetException JavaDoc e) {
63       Throwable JavaDoc t = e.getTargetException();
64       if (t instanceof RuntimeException JavaDoc) {
65         throw (RuntimeException JavaDoc) t;
66       } else if (t instanceof SAXException JavaDoc) {
67         throw (SAXException JavaDoc) t;
68       } else {
69         throw new UndeclaredThrowableException JavaDoc(t);
70       }
71     }
72   }
73 }
74
Popular Tags