Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/xml/XsdTypeMap.java


1   /*
2    * XSTypeMap.java
3    *
4    * Created on 8 aprile 2003, 19.09
5       Javatools (modified version) - Some useful general classes.
6       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
7   
8       This program is free software; you can redistribute it and/or modify
9       it under the terms of the GNU General Public License as published by
10      the Free Software Foundation; either version 2 of the License, or
11      (at your option) any later version.
12  
13      This program is distributed in the hope that it will be useful,
14      but WITHOUT ANY WARRANTY; without even the implied warranty of
15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16      GNU General Public License for more details.
17  
18      You should have received a copy of the GNU General Public License
19      along with this program; if not, write to the Free Software
20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  
22      Contact me at: brenmcguire@users.sourceforge.net
23   */
24  
25  package javatools.xml;
26  
27  import java.util.*;
28  import java.text.DateFormat;
29  import org.xml.sax.InputSource;
30  import de.ispsoft.jaxme.generator.*;
31  import javatools.text.AppropriateFormat;
32  
33  /**
34   *
35   * @author Antonio Petrelli
36   * @version 0.2.0
37   */
38  public class XsdTypeMap {
39      
40      /** Creates a new instance of XSTypeMap */
41      public XsdTypeMap(String file) {
42          XsdSchemaReader reader;
43          Schema schema;
44          
45          try {
46              reader = new XsdSchemaReader();
47              schema = reader.parse(file);
48              buildMaps(schema);
49              formatter = new AppropriateFormat();
50          }
51          catch(Exception e) {
52              System.out.println("[XsdTypeMap (constructor)] "+e.getMessage());
53          }
54      }
55      
56      public XsdTypeMap(InputSource is) {
57          XsdSchemaReader reader;
58          Schema schema;
59          
60          try {
61              reader = new XsdSchemaReader();
62              schema = reader.parse(is);
63              buildMaps(schema);
64              formatter = new AppropriateFormat();
65          }
66          catch(Exception e) {
67              System.out.println("[XsdTypeMap (constructor)] "+e.getMessage());
68          }
69      }
70      
71      public void setDateFormat(DateFormat dFormat) {
72          formatter.setDateFormat(dFormat);
73      }
74      
75      public String getType(String element) {
76          return (String) element2type.get(element);
77      }
78      
79      public String getType(String element, String attribute) {
80          HashMap internalMap;
81          String tempValue;
82          
83          tempValue = null;
84          internalMap = (HashMap) attribute2type.get(element);
85          if (internalMap != null)
86              tempValue = (String) internalMap.get(attribute);
87          return tempValue;
88      }
89      
90      public Object buildObject(String element, String textToParse) {
91          String type;
92          Object tempObject;
93          
94          tempObject = null;
95          type = getType(element);
96          if (type != null)
97              tempObject = formatter.parseObject(textToParse, type);
98          return tempObject;
99      }
100     
101     public Object buildObject(String element, String attribute, String textToParse) {
102         String type;
103         Object tempObject;
104         
105         tempObject = null;
106         type = getType(element, attribute);
107         if (type != null)
108             tempObject = formatter.parseObject(textToParse, type);
109         return tempObject;
110     }
111 
112     private HashMap element2type;
113     private HashMap attribute2type;
114     private AppropriateFormat formatter;
115     
116     private void buildMaps(Schema schema) {
117         SchemaElement element, parent;
118         HashMap internalMap;
119         Iterator elementIt;
120         String parentName;
121         
122         element2type = new HashMap();
123         attribute2type = new HashMap();
124         elementIt = schema.getSchemaElements();
125         while (elementIt.hasNext()) {
126             element = (SchemaElement) elementIt.next();
127             addElement(element);
128         }
129     }
130     
131     private void addElement(SchemaElement element) {
132         SchemaElement parent;
133         SchemaComplexElement complex;
134         HashMap internalMap;
135         Iterator elementIt;
136         String fieldName;
137         
138         if (!element.isAttribute()) {
139             fieldName = element.getName();
140             element2type.put(fieldName, element.getFieldType());
141             if (element.isComplex()) {
142                 complex = (SchemaComplexElement) element;
143                 internalMap = new HashMap();
144                 attribute2type.put(fieldName, internalMap);
145                 elementIt = complex.getAttributes();
146                 while(elementIt.hasNext())
147                     addAttribute(internalMap, (SchemaElement) elementIt.next());
148                 elementIt = complex.getChilds();
149                 while(elementIt.hasNext())
150                     addElement((SchemaElement) elementIt.next());
151             }
152         }
153     }
154     
155     private void addAttribute(HashMap map, SchemaElement element) {
156         map.put(element.getName(), element.getFieldType());
157     }
158 }