KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > xml > betwixt > XmlReader


1 /*
2  * Copyright 2000-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 package org.jahia.utils.xml.betwixt;
17
18 import java.io.InputStream JavaDoc;
19
20 import org.apache.commons.betwixt.XMLIntrospector;
21 import org.apache.commons.betwixt.io.BeanReader;
22 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
23 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
24
25 /**
26  * @author <a HREF="mailto:jason@zenplex.com">Jason van Zyl</a>
27  * @version $Id: XmlReader.java 6702 2004-05-28 15:34:30Z shuber $
28  */

29 public class XmlReader
30 {
31     /** Betwixt BeanReader. */
32     private BeanReader beanReader;
33
34     /** Class type we are reading. */
35     private Class JavaDoc clazz;
36
37     /**
38      * Constructor
39      */

40     public XmlReader(Class JavaDoc clazz)
41     {
42         this.clazz = clazz;
43     }
44
45     /**
46      * Parse the given InputStream into an Object.
47      *
48      * @param is InputSteam to parse.
49      * @return Object
50      */

51     public Object JavaDoc parse(InputStream JavaDoc is)
52         throws Exception JavaDoc
53     {
54         if (beanReader == null)
55         {
56             beanReader = createBeanReader(clazz);
57         }
58
59         return beanReader.parse(is);
60     }
61
62     /**
63      * Create the Betwixt BeanReader for processing this class.
64      *
65      * @param clazz Type of Class to parse.
66      * @return BeanReader capable of parsing the type of clazz.
67      */

68     private BeanReader createBeanReader(Class JavaDoc clazz)
69         throws Exception JavaDoc
70     {
71         BeanReader reader = new BeanReader();
72         reader.setXMLIntrospector(createXMLIntrospector());
73         reader.registerBeanClass(clazz);
74         return reader;
75     }
76
77     /**
78      * Create Betwixt XMLIntrospector.
79      *
80      * @return XMLIntrospector.
81      */

82     private XMLIntrospector createXMLIntrospector()
83     {
84         XMLIntrospector introspector = new XMLIntrospector();
85
86         introspector.setAttributesForPrimitives(false);
87         introspector.setNameMapper(new DecapitalizeNameMapper());
88         introspector.setPluralStemmer(new DefaultPluralStemmer());
89
90         return introspector;
91     }
92 }
93
Popular Tags