root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / CXMLParser.java @ 2249
History | View | Annotate | Download (4.72 KB)
1 |
/*
|
---|---|
2 |
* Created on 21-jun-2005
|
3 |
*
|
4 |
* To change the template for this generated file go to
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package org.cresques.io; |
48 |
|
49 |
import java.util.regex.Matcher; |
50 |
import java.util.regex.Pattern; |
51 |
|
52 |
/**
|
53 |
*
|
54 |
* @author Nacho Brodin <brodin_ign@gva.es>
|
55 |
*
|
56 |
* Parser b?sico XML para las ventanas de propiedades de los drivers de escritura.
|
57 |
*/
|
58 |
public class CXMLParser { |
59 |
private String XML; |
60 |
|
61 |
/**
|
62 |
* Constructor
|
63 |
* @param fXML Texto en formato XML
|
64 |
*/
|
65 |
public CXMLParser(String fXML){ |
66 |
XML = fXML; |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Obtiene texto XML
|
71 |
* @return texto XML
|
72 |
*/
|
73 |
public String getXML() { |
74 |
return this.XML; |
75 |
} |
76 |
|
77 |
/**
|
78 |
* Obtiene el valor del tag
|
79 |
* @param tag Tag del cual se quiere obtener el valor
|
80 |
* @param bDeleteValue Si es true elimina el tag y si es false lo mantiene
|
81 |
* @return Devuelve el contenido del tag
|
82 |
*/
|
83 |
public String getValue( String tag, boolean deleteValue) { |
84 |
|
85 |
XML = XML.trim(); |
86 |
String startTag = new String("<" + tag); |
87 |
String endTag = new String("</" + tag + ">"); |
88 |
|
89 |
int iStart = XML.indexOf(startTag);
|
90 |
int endOpenTag = XML.indexOf(">"); |
91 |
while (endOpenTag<iStart && endOpenTag!=-1) |
92 |
endOpenTag = XML.indexOf(">",endOpenTag+1); |
93 |
iStart = endOpenTag+1;
|
94 |
|
95 |
if (iStart==0) return ""; |
96 |
|
97 |
int iEnd = XML.indexOf(endTag);
|
98 |
if (iEnd==0) return ""; |
99 |
|
100 |
String res;
|
101 |
try {
|
102 |
res = XML.substring(iStart, iEnd); |
103 |
if (deleteValue){
|
104 |
int start = XML.indexOf(startTag);
|
105 |
int end = XML.indexOf(">"); |
106 |
while (end<start && end!=-1) |
107 |
end = XML.indexOf(">",end+1); |
108 |
String subcadena = XML.substring(start,end+1); |
109 |
Pattern p = Pattern.compile(subcadena); |
110 |
Matcher m = p.matcher(XML);
|
111 |
XML = m.replaceFirst("");
|
112 |
p = Pattern.compile(endTag);
|
113 |
m = p.matcher(XML); |
114 |
XML = m.replaceFirst("");
|
115 |
XML = XML.trim(); |
116 |
if(!XML.startsWith("<")){ |
117 |
if(XML.indexOf("<")==-1) |
118 |
XML = new String(""); |
119 |
else
|
120 |
XML = XML.substring(XML.indexOf("<"),XML.length());
|
121 |
} |
122 |
} |
123 |
} catch (StringIndexOutOfBoundsException e) { |
124 |
return ""; |
125 |
} |
126 |
return res;
|
127 |
} |
128 |
|
129 |
/**
|
130 |
* Obtiene un atributo de la primera aparici?n de un tag concreto
|
131 |
* @param tag Tag del cual quiere obtenerse un atributo
|
132 |
* @param att Atributo del cual se desea el valor
|
133 |
* @return
|
134 |
*/
|
135 |
public String getAttr(String tag, String att){ |
136 |
|
137 |
XML = XML.trim(); |
138 |
int start = XML.indexOf(tag) + tag.length();
|
139 |
int end = XML.indexOf(">"); |
140 |
while (end<start && end!=-1) |
141 |
end = XML.indexOf(">",end+1); |
142 |
String subcadena = XML.substring(start,end);
|
143 |
start = subcadena.indexOf(att)+att.length()+1;
|
144 |
|
145 |
if(subcadena.indexOf(att)!=-1){ |
146 |
end = subcadena.indexOf("\"",start+1); |
147 |
//System.out.println(start+" "+end);
|
148 |
if(end==-1) |
149 |
end = subcadena.length(); |
150 |
return subcadena.substring(start+1, end); |
151 |
}else
|
152 |
return new String(""); |
153 |
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* Obtiene el nombre del siguiente tag XML
|
158 |
* @return Nombre del siguiente tag
|
159 |
*/
|
160 |
public String getTag(){ |
161 |
|
162 |
XML = XML.trim(); |
163 |
int start = XML.indexOf("<"); |
164 |
if(start==-1) |
165 |
return new String(""); |
166 |
int end = XML.indexOf(" "); |
167 |
if(end>XML.indexOf(">") || end==-1) |
168 |
end = XML.indexOf(">");
|
169 |
if(end==-1) |
170 |
return new String(""); |
171 |
return XML.substring(start+1,end); |
172 |
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Dado un nombre de un tag devuelve el n?mero de estos en el XML
|
177 |
* @param tag Nombre del tag a contar
|
178 |
* @return N?mero de tags coincidentes
|
179 |
*/
|
180 |
public int getNumTags(String tag){ |
181 |
|
182 |
int numTags = 0; |
183 |
int index = XML.indexOf("<"+tag); |
184 |
while(index!=-1){ |
185 |
numTags++; |
186 |
index = XML.indexOf("<"+tag,index+1); |
187 |
} |
188 |
return numTags;
|
189 |
|
190 |
} |
191 |
|
192 |
} |