Statistics
| Revision:

root / trunk / libraries / libGPE / src / org / gvsig / xmlschema / utils / SchemaDocumentBuilder.java @ 12175

History | View | Annotate | Download (4.38 KB)

1
package org.gvsig.xmlschema.utils;
2

    
3
import java.io.InputStream;
4

    
5
import javax.xml.parsers.DocumentBuilder;
6
import javax.xml.parsers.DocumentBuilderFactory;
7
import javax.xml.parsers.ParserConfigurationException;
8

    
9
import org.gvsig.xmlschema.exceptions.SchemaCreationException;
10
import org.gvsig.xmlschema.exceptions.TypeNotFoundException;
11
import org.gvsig.xmlschema.som.IXSSchema;
12
import org.gvsig.xmlschema.som.impl.XSSchemaImpl;
13
import org.w3c.dom.Document;
14
import org.w3c.dom.Element;
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id: SchemaDocumentBuilder.java 12175 2007-06-14 16:15:05Z jorpiell $
59
 * $Log$
60
 * Revision 1.1  2007-06-14 16:15:03  jorpiell
61
 * builds to create the jars generated and add the schema code to the libGPEProject
62
 *
63
 * Revision 1.1  2007/06/14 13:50:07  jorpiell
64
 * The schema jar name has been changed
65
 *
66
 * Revision 1.2  2007/06/07 14:54:13  jorpiell
67
 * Add the schema support
68
 *
69
 * Revision 1.1  2007/05/30 12:25:48  jorpiell
70
 * Add the element collection
71
 *
72
 * Revision 1.1  2007/05/28 12:38:03  jorpiell
73
 * Some bugs fixed
74
 *
75
 *
76
 */
77
/**
78
 * Singleton to create schemas
79
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
80
 */
81
public class SchemaDocumentBuilder {
82
        private static SchemaDocumentBuilder instance = null;
83
        private DocumentBuilder builder = null;
84
        
85
        /**
86
         * This method cretaes the singleton instance
87
         *
88
         */
89
        private synchronized static void createInstance() {
90
                if (instance == null) { 
91
                        instance = new SchemaDocumentBuilder();
92
                }
93
        }
94
        
95
        /**
96
         * @return the schema builder instance
97
         */
98
        public static SchemaDocumentBuilder getInstance() {
99
                if (instance == null){
100
                        createInstance();
101
                }
102
                return instance;
103
        }
104
        
105
        /**
106
         * Creates a new schema from a namespace
107
         * @param namespaceURI
108
         * Schema namspace
109
         * @return
110
         * A new schema
111
         * @throws SchemaCreationException
112
         */        
113
        public IXSSchema createXSSchema(String namespaceURI) throws SchemaCreationException {
114
                IXSSchema schema;
115
                try {
116
                        Document document = getBuilder().newDocument();
117
                        Element element = document.createElement(SchemaTags.SCHEMA_ROOT);
118
                        element.setAttributeNS(SchemaTags.XSD_NS_URI, 
119
                                        SchemaTags.XMLNS_NS + ":" + SchemaTags.XS_NS,
120
                                        SchemaTags.XSD_NS_URI);
121
                        element.setAttributeNS(SchemaTags.GML_NS_URI, 
122
                                        SchemaTags.XMLNS_NS + ":" + SchemaTags.GML_NS,
123
                                        SchemaTags.GML_NS_URI);
124
                        element.setAttribute(SchemaTags.TARGET_NAMESPACE,
125
                                        namespaceURI);
126
                        document.appendChild(element);
127
                        schema = new XSSchemaImpl(document);
128
                } catch (ParserConfigurationException e) {
129
                        throw new SchemaCreationException(e);
130
                } catch (TypeNotFoundException e) {
131
                        throw new SchemaCreationException(e);
132
                }
133
                return schema;
134
        }
135
        
136
        /**
137
         * Parse a schema file
138
         * @param is
139
         * XSD schema file
140
         * @return
141
         * A schema
142
         * @throws SchemaCreationException 
143
         */
144
        public IXSSchema parse(InputStream is) throws SchemaCreationException {
145
                try {
146
                        Document document = getBuilder().parse(is);
147
                        return new XSSchemaImpl(document);
148
                } catch (Exception e) {
149
                        throw new SchemaCreationException(e);
150
                }                
151
        }
152
        
153
        /**
154
         * @return the builder
155
         * @throws ParserConfigurationException 
156
         */
157
        private DocumentBuilder getBuilder() throws ParserConfigurationException {
158
                if (builder == null){
159
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
160
                        builder = factory.newDocumentBuilder();
161
                }
162
                return builder;
163
        }        
164
}