KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > typing > XMLTypeInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  * Copyright (C) 2003 XQuark Group.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
19  * You can also get it at http://www.gnu.org/licenses/lgpl.html
20  *
21  * For more information on this software, see http://www.xquark.org.
22  */

23
24 package org.xquark.jdbc.typing;
25
26 import java.io.StringReader JavaDoc;
27
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xquark.schema.*;
31
32 /** This class describes the XML type of a datum to map.
33  * <p>This class is to be used in a custom mapping context to indicate the type
34  * of the datum returned by the user generator.</p>
35  */

36 public class XMLTypeInfo implements SchemaConstants
37 {
38     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40     
41     //////////////////////////////////////////////////////////////////////////
42
// DATA
43
//////////////////////////////////////////////////////////////////////////
44
/** The XML Schema API simple type
45      */

46     protected SimpleType sType;
47     
48     protected String JavaDoc stringType;
49
50     //////////////////////////////////////////////////////////////////////////
51
// CONSTRUCTORS & ACCESSORS
52
//////////////////////////////////////////////////////////////////////////
53
protected XMLTypeInfo() {}
54     
55     /** Constructor.
56      * @param XMLType an XML Schema type as an XML fragment.
57      * @see org.xquark.repository.UserGenerator#getXMLType()
58      */

59     public XMLTypeInfo(String JavaDoc XMLType)
60     {
61         setXMLType(XMLType);
62     }
63     
64     /** Constructor.
65      * @param XMLType an XML Schema API SimpleType object.
66      */

67     public XMLTypeInfo(SimpleType XMLType)
68     {
69         setXMLType(XMLType);
70     }
71     
72     /** Accessor.
73      * @param XMLType an XML Schema API SimpleType object.
74      */

75     public void setXMLType(SimpleType XMLType)
76     {
77         sType = XMLType;
78     }
79     
80     /** Accessor.
81      * @param XMLType an XML Schema API SimpleType object.
82      */

83     public SimpleType getXMLType()
84     {
85         return sType;
86     }
87     
88     public String JavaDoc getStringXMLType()
89     {
90         return stringType;
91     }
92     
93     /** Accessor.
94      * @param XMLType an XML Schema type as an XML fragment.
95      * @see getXMLType()
96      */

97     public void setXMLType(String JavaDoc XMLType)
98     {
99         XMLType = XMLType.trim();
100         stringType = XMLType;
101         
102         SchemaManager dummyManager = new SchemaManager();
103         int offset = XMLType.indexOf(SIMPLE_TYPE_TAG);
104         final String JavaDoc DUMMY_TYPE = "dummyType";
105         
106         if (offset >= 0) // a fragment to adapt to add a name
107
{
108             StringBuffer JavaDoc dummySchemaString = new StringBuffer JavaDoc("<");
109             dummySchemaString.append(SCHEMA_TAG);
110             dummySchemaString.append(" xmlns=\"");
111             dummySchemaString.append(XMLSCHEMA_URI);
112             dummySchemaString.append("\" ");
113             dummySchemaString.append(TARGET_NAMESPACE_ATTR);
114             dummySchemaString.append("=\"RepositoryTemp\">");
115             dummySchemaString.append(XMLType.substring(0, offset + SIMPLE_TYPE_TAG.length()));
116             dummySchemaString.append(" name=\"");
117             dummySchemaString.append(DUMMY_TYPE);
118             dummySchemaString.append('"');
119             dummySchemaString.append(XMLType.substring(offset + SIMPLE_TYPE_TAG.length()));
120             dummySchemaString.append("</");
121             dummySchemaString.append(SCHEMA_TAG);
122             dummySchemaString.append('>');
123             
124             // loadSchema
125
Schema dummySchema;
126             try {
127                 dummySchema = dummyManager.loadSchema(new InputSource JavaDoc(new StringReader JavaDoc(dummySchemaString.toString())));
128             }
129             catch (SAXException JavaDoc e) {
130                 throw new RuntimeException JavaDoc(e.getMessage());
131             }
132             sType = (SimpleType)dummySchema.getType(DUMMY_TYPE);
133         }
134         else // a simple string representing an XML Schema predefined datatype
135
{
136             sType = (SimpleType)dummyManager.getType(XMLSCHEMA_URI, XMLType);
137             if (sType == null)
138                 throw new RuntimeException JavaDoc("Unknown XML Schema predefined datatype \"" + XMLType + "\" in user generator");
139         }
140     }
141     
142     public String JavaDoc toString()
143     {
144         return sType.toString();
145     }
146         
147 }
148
Popular Tags