Revision 215
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/impl/DefaultCatalogManager.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 {Iver T.I.} {Task} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.catalog.impl; |
|
29 |
|
|
30 |
import java.lang.reflect.InvocationTargetException; |
|
31 |
import java.net.URI; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Iterator; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.TreeMap; |
|
36 |
|
|
37 |
import org.gvsig.catalog.CatalogManager; |
|
38 |
import org.gvsig.catalog.drivers.ICatalogServiceDriver; |
|
39 |
import org.gvsig.catalog.loaders.LayerLoader; |
|
40 |
import org.gvsig.catalog.metadataxml.XMLNode; |
|
41 |
import org.gvsig.catalog.schemas.Record; |
|
42 |
import org.gvsig.catalog.schemas.Resource; |
|
43 |
import org.gvsig.catalog.schemas.UnknownRecord; |
|
44 |
import org.gvsig.tools.ToolsLocator; |
|
45 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
|
46 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
|
47 |
import org.gvsig.utils.swing.jcomboServer.ServerData; |
|
48 |
import org.slf4j.Logger; |
|
49 |
import org.slf4j.LoggerFactory; |
|
50 |
|
|
51 |
/** |
|
52 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a> |
|
53 |
*/ |
|
54 |
public class DefaultCatalogManager implements CatalogManager { |
|
55 |
private static final Logger logger = LoggerFactory.getLogger(DefaultCatalogManager.class); |
|
56 |
private static final String DRIVER_REGISTER_NAME = "CatalogDrivers"; |
|
57 |
private ArrayList records = null; |
|
58 |
private static TreeMap loadersPool = null; |
|
59 |
private Map<String,ServerData> serversPersistence =null; |
|
60 |
|
|
61 |
public DefaultCatalogManager() { |
|
62 |
super(); |
|
63 |
records = new ArrayList(); |
|
64 |
loadersPool = new TreeMap(); |
|
65 |
} |
|
66 |
|
|
67 |
/* (non-Javadoc) |
|
68 |
* @see org.gvsig.catalog.CatalogManager#getDriver(java.lang.String) |
|
69 |
*/ |
|
70 |
public ICatalogServiceDriver getDriver(String protocol) { |
|
71 |
ExtensionPointManager extensionPointManager = ToolsLocator |
|
72 |
.getExtensionPointManager(); |
|
73 |
ExtensionPoint extensionPoint = extensionPointManager.add(DRIVER_REGISTER_NAME); |
|
74 |
|
|
75 |
Iterator extensions = extensionPoint.iterator(); |
|
76 |
while (extensions.hasNext()){ |
|
77 |
ExtensionPoint.Extension extension = (ExtensionPoint.Extension)extensions.next(); |
|
78 |
ICatalogServiceDriver driver; |
|
79 |
try { |
|
80 |
driver = (ICatalogServiceDriver)extension.create(); |
|
81 |
if (driver.getServiceName().toLowerCase().compareTo(protocol.toLowerCase()) == 0){ |
|
82 |
return driver; |
|
83 |
} |
|
84 |
} catch (InstantiationException e) { |
|
85 |
logger.error("Impossible to create a catalog driver", e); |
|
86 |
} catch (IllegalAccessException e) { |
|
87 |
logger.error("Impossible to create a catalog driver", e); |
|
88 |
} |
|
89 |
} |
|
90 |
return null; |
|
91 |
} |
|
92 |
|
|
93 |
/* (non-Javadoc) |
|
94 |
* @see org.gvsig.catalog.CatalogManager#getDrivers() |
|
95 |
*/ |
|
96 |
public ICatalogServiceDriver[] getDrivers() { |
|
97 |
ICatalogServiceDriver[] drivers = null; |
|
98 |
ExtensionPointManager extensionPointManager = ToolsLocator |
|
99 |
.getExtensionPointManager(); |
|
100 |
ExtensionPoint extensionPoint = extensionPointManager.add(DRIVER_REGISTER_NAME); |
|
101 |
drivers = new ICatalogServiceDriver[extensionPoint.getCount()]; |
|
102 |
Iterator extensions = extensionPoint.iterator(); |
|
103 |
int i = 0; |
|
104 |
while (extensions.hasNext()){ |
|
105 |
ExtensionPoint.Extension extension = (ExtensionPoint.Extension)extensions.next(); |
|
106 |
ICatalogServiceDriver driver; |
|
107 |
try { |
|
108 |
driver = (ICatalogServiceDriver)extension.create(); |
|
109 |
drivers[i] = driver; |
|
110 |
} catch (InstantiationException e) { |
|
111 |
logger.error("Impossible to create a catalog driver", e); |
|
112 |
} catch (IllegalAccessException e) { |
|
113 |
logger.error("Impossible to create a catalog driver", e); |
|
114 |
} |
|
115 |
|
|
116 |
i++; |
|
117 |
} |
|
118 |
return drivers; |
|
119 |
} |
|
120 |
|
|
121 |
/* (non-Javadoc) |
|
122 |
* @see org.gvsig.catalog.CatalogManager#register(java.lang.String, java.lang.Class) |
|
123 |
*/ |
|
124 |
public void register(String name, Class driver) { |
|
125 |
ExtensionPointManager extensionPointManager = ToolsLocator |
|
126 |
.getExtensionPointManager(); |
|
127 |
ExtensionPoint extensionPoint = extensionPointManager.add(DRIVER_REGISTER_NAME); |
|
128 |
extensionPoint.append(name.toLowerCase(), "", driver); |
|
129 |
} |
|
130 |
|
|
131 |
/* (non-Javadoc) |
|
132 |
* @see org.gvsig.catalog.CatalogManager#addRecord(org.gvsig.catalog.schemas.Record) |
|
133 |
*/ |
|
134 |
public void addRecord(Record record) { |
|
135 |
records.add(record); |
|
136 |
} |
|
137 |
|
|
138 |
/* (non-Javadoc) |
|
139 |
* @see org.gvsig.catalog.CatalogManager#createRecord(java.net.URI, org.gvsig.catalog.metadataxml.XMLNode) |
|
140 |
*/ |
|
141 |
public Record createRecord(URI uri, XMLNode node) { |
|
142 |
for (int i=0 ; i<records.size() ; i++){ |
|
143 |
Record record = (Record)records.get(i); |
|
144 |
if (node != null){ |
|
145 |
if (record.accept(uri, node)){ |
|
146 |
Object[] values = {uri, node}; |
|
147 |
Class[] types = {URI.class, XMLNode.class}; |
|
148 |
try { |
|
149 |
return (Record)record.getClass().getConstructor(types).newInstance(values); |
|
150 |
} catch (Exception e) { |
|
151 |
//It the instance can be created the |
|
152 |
//default record has to be returned |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
return new UnknownRecord(uri,node); |
|
158 |
} |
|
159 |
|
|
160 |
/* (non-Javadoc) |
|
161 |
* @see org.gvsig.catalog.CatalogManager#addLayerLoader(java.lang.String, java.lang.Class) |
|
162 |
*/ |
|
163 |
public void addLayerLoader(String key, Class loader) { |
|
164 |
loadersPool.put(key, loader); |
|
165 |
} |
|
166 |
|
|
167 |
/* (non-Javadoc) |
|
168 |
* @see org.gvsig.catalog.CatalogManager#getLayerLoader(org.gvsig.catalog.schemas.Resource) |
|
169 |
*/ |
|
170 |
public LayerLoader getLayerLoader(Resource resource) |
|
171 |
throws IllegalArgumentException, SecurityException, |
|
172 |
InstantiationException, IllegalAccessException, |
|
173 |
InvocationTargetException, NoSuchMethodException { |
|
174 |
if (loadersPool.containsKey(resource.getType())) { |
|
175 |
Class llClass = (Class) loadersPool.get(resource.getType()); |
|
176 |
Class [] args = {Resource.class}; |
|
177 |
Object [] params = {resource}; |
|
178 |
return (LayerLoader) llClass.getConstructor(args).newInstance(params); |
|
179 |
} |
|
180 |
return null; |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public Map<String,ServerData> getServersPersistence() { |
|
185 |
return serversPersistence; |
|
186 |
} |
|
187 |
|
|
188 |
@Override |
|
189 |
public void setServersPersistence(Map<String,ServerData> serversPersistence) { |
|
190 |
this.serversPersistence=serversPersistence; |
|
191 |
} |
|
192 |
|
|
193 |
} |
|
194 |
|
|
0 | 195 |
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/impl/DefaultCatalogLibrary.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2009 {Iver T.I.} {Task} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.catalog.impl; |
|
29 |
|
|
30 |
import org.gvsig.catalog.CatalogLibrary; |
|
31 |
import org.gvsig.catalog.CatalogLocator; |
|
32 |
import org.gvsig.catalog.CatalogManager; |
|
33 |
import org.gvsig.catalog.csw.drivers.CSWISO19115CatalogServiceDriver; |
|
34 |
import org.gvsig.catalog.csw.drivers.CSWebRIMCatalogServiceDriver; |
|
35 |
import org.gvsig.catalog.schemas.DeegreeISO19115Record; |
|
36 |
import org.gvsig.catalog.schemas.DublinCoreRecord; |
|
37 |
import org.gvsig.catalog.schemas.GeonetworkISO19115Record; |
|
38 |
import org.gvsig.catalog.schemas.IdecISO19115Record; |
|
39 |
import org.gvsig.catalog.schemas.IdeeISO19115Record; |
|
40 |
import org.gvsig.catalog.schemas.Iso19139Record; |
|
41 |
import org.gvsig.catalog.schemas.LaitsGmuEbRIMRecord; |
|
42 |
import org.gvsig.catalog.schemas.LaitsGmuISO19115Record; |
|
43 |
import org.gvsig.catalog.schemas.LaitsGmuServicesRecord; |
|
44 |
import org.gvsig.catalog.srw.drivers.SRWCatalogServiceDriver; |
|
45 |
import org.gvsig.catalog.z3950.drivers.Z3950CatalogServiceDriver; |
|
46 |
import org.gvsig.tools.library.AbstractLibrary; |
|
47 |
import org.gvsig.tools.library.LibraryException; |
|
48 |
|
|
49 |
/** |
|
50 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a> |
|
51 |
*/ |
|
52 |
public class DefaultCatalogLibrary extends AbstractLibrary { |
|
53 |
|
|
54 |
@Override |
|
55 |
public void doRegistration() { |
|
56 |
registerAsImplementationOf(CatalogLibrary.class); |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
protected void doInitialize() throws LibraryException { |
|
61 |
//Register the default CatalogManager |
|
62 |
CatalogLocator.registerCatalogManager(DefaultCatalogManager.class); |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
protected void doPostInitialize() throws LibraryException { |
|
67 |
CatalogManager catalogManager = CatalogLocator.getCatalogManager(); |
|
68 |
|
|
69 |
//Register the default catalog drivers |
|
70 |
catalogManager.register("z3950", Z3950CatalogServiceDriver.class); |
|
71 |
catalogManager.register("srw", SRWCatalogServiceDriver.class); |
|
72 |
catalogManager.register("csw/iso 19115", CSWISO19115CatalogServiceDriver.class); |
|
73 |
catalogManager.register("csw/ebrim", CSWebRIMCatalogServiceDriver.class); |
|
74 |
|
|
75 |
//Default the records |
|
76 |
catalogManager.addRecord(new GeonetworkISO19115Record()); |
|
77 |
catalogManager.addRecord(new DeegreeISO19115Record()); |
|
78 |
catalogManager.addRecord(new DublinCoreRecord()); |
|
79 |
catalogManager.addRecord(new IdecISO19115Record()); |
|
80 |
catalogManager.addRecord(new IdeeISO19115Record()); |
|
81 |
catalogManager.addRecord(new Iso19139Record()); |
|
82 |
catalogManager.addRecord(new LaitsGmuISO19115Record()); |
|
83 |
catalogManager.addRecord(new LaitsGmuServicesRecord()); |
|
84 |
catalogManager.addRecord(new LaitsGmuEbRIMRecord()); |
|
85 |
} |
|
86 |
} |
|
0 | 87 |
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/metadataxml/XMLTree.java | ||
---|---|---|
1 |
|
|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
3 |
* |
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
19 |
* |
|
20 |
* For more information, contact: |
|
21 |
* |
|
22 |
* Generalitat Valenciana |
|
23 |
* Conselleria d'Infraestructures i Transport |
|
24 |
* Av. Blasco Ib??ez, 50 |
|
25 |
* 46010 VALENCIA |
|
26 |
* SPAIN |
|
27 |
* |
|
28 |
* +34 963862235 |
|
29 |
* gvsig@gva.es |
|
30 |
* www.gvsig.gva.es |
|
31 |
* |
|
32 |
* or |
|
33 |
* |
|
34 |
* IVER T.I. S.A |
|
35 |
* Salamanca 50 |
|
36 |
* 46005 Valencia |
|
37 |
* Spain |
|
38 |
* |
|
39 |
* +34 963163400 |
|
40 |
* dac@iver.es |
|
41 |
*/ |
|
42 |
package org.gvsig.catalog.metadataxml; |
|
43 |
import java.io.ByteArrayInputStream; |
|
44 |
import java.io.File; |
|
45 |
import java.io.FileWriter; |
|
46 |
import java.io.IOException; |
|
47 |
import java.io.InputStream; |
|
48 |
import java.io.StringWriter; |
|
49 |
import java.util.ArrayList; |
|
50 |
import java.util.StringTokenizer; |
|
51 |
|
|
52 |
import org.apache.xml.serialize.OutputFormat; |
|
53 |
import org.apache.xml.serialize.XMLSerializer; |
|
54 |
import org.gvsig.catalog.utils.Strings; |
|
55 |
import org.w3c.dom.Document; |
|
56 |
|
|
57 |
/** |
|
58 |
* Utils to parse XML trees using DOM |
|
59 |
* @author Jorge Piera Llodra (piera_jor@gva.es) |
|
60 |
*/ |
|
61 |
public class XMLTree { |
|
62 |
public static final String SEPARATOR = "->"; |
|
63 |
|
|
64 |
/** |
|
65 |
* Create a XML node from a File |
|
66 |
* @return XML node |
|
67 |
* @param file File name |
|
68 |
*/ |
|
69 |
public static XMLNode xmlToTree(File file) { |
|
70 |
try { |
|
71 |
return new XMLNode(file); |
|
72 |
} catch (Exception e) { |
|
73 |
// TODO Auto-generated catch block |
|
74 |
e.printStackTrace(); |
|
75 |
return null; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* Create a XML node from a InputStream |
|
81 |
* @return XML node |
|
82 |
* @param stream InputStream |
|
83 |
*/ |
|
84 |
public static XMLNode xmlToTree(InputStream stream) { |
|
85 |
try { |
|
86 |
return new XMLNode(stream); |
|
87 |
} catch (Exception e) { |
|
88 |
// TODO Auto-generated catch block |
|
89 |
//e.printStackTrace(); |
|
90 |
return null; |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* Create a XML node from a String |
|
96 |
* @return XML node |
|
97 |
* @param stream InputStream |
|
98 |
*/ |
|
99 |
public static XMLNode xmlToTree(String string) { |
|
100 |
try { |
|
101 |
return new XMLNode(new ByteArrayInputStream(string.getBytes())); |
|
102 |
} catch (Exception e) { |
|
103 |
// Unconvertible UTF-8 character |
|
104 |
string = Strings.replace(string, |
|
105 |
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>", |
|
106 |
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); |
|
107 |
try { |
|
108 |
return new XMLNode(new ByteArrayInputStream(string.getBytes())); |
|
109 |
} catch (Exception e1) { |
|
110 |
// TODO Auto-generated catch block |
|
111 |
e1.printStackTrace(); |
|
112 |
return null; |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* Devuelve un fichero que crea a partir de un arbol XML |
|
119 |
* @return Devuelve el fichero escrito |
|
120 |
* @param dom Documento en XML |
|
121 |
* @param nombreFichero Nombre del fichero. |
|
122 |
*/ |
|
123 |
public static File treeToXML(Document dom, String nombreFichero) { |
|
124 |
OutputFormat format = null; |
|
125 |
StringWriter stringOut = null; |
|
126 |
XMLSerializer serial = null; |
|
127 |
FileWriter file = null; |
|
128 |
//Creamos un fichero para almacenar la respuesta |
|
129 |
File file_answer = new File(nombreFichero); |
|
130 |
format = new OutputFormat(dom); |
|
131 |
format.setEncoding("ISO-8859-1"); |
|
132 |
format.setIndent(5); |
|
133 |
stringOut = new StringWriter(); |
|
134 |
serial = new XMLSerializer(stringOut, format); |
|
135 |
try { |
|
136 |
serial.asDOMSerializer(); |
|
137 |
serial.serialize(dom); |
|
138 |
file = new FileWriter(file_answer); |
|
139 |
file.write(stringOut.toString()); |
|
140 |
file.close(); |
|
141 |
} catch (IOException e) { |
|
142 |
// TODO Auto-generated catch block |
|
143 |
e.printStackTrace(); |
|
144 |
return null; |
|
145 |
} |
|
146 |
return file_answer; |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* Busca un Nodo dado una ruta de nodo del tipo "nodoRaiz:nodoPrimerNivel:...:nodoNivelN": |
|
151 |
* @return Devuelve el Nodo que corresponde a la ruta correcta o 'null' si no |
|
152 |
* lo encuentra |
|
153 |
* @param nodoRaiz Nodo a partir del cual se quiere hacer la b?squeda |
|
154 |
* @param etiqueta Ruta del campo que queremos buscar, separando los niveles por ':' |
|
155 |
*/ |
|
156 |
public static XMLNode searchNode(XMLNode nodoRaiz, String etiqueta) { |
|
157 |
XMLNode[] nodes = searchMultipleNode(nodoRaiz, etiqueta); |
|
158 |
if ((nodes != null) && (nodes.length > 0)) { |
|
159 |
return nodes[0]; |
|
160 |
} else { |
|
161 |
return null; |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
/** |
|
166 |
* Busca el padre de un Nodo dado una ruta de nodo del tipo "nodoRaiz:nodoPrimerNivel:...:nodoNivelN": |
|
167 |
* |
|
168 |
* @param nodoRaiz Nodo a partir del cual se quiere hacer la b?squeda |
|
169 |
* @param etiqueta Ruta del campo que queremos buscar, separando los niveles por ':' |
|
170 |
* |
|
171 |
* @return Devuelve el Nodo padre que corresponde a la ruta correcta o 'null' si no |
|
172 |
* lo encuentra |
|
173 |
* @param rootNode |
|
174 |
* @param label |
|
175 |
*/ |
|
176 |
public static XMLNode searchParentNode(XMLNode rootNode, String label) { |
|
177 |
StringTokenizer sti = new StringTokenizer(label, "->"); |
|
178 |
if (rootNode == null) { |
|
179 |
return null; |
|
180 |
} |
|
181 |
|
|
182 |
XMLNode currentNode = rootNode.getSubNode(0); |
|
183 |
XMLNode parentNode = rootNode; |
|
184 |
|
|
185 |
//A cuantos niveles est? el TOKEN |
|
186 |
int niveles = sti.countTokens(); |
|
187 |
String nombreNodo = cutNamespace(sti.nextToken()); |
|
188 |
int nivelActual = 1; |
|
189 |
int i = 0; |
|
190 |
while (i < parentNode.getNumSubNodes()) { |
|
191 |
if (nombreNodo.equals(cutNamespace(currentNode.getName()))) { |
|
192 |
if (niveles == nivelActual) { |
|
193 |
return parentNode; |
|
194 |
} |
|
195 |
parentNode = currentNode; |
|
196 |
currentNode = currentNode.getSubNode(0); |
|
197 |
nombreNodo = sti.nextToken(); |
|
198 |
nivelActual++; |
|
199 |
i = 0; |
|
200 |
} else { |
|
201 |
currentNode = currentNode.getSubNode(i); |
|
202 |
i ++; |
|
203 |
} |
|
204 |
} |
|
205 |
return null; |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* Hace una busqueda de un atributo de un nodo |
|
210 |
* |
|
211 |
* @param nodo Nodo del que se quiere buscar el atributo |
|
212 |
* @param nombreAtributo Nombre del atributo |
|
213 |
* |
|
214 |
* @return Valor del atributo, o null si no lo ha encontrado |
|
215 |
* @param node |
|
216 |
* @param attributeName |
|
217 |
*/ |
|
218 |
public static String searchAtribute(XMLNode node, String attributeName) { |
|
219 |
return node.getAttribute(attributeName); |
|
220 |
} |
|
221 |
|
|
222 |
/** |
|
223 |
* Hace una busqueda de una etiqueta en un nodo y devuelve |
|
224 |
* su valor |
|
225 |
* |
|
226 |
* @param nodo Nodo del que se quiere buscar el atributo |
|
227 |
* |
|
228 |
* @return Valor de la etiqueta |
|
229 |
* @param node |
|
230 |
* @param etiqueta Nombre de la etiqueta |
|
231 |
*/ |
|
232 |
public static String searchNodeValue(XMLNode node, String etiqueta) { |
|
233 |
XMLNode nodoB = searchNode(node, etiqueta); |
|
234 |
if (nodoB == null) |
|
235 |
return null; |
|
236 |
return nodoB.getText(); |
|
237 |
|
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Hace una busqueda de una etiqueta en un nodo y devuelve |
|
242 |
* el valor del atributo correspondiente |
|
243 |
* |
|
244 |
* @param nodo Nodo del que se quiere buscar el atributo |
|
245 |
* |
|
246 |
* @return Valor del atributo de la etiqueta o null |
|
247 |
* @param node |
|
248 |
* @param etiqueta Nombre de la etiqueta |
|
249 |
* @param atributo |
|
250 |
*/ |
|
251 |
public static String searchNodeAtribute(XMLNode node, String etiqueta, String atributo) { |
|
252 |
XMLNode nodoB = searchNode(node, etiqueta); |
|
253 |
if (nodoB == null) { |
|
254 |
return null; |
|
255 |
} else { |
|
256 |
return searchAtribute(nodoB, atributo); |
|
257 |
} |
|
258 |
} |
|
259 |
|
|
260 |
/** |
|
261 |
* Hace una busqueda de nodos que se llaman igual y devuleve el valor |
|
262 |
* |
|
263 |
* @param parentLabel Ruta del campo que queremos buscar, separando los niveles por '->' |
|
264 |
* |
|
265 |
* @return Un vector con valores de las etiquetas |
|
266 |
* @param rootNode Nodo a partir del cual se quiere hacer la b?squeda |
|
267 |
* @param label Node label |
|
268 |
*/ |
|
269 |
public static String[] searchMultipleNodeValue(XMLNode rootNode, String label) { |
|
270 |
XMLNode[] nodes = searchMultipleNode(rootNode, label); |
|
271 |
if ((nodes == null) || (nodes.length == 0)) { |
|
272 |
return null; |
|
273 |
} |
|
274 |
String[] values = new String[nodes.length]; |
|
275 |
for (int i = 0; i < nodes.length; i++) |
|
276 |
//if (nodes[i].getFirstChild() != null) { |
|
277 |
values[i] = nodes[i].getText(); |
|
278 |
//} |
|
279 |
return values; |
|
280 |
} |
|
281 |
|
|
282 |
/** |
|
283 |
* Hace una busqueda de nodos que se llaman igual desde uno dado(sin recursividad) |
|
284 |
* |
|
285 |
* @param etiqueta Ruta del campo que queremos buscar, separando los niveles por ':' |
|
286 |
* |
|
287 |
* @return Un vector con los nodos que ha encontrado |
|
288 |
* @param nodoRaiz Nodo a partir del cual se quiere hacer la b?squeda |
|
289 |
* @param label |
|
290 |
*/ |
|
291 |
public static XMLNode[] searchMultipleNode(XMLNode nodoRaiz, String label) { |
|
292 |
ArrayList rootNodes = new ArrayList(); |
|
293 |
ArrayList leafNodes = new ArrayList(); |
|
294 |
String firstLabel = null; |
|
295 |
leafNodes.add(nodoRaiz); |
|
296 |
int level = getLevelNumber(label); |
|
297 |
int k = 1; |
|
298 |
while (k <= level) { |
|
299 |
firstLabel = cutNamespace(getParentLabel(label)); |
|
300 |
label = getChildLabel(label); |
|
301 |
rootNodes = new ArrayList(leafNodes); |
|
302 |
leafNodes.clear(); |
|
303 |
for (int i = 0; i < rootNodes.size(); i++) { |
|
304 |
XMLNode root = (XMLNode) rootNodes.get(i); |
|
305 |
if (root != null) { |
|
306 |
XMLNode[] nodes = root.getSubnodes(); |
|
307 |
for (int j = 0; j < nodes.length; j++) { |
|
308 |
if (cutNamespace(nodes[j].getName()).equals(firstLabel)) { |
|
309 |
leafNodes.add(nodes[j]); |
|
310 |
} |
|
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
k++; |
|
315 |
} |
|
316 |
XMLNode[] nodes = new XMLNode[leafNodes.size()]; |
|
317 |
|
|
318 |
for (int i = 0; i < leafNodes.size(); i++) |
|
319 |
nodes[i] = (XMLNode) leafNodes.get(i); |
|
320 |
return nodes; |
|
321 |
} |
|
322 |
|
|
323 |
/** |
|
324 |
* Gets the parent node label |
|
325 |
* |
|
326 |
* |
|
327 |
* @return The parent node label |
|
328 |
* @param nodeLabel Node label |
|
329 |
*/ |
|
330 |
private static String getParentLabel(String nodeLabel) { |
|
331 |
return separateParams(nodeLabel,SEPARATOR)[0]; |
|
332 |
} |
|
333 |
|
|
334 |
/** |
|
335 |
* It cuts an String in an array of Strings separated by a pattern |
|
336 |
* |
|
337 |
* |
|
338 |
* @return An array of Strings |
|
339 |
* @param text Text to cut |
|
340 |
* @param separator Pattent to find * |
|
341 |
*/ |
|
342 |
private static String[] separateParams(String text, String separator) { |
|
343 |
return text.split(separator); |
|
344 |
} |
|
345 |
|
|
346 |
/** |
|
347 |
* Gets the node label |
|
348 |
* |
|
349 |
* |
|
350 |
* @return The node label |
|
351 |
* @param nodeLabel Node label |
|
352 |
*/ |
|
353 |
private static String getChildLabel(String nodeLabel) { |
|
354 |
String st = null; |
|
355 |
String[] labels = separateParams(nodeLabel,SEPARATOR); |
|
356 |
|
|
357 |
if (labels.length == 1){ |
|
358 |
return labels[0]; |
|
359 |
} |
|
360 |
|
|
361 |
st = labels[1]; |
|
362 |
|
|
363 |
for (int i=2 ; i<labels.length ; i++) |
|
364 |
st = st + SEPARATOR + labels[i]; |
|
365 |
|
|
366 |
return st; |
|
367 |
} |
|
368 |
|
|
369 |
/** |
|
370 |
* @return |
|
371 |
* @param nodeLabel |
|
372 |
*/ |
|
373 |
private static int getLevelNumber(String nodeLabel) { |
|
374 |
String[] labels = separateParams(nodeLabel,SEPARATOR); |
|
375 |
return labels.length; |
|
376 |
} |
|
377 |
|
|
378 |
/** |
|
379 |
* Remove the namespace from a label |
|
380 |
* @param label |
|
381 |
*/ |
|
382 |
private static String cutNamespace(String label){ |
|
383 |
if (label == null){ |
|
384 |
return null; |
|
385 |
} |
|
386 |
int i = label.indexOf(":"); |
|
387 |
if (i > 0){ |
|
388 |
return label.substring(i + 1, label.length()); |
|
389 |
} |
|
390 |
return label; |
|
391 |
} |
|
392 |
} |
|
0 | 393 |
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/metadataxml/XMLTreeNumberOfRecordsAnswer.java | ||
---|---|---|
1 |
|
|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
3 |
* |
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
19 |
* |
|
20 |
* For more information, contact: |
|
21 |
* |
|
22 |
* Generalitat Valenciana |
|
23 |
* Conselleria d'Infraestructures i Transport |
|
24 |
* Av. Blasco Ib??ez, 50 |
|
25 |
* 46010 VALENCIA |
|
26 |
* SPAIN |
|
27 |
* |
|
28 |
* +34 963862235 |
|
29 |
* gvsig@gva.es |
|
30 |
* www.gvsig.gva.es |
|
31 |
* |
|
32 |
* or |
|
33 |
* |
|
34 |
* IVER T.I. S.A |
|
35 |
* Salamanca 50 |
|
36 |
* 46005 Valencia |
|
37 |
* Spain |
|
38 |
* |
|
39 |
* +34 963163400 |
|
40 |
* dac@iver.es |
|
41 |
*/ |
|
42 |
package org.gvsig.catalog.metadataxml; |
|
43 |
import java.io.ByteArrayInputStream; |
|
44 |
|
|
45 |
/** |
|
46 |
* |
|
47 |
* |
|
48 |
* |
|
49 |
* @author Jorge Piera Llodra (piera_jor@gva.es) |
|
50 |
*/ |
|
51 |
public class XMLTreeNumberOfRecordsAnswer { |
|
52 |
|
|
53 |
/** |
|
54 |
* |
|
55 |
* |
|
56 |
* |
|
57 |
* @return |
|
58 |
* @param numberOfRecords |
|
59 |
* @param firstRecord |
|
60 |
* @param lastRecord |
|
61 |
*/ |
|
62 |
public static XMLNode getNode(int numberOfRecords, int firstRecord, int lastRecord) { |
|
63 |
String message = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + |
|
64 |
"<Elements> " + "<NumberOfRecords>" + |
|
65 |
String.valueOf(numberOfRecords) + "</NumberOfRecords>" + |
|
66 |
"<FirstRecord>" + String.valueOf(firstRecord) + "</FirstRecord>" + |
|
67 |
"<LastRecord>" + String.valueOf(lastRecord) + "</LastRecord>" + |
|
68 |
"</Elements>"; |
|
69 |
ByteArrayInputStream buffer = new ByteArrayInputStream(message.getBytes()); |
|
70 |
return XMLTree.xmlToTree(buffer); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* |
|
75 |
* |
|
76 |
* |
|
77 |
* @return |
|
78 |
* @param node |
|
79 |
*/ |
|
80 |
public static int getNumberOfRecords(XMLNode node) { |
|
81 |
return Integer.parseInt(XMLTree.searchNodeValue(node, "NumberOfRecords")); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* |
|
86 |
* |
|
87 |
* |
|
88 |
* @return |
|
89 |
* @param node |
|
90 |
*/ |
|
91 |
public static int getFirstRecord(XMLNode node) { |
|
92 |
return Integer.parseInt(XMLTree.searchNodeValue(node, "FirstRecord")); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* |
|
97 |
* |
|
98 |
* |
|
99 |
* @return |
|
100 |
* @param node |
|
101 |
*/ |
|
102 |
public static int getLastRecord(XMLNode node) { |
|
103 |
return Integer.parseInt(XMLTree.searchNodeValue(node, "LastRecord")); |
|
104 |
} |
|
105 |
} |
|
0 | 106 |
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/metadataxml/XMLNode.java | ||
---|---|---|
1 |
package org.gvsig.catalog.metadataxml; |
|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
3 |
* |
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
19 |
* |
|
20 |
* For more information, contact: |
|
21 |
* |
|
22 |
* Generalitat Valenciana |
|
23 |
* Conselleria d'Infraestructures i Transport |
|
24 |
* Av. Blasco Ib??ez, 50 |
|
25 |
* 46010 VALENCIA |
|
26 |
* SPAIN |
|
27 |
* |
|
28 |
* +34 963862235 |
|
29 |
* gvsig@gva.es |
|
30 |
* www.gvsig.gva.es |
|
31 |
* |
|
32 |
* or |
|
33 |
* |
|
34 |
* IVER T.I. S.A |
|
35 |
* Salamanca 50 |
|
36 |
* 46005 Valencia |
|
37 |
* Spain |
|
38 |
* |
|
39 |
* +34 963163400 |
|
40 |
* dac@iver.es |
|
41 |
*/ |
|
42 |
import java.io.File; |
|
43 |
import java.io.FileWriter; |
|
44 |
import java.io.InputStream; |
|
45 |
import java.io.Writer; |
|
46 |
import java.util.Hashtable; |
|
47 |
import java.util.Vector; |
|
48 |
|
|
49 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
50 |
|
|
51 |
import org.w3c.dom.Document; |
|
52 |
import org.w3c.dom.Element; |
|
53 |
import org.w3c.dom.NamedNodeMap; |
|
54 |
import org.w3c.dom.Node; |
|
55 |
import org.w3c.dom.NodeList; |
|
56 |
|
|
57 |
/** |
|
58 |
* Esta clase representa un XMLNode simplificado |
|
59 |
* Contiene una lista de subnodos y una lista de atributos. |
|
60 |
* Tambi?n tiene una cadena. |
|
61 |
* Soporta la lectura y escritura de y desde un fichero XML. |
|
62 |
* Tambi?n soporta la lectura desde Internet |
|
63 |
* Modificado por jaume |
|
64 |
* |
|
65 |
*/ |
|
66 |
public class XMLNode { |
|
67 |
public static final String ISNOTXML = "NOTXML"; |
|
68 |
private Vector subNodes = new Vector(); |
|
69 |
private String cdata = null; |
|
70 |
private Hashtable attr = new Hashtable(); |
|
71 |
private String nodeName; |
|
72 |
private String text = null; |
|
73 |
private Vector attrKeys = new Vector(); |
|
74 |
private String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
|
75 |
|
|
76 |
/** |
|
77 |
* Constructor, lee de un fichero |
|
78 |
* @param file |
|
79 |
* @author jaume - jaume.dominguez@iver.es |
|
80 |
* Modified by Jorge Piera Llodra - piera_jor@gva.es |
|
81 |
*/ |
|
82 |
public XMLNode(File file) throws Exception { |
|
83 |
this(factory.newDocumentBuilder().parse(file)); |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Constructor. Usando url.openStream() se puede usar |
|
88 |
* para leer info descargada de internet. |
|
89 |
* |
|
90 |
* |
|
91 |
* @param inputstream |
|
92 |
* @throws Exception |
|
93 |
*/ |
|
94 |
public XMLNode(InputStream inputstream) throws Exception { |
|
95 |
this(factory.newDocumentBuilder().parse(inputstream)); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* Contructor, constructor desde un documento DOM |
|
100 |
* |
|
101 |
* |
|
102 |
* @param dom |
|
103 |
* @author jaume - jaume.dominguez@iver.es |
|
104 |
*/ |
|
105 |
public XMLNode(Document dom) throws Exception { |
|
106 |
this((Element)dom.getFirstChild()); |
|
107 |
} |
|
108 |
|
|
109 |
/** |
|
110 |
* Contructor, crea un nodo con su nombre |
|
111 |
* |
|
112 |
* |
|
113 |
* @param name |
|
114 |
*/ |
|
115 |
public XMLNode(String name) throws Exception { |
|
116 |
nodeName = name; |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* Contructor, crea un nodo con su nombre y el texto |
|
121 |
* |
|
122 |
* |
|
123 |
* @param name |
|
124 |
* @param text |
|
125 |
*/ |
|
126 |
public XMLNode(String name, String text) throws Exception { |
|
127 |
nodeName = name; |
|
128 |
this.text = text; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Contructor, desde un elemento DOM |
|
133 |
* |
|
134 |
* |
|
135 |
* @param dom |
|
136 |
*/ |
|
137 |
public XMLNode(Element dom) throws Exception { |
|
138 |
nodeName = dom.getNodeName(); |
|
139 |
NamedNodeMap map = dom.getAttributes(); |
|
140 |
for (int i = 0; i < map.getLength(); i++) { |
|
141 |
Node att = map.item(i); |
|
142 |
addAtrribute(att.getNodeName(), att.getNodeValue()); |
|
143 |
|
|
144 |
} |
|
145 |
NodeList nodeList = dom.getChildNodes(); |
|
146 |
for (int i = 0; i < nodeList.getLength(); i++) { |
|
147 |
Node sub = nodeList.item(i); |
|
148 |
if (sub.getNodeType() == Node.ELEMENT_NODE) { |
|
149 |
addSubNode(new XMLNode((Element)sub)); |
|
150 |
}else if (sub.getNodeType() == Node.CDATA_SECTION_NODE){ |
|
151 |
String sCdata = sub.getNodeValue().trim(); |
|
152 |
if (sCdata.length() > 0) { |
|
153 |
cdata = sCdata; |
|
154 |
} |
|
155 |
}else if (sub.getNodeType() == Node.TEXT_NODE) { |
|
156 |
String s = sub.getNodeValue().trim(); |
|
157 |
if (s.length() > 0) { |
|
158 |
if (text != null) { |
|
159 |
throw new Exception("XMLNode '" + nodeName + "' has 2 Textblocks"); |
|
160 |
} |
|
161 |
text = s; |
|
162 |
} |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* |
|
169 |
* |
|
170 |
* |
|
171 |
* @param s |
|
172 |
*/ |
|
173 |
public void setText(String s) { |
|
174 |
text = s; |
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* |
|
179 |
* |
|
180 |
* |
|
181 |
* @param s |
|
182 |
*/ |
|
183 |
public void addSubNode(XMLNode s) { |
|
184 |
subNodes.add(s); |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* |
|
189 |
* |
|
190 |
* |
|
191 |
* @param name |
|
192 |
* @param value |
|
193 |
*/ |
|
194 |
public void addAtrribute(String name, String value) throws Exception { |
|
195 |
if (attr.containsKey(name)) { |
|
196 |
throw new Exception( |
|
197 |
"XMLNode '" + nodeName + "' already contains Attribute '" + name + "'"); |
|
198 |
} |
|
199 |
attr.put(name, value); |
|
200 |
attrKeys.add(name); |
|
201 |
} |
|
202 |
|
|
203 |
/** |
|
204 |
* |
|
205 |
* |
|
206 |
* |
|
207 |
* @return |
|
208 |
*/ |
|
209 |
public int getNumSubNodes() { |
|
210 |
return subNodes.size(); |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* |
|
215 |
* |
|
216 |
* |
|
217 |
* @return |
|
218 |
*/ |
|
219 |
public String getName() { |
|
220 |
return nodeName; |
|
221 |
} |
|
222 |
|
|
223 |
/** |
|
224 |
* |
|
225 |
* |
|
226 |
* |
|
227 |
* @return |
|
228 |
*/ |
|
229 |
public String getText() { |
|
230 |
return text; |
|
231 |
} |
|
232 |
|
|
233 |
/** |
|
234 |
* |
|
235 |
* |
|
236 |
* |
|
237 |
* @return |
|
238 |
*/ |
|
239 |
public String getCdata() { |
|
240 |
return cdata; |
|
241 |
} |
|
242 |
|
|
243 |
/** |
|
244 |
* |
|
245 |
* |
|
246 |
* |
|
247 |
* @return |
|
248 |
* @param index |
|
249 |
*/ |
|
250 |
public XMLNode getSubNode(int index) { |
|
251 |
return (XMLNode)subNodes.get(index); |
|
252 |
} |
|
253 |
|
|
254 |
/** |
|
255 |
* |
|
256 |
* |
|
257 |
* |
|
258 |
* @return |
|
259 |
*/ |
|
260 |
public XMLNode[] getSubnodes() { |
|
261 |
XMLNode[] xmlNodes = new XMLNode[getNumSubNodes()]; |
|
262 |
for (int i=0 ; i<getNumSubNodes() ; i++){ |
|
263 |
xmlNodes[i] = getSubNode(i); |
|
264 |
} |
|
265 |
return xmlNodes; |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* |
|
270 |
* |
|
271 |
* |
|
272 |
* @return |
|
273 |
*/ |
|
274 |
public Vector getAttributeNames() { |
|
275 |
return attrKeys; |
|
276 |
} |
|
277 |
|
|
278 |
/** |
|
279 |
* |
|
280 |
* |
|
281 |
* |
|
282 |
* @param wr |
|
283 |
*/ |
|
284 |
public void write(Writer wr) throws Exception { |
|
285 |
wwrite("", wr); |
|
286 |
} |
|
287 |
|
|
288 |
/** |
|
289 |
* Escribe el c?digo XML de este objeto con su identaci?n |
|
290 |
* |
|
291 |
* |
|
292 |
* @param pre |
|
293 |
* @param wr |
|
294 |
*/ |
|
295 |
private void wwrite(String pre, Writer wr) throws Exception { |
|
296 |
wr.write(pre + "<" + nodeName); |
|
297 |
for (int i = 0; i < attrKeys.size(); i++) { |
|
298 |
String name = (String)attrKeys.get(i); |
|
299 |
String val = (String)attr.get(name); |
|
300 |
wr.write(" " + name + "='" + val + "'"); |
|
301 |
} |
|
302 |
if (getNumSubNodes() == 0 && text == null) { |
|
303 |
wr.write("/>\n"); |
|
304 |
} |
|
305 |
else { |
|
306 |
wr.write(">"); |
|
307 |
if (text != null) { |
|
308 |
wr.write(text); |
|
309 |
} |
|
310 |
if (getNumSubNodes() > 0) { |
|
311 |
wr.write("\n"); |
|
312 |
for (int i = 0; i < subNodes.size(); i++) { |
|
313 |
if (getSubNode(i) != null) { |
|
314 |
getSubNode(i).wwrite(pre + " ", wr); |
|
315 |
} |
|
316 |
} |
|
317 |
wr.write(pre + "</" + nodeName + ">\n"); |
|
318 |
} |
|
319 |
else { |
|
320 |
wr.write("</" + nodeName + ">\n"); |
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
|
|
325 |
/** |
|
326 |
* |
|
327 |
* |
|
328 |
* |
|
329 |
* @return |
|
330 |
* @param key |
|
331 |
*/ |
|
332 |
public String getAttribute(String key) { |
|
333 |
return (String)attr.get(key); |
|
334 |
} |
|
335 |
|
|
336 |
/** |
|
337 |
* |
|
338 |
* |
|
339 |
* |
|
340 |
* @return |
|
341 |
* @param key |
|
342 |
*/ |
|
343 |
public double getDoubleAttribute(String key) { |
|
344 |
return Double.parseDouble((String)attr.get(key)); |
|
345 |
} |
|
346 |
|
|
347 |
/** |
|
348 |
* |
|
349 |
* |
|
350 |
* |
|
351 |
* @return |
|
352 |
* @param key |
|
353 |
*/ |
|
354 |
public boolean getBoolAttribute(String key) { |
|
355 |
if (!hasAttribute(key)) { |
|
356 |
return false; |
|
357 |
} |
|
358 |
return new Boolean((String)attr.get(key)).booleanValue(); |
|
359 |
} |
|
360 |
|
|
361 |
/** |
|
362 |
* |
|
363 |
* |
|
364 |
* |
|
365 |
* @return |
|
366 |
* @param key |
|
367 |
*/ |
|
368 |
public int getIntAttribute(String key) { |
|
369 |
return Integer.parseInt((String)attr.get(key)); |
|
370 |
} |
|
371 |
|
|
372 |
/** |
|
373 |
* |
|
374 |
* |
|
375 |
* |
|
376 |
* @return |
|
377 |
* @param key |
|
378 |
*/ |
|
379 |
public boolean hasAttribute(String key) { |
|
380 |
return attr.containsKey(key); |
|
381 |
} |
|
382 |
// escribe al fichero |
|
383 |
|
|
384 |
/** |
|
385 |
* |
|
386 |
* |
|
387 |
* |
|
388 |
* @param f |
|
389 |
*/ |
|
390 |
public void write(File f) throws Exception { |
|
391 |
FileWriter fw = new FileWriter(f); |
|
392 |
fw.write(header); |
|
393 |
write(fw); |
|
394 |
fw.flush(); |
|
395 |
fw.close(); |
|
396 |
} |
|
397 |
|
|
398 |
/** |
|
399 |
* @return |
|
400 |
*/ |
|
401 |
public String toString() { |
|
402 |
return this.getName(); |
|
403 |
} |
|
404 |
|
|
405 |
static DocumentBuilderFactory factory; |
|
406 |
static { |
|
407 |
factory = DocumentBuilderFactory.newInstance(); |
|
408 |
factory.setValidating(false); |
|
409 |
factory.setNamespaceAware(false); |
|
410 |
factory.setIgnoringComments(true); |
|
411 |
} |
|
412 |
|
|
413 |
/** |
|
414 |
* @param header |
|
415 |
*/ |
|
416 |
public void setHeader(String header) { |
|
417 |
this.header = header; |
|
418 |
} |
|
419 |
|
|
420 |
/** |
|
421 |
* This method prints all the child nodes. It is used only for |
|
422 |
* fixing bugs. |
|
423 |
* |
|
424 |
*/ |
|
425 |
public void printSubNodes() { |
|
426 |
for (int i=0 ; i<getNumSubNodes() ; i++){ |
|
427 |
System.out.println(getSubNode(i).getName() + " = " + getSubNode(i).getText()); |
|
428 |
} |
|
429 |
} |
|
430 |
|
|
431 |
/** |
|
432 |
* This method prints a node in the standard output. Just for degug |
|
433 |
* @param node |
|
434 |
*/ |
|
435 |
public void printNode() { |
|
436 |
printNode(this); |
|
437 |
} |
|
438 |
|
|
439 |
/** |
|
440 |
* Busca un Nodo dado una ruta de nodo del tipo "nodoRaiz:nodoPrimerNivel:...:nodoNivelN": |
|
441 |
* @return Devuelve el Nodo que corresponde a la ruta correcta o 'null' si no |
|
442 |
* lo encuentra |
|
443 |
* @param etiqueta Ruta del campo que queremos buscar, separando los niveles por ':' |
|
444 |
*/ |
|
445 |
public XMLNode searchNode(String etiqueta) { |
|
446 |
return XMLTree.searchNode(this, etiqueta); |
|
447 |
} |
|
448 |
|
|
449 |
/** |
|
450 |
* Hace una busqueda de un atributo de un nodo |
|
451 |
* |
|
452 |
* @param nombreAtributo Nombre del atributo |
|
453 |
* @return Valor del atributo, o null si no lo ha encontrado |
|
454 |
* @param attributeName |
|
455 |
*/ |
|
456 |
public String searchAtribute(String attributeName) { |
|
457 |
return XMLTree.searchAtribute(this, attributeName); |
|
458 |
} |
|
459 |
|
|
460 |
/** |
|
461 |
* Hace una busqueda de una etiqueta en un nodo y devuelve |
|
462 |
* su valor |
|
463 |
* @return Valor de la etiqueta |
|
464 |
* @param etiqueta Nombre de la etiqueta |
|
465 |
*/ |
|
466 |
public String searchNodeValue(String etiqueta) { |
|
467 |
return XMLTree.searchNodeValue(this,etiqueta); |
|
468 |
} |
|
469 |
|
|
470 |
/** |
|
471 |
* Hace una busqueda de una etiqueta en un nodo y devuelve |
|
472 |
* el valor del atributo correspondiente |
|
473 |
* @return Valor del atributo de la etiqueta o null |
|
474 |
* @param etiqueta Nombre de la etiqueta |
|
475 |
* @param atributo |
|
476 |
*/ |
|
477 |
public String searchNodeAtribute(XMLNode node, String etiqueta, String atributo) { |
|
478 |
return XMLTree.searchNodeAtribute(this, etiqueta, atributo); |
|
479 |
} |
|
480 |
|
|
481 |
/** |
|
482 |
* Hace una busqueda de nodos que se llaman igual y devuleve el valor |
|
483 |
* @return Un vector con valores de las etiquetas |
|
484 |
* @param rootNode Nodo a partir del cual se quiere hacer la b?squeda |
|
485 |
* @param label Node label |
|
486 |
*/ |
|
487 |
public String[] searchMultipleNodeValue(String label) { |
|
488 |
return XMLTree.searchMultipleNodeValue(this, label); |
|
489 |
} |
|
490 |
|
|
491 |
/** |
|
492 |
* Hace una busqueda de nodos que se llaman igual desde uno dado(sin recursividad) |
|
493 |
* @return Un vector con los nodos que ha encontrado |
|
494 |
* @param nodoRaiz Nodo a partir del cual se quiere hacer la b?squeda |
|
495 |
* @param label |
|
496 |
*/ |
|
497 |
public XMLNode[] searchMultipleNode(String label) { |
|
498 |
return XMLTree.searchMultipleNode(this, label); |
|
499 |
} |
|
500 |
|
|
501 |
/** |
|
502 |
* @return The XML tree like a String |
|
503 |
*/ |
|
504 |
public String getXmlTree(){ |
|
505 |
StringBuffer buffer = new StringBuffer(); |
|
506 |
buffer.append("<" + getName()); |
|
507 |
for (int i = 0; i < getAttributeNames().size(); i++) { |
|
508 |
String name = (String)getAttributeNames().get(i); |
|
509 |
String val = (String)getAttribute(name); |
|
510 |
buffer.append(" " + name + "='" + val + "'"); |
|
511 |
} |
|
512 |
if (getNumSubNodes() == 0 && getText() == null) { |
|
513 |
buffer.append("/>\n"); |
|
514 |
} |
|
515 |
else { |
|
516 |
buffer.append(">"); |
|
517 |
if (getText() != null) { |
|
518 |
buffer.append(getText()); |
|
519 |
} |
|
520 |
if (getNumSubNodes() > 0) { |
|
521 |
buffer.append("\n"); |
|
522 |
for (int i = 0; i < getSubnodes().length; i++) { |
|
523 |
if (getSubnodes()[i] != null) { |
|
524 |
buffer.append(getSubnodes()[i].getXmlTree()); |
|
525 |
} |
|
526 |
} |
|
527 |
buffer.append("</" + getName() + ">\n"); |
|
528 |
} |
|
529 |
else { |
|
530 |
buffer.append("</" + getName() + ">\n"); |
|
531 |
} |
|
532 |
} |
|
533 |
return buffer.toString(); |
|
534 |
} |
|
535 |
|
|
536 |
/** |
|
537 |
* Print a node by the default exit |
|
538 |
* @param node |
|
539 |
*/ |
|
540 |
private void printNode(XMLNode node){ |
|
541 |
System.out.print(getXmlTree()); |
|
542 |
} |
|
543 |
} |
|
0 | 544 |
org.gvsig.catalog/tags/org.gvsig.catalog-2.0.31/org.gvsig.catalog.lib/src/main/java/org/gvsig/catalog/languages/FilterEncoding.java | ||
---|---|---|
1 |
|
|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana |
|
3 |
* |
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* 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 |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA. |
|
19 |
* |
|
20 |
* For more information, contact: |
|
21 |
* |
|
22 |
* Generalitat Valenciana |
|
23 |
* Conselleria d'Infraestructures i Transport |
|
24 |
* Av. Blasco Ib??ez, 50 |
|
25 |
* 46010 VALENCIA |
|
26 |
* SPAIN |
|
27 |
* |
|
28 |
* +34 963862235 |
|
29 |
* gvsig@gva.es |
|
30 |
* www.gvsig.gva.es |
|
31 |
* |
|
32 |
* or |
|
33 |
* |
|
34 |
* IVER T.I. S.A |
|
35 |
* Salamanca 50 |
|
36 |
* 46005 Valencia |
|
37 |
* Spain |
|
38 |
* |
|
39 |
* +34 963163400 |
|
40 |
* dac@iver.es |
|
41 |
*/ |
|
42 |
package org.gvsig.catalog.languages; |
|
43 |
import java.util.Iterator; |
|
44 |
|
|
45 |
import org.gvsig.catalog.querys.Coordinates; |
|
46 |
|
|
47 |
/** |
|
48 |
* This class implements the Filter Encoding Language. It is used to |
|
49 |
* create queries in this language |
|
50 |
* @author Jorge Piera Llodra (jorge.piera@iver.es) |
|
51 |
* @see http://portal.opengeospatial.org/files/?artifact_id=8340 |
|
52 |
*/ |
|
53 |
public class FilterEncoding extends AbstractGeneralLanguage { |
|
54 |
//Properties |
|
55 |
public static final String PROPERTY_IS_LIKE = "PropertyIsLike"; |
|
56 |
public static final String PROPERTY_IS_LESS = "PropertyIsLess"; |
|
57 |
public static final String PROPERTY_IS_GREATER = "PropertyIsGreater"; |
|
58 |
public static final String PROPERTY_IS_GREATER_THAN = "PropertyIsGreaterThan"; |
|
59 |
public static final String PROPERTY_IS_LESS_THAN = "PropertyIsLessThan"; |
|
60 |
public static final String PROPERTY_IS_EQUALS_TO = "PropertyIsEqualTo"; |
|
61 |
//Type options |
|
62 |
public static final String TYPE_LITERAL = "Literal"; |
|
63 |
public static final String TYPE_TWO_PROPERTIES = "PropertyName"; |
|
64 |
//Default values |
|
65 |
public static final String DEFAULT_PREFIX = "ogc"; |
|
66 |
public static final String DEFAULT_WILDCARD = "*"; |
|
67 |
public static final String DEFAULT_SINGLECHAR = "?"; |
|
68 |
public static final String DEFAULT_ESCAPE = "\\"; |
|
69 |
public static final String DEFAULT_NAMESPACE = "xmlns:ogc=\"http://www.opengis.net/ogc\""; |
|
70 |
//Private labels |
|
71 |
private static final String FILTER = "Filter"; |
|
72 |
|
|
73 |
private String prefix = null; |
|
74 |
private String wildCard = null; |
|
75 |
private String singleChar = null; |
|
76 |
private String escape = null; |
|
77 |
private String namespace = null; |
|
78 |
private String wildCardLabel = "wildCard"; |
|
79 |
private String escapeCharLabel = "escapeChar"; |
|
80 |
private String singleCharLabel = "singleChar"; |
|
81 |
|
|
82 |
/** |
|
83 |
* Create a new Filter Encoding Parser |
|
84 |
* @param prefix Prefix of the labels (if its necessary). |
Also available in: Unified diff