svn-gvsig-desktop / tags / v1_1_Build_1009 / libraries / libCq CMS for java.old / src / org / cresques / io / CXMLParser.java @ 12649
History | View | Annotate | Download (5.22 KB)
1 |
/*
|
---|---|
2 |
* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
3 |
*
|
4 |
* Copyright (C) 2004-5.
|
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 |
* cresques@gmail.com
|
23 |
*/
|
24 |
package org.cresques.io; |
25 |
|
26 |
import java.util.regex.Matcher; |
27 |
import java.util.regex.Pattern; |
28 |
|
29 |
|
30 |
/**
|
31 |
* Parser b?sico XML para las ventanas de propiedades de los drivers de escritura.
|
32 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
33 |
*/
|
34 |
public class CXMLParser { |
35 |
private String XML; |
36 |
|
37 |
/**
|
38 |
* Constructor
|
39 |
* @param fXML Texto en formato XML
|
40 |
*/
|
41 |
public CXMLParser(String fXML) { |
42 |
XML = fXML; |
43 |
} |
44 |
|
45 |
/**
|
46 |
* Obtiene texto XML
|
47 |
* @return texto XML
|
48 |
*/
|
49 |
public String getXML() { |
50 |
return this.XML; |
51 |
} |
52 |
|
53 |
/**
|
54 |
* Obtiene el valor del tag
|
55 |
* @param tag Tag del cual se quiere obtener el valor
|
56 |
* @param bDeleteValue Si es true elimina el tag y si es false lo mantiene
|
57 |
* @return Devuelve el contenido del tag
|
58 |
*/
|
59 |
public String getValue(String tag, boolean deleteValue) { |
60 |
XML = XML.trim(); |
61 |
|
62 |
String startTag = new String("<" + tag); |
63 |
String endTag = new String("</" + tag + ">"); |
64 |
|
65 |
int iStart = XML.indexOf(startTag);
|
66 |
int endOpenTag = XML.indexOf(">"); |
67 |
|
68 |
while ((endOpenTag < iStart) && (endOpenTag != -1)) |
69 |
endOpenTag = XML.indexOf(">", endOpenTag + 1); |
70 |
|
71 |
iStart = endOpenTag + 1;
|
72 |
|
73 |
if (iStart == 0) { |
74 |
return ""; |
75 |
} |
76 |
|
77 |
int iEnd = XML.indexOf(endTag);
|
78 |
|
79 |
if (iEnd == 0) { |
80 |
return ""; |
81 |
} |
82 |
|
83 |
String res;
|
84 |
|
85 |
try {
|
86 |
res = XML.substring(iStart, iEnd); |
87 |
|
88 |
if (deleteValue) {
|
89 |
int start = XML.indexOf(startTag);
|
90 |
int end = XML.indexOf(">"); |
91 |
|
92 |
while ((end < start) && (end != -1)) |
93 |
end = XML.indexOf(">", end + 1); |
94 |
|
95 |
String subcadena = XML.substring(start, end + 1); |
96 |
Pattern p = Pattern.compile(subcadena); |
97 |
Matcher m = p.matcher(XML);
|
98 |
XML = m.replaceFirst("");
|
99 |
p = Pattern.compile(endTag);
|
100 |
m = p.matcher(XML); |
101 |
XML = m.replaceFirst("");
|
102 |
XML = XML.trim(); |
103 |
|
104 |
if (!XML.startsWith("<")) { |
105 |
if (XML.indexOf("<") == -1) { |
106 |
XML = new String(""); |
107 |
} else {
|
108 |
XML = XML.substring(XML.indexOf("<"), XML.length());
|
109 |
} |
110 |
} |
111 |
} |
112 |
} catch (StringIndexOutOfBoundsException e) { |
113 |
return ""; |
114 |
} |
115 |
|
116 |
return res;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Obtiene un atributo de la primera aparici?n de un tag concreto
|
121 |
* @param tag Tag del cual quiere obtenerse un atributo
|
122 |
* @param att Atributo del cual se desea el valor
|
123 |
* @return
|
124 |
*/
|
125 |
public String getAttr(String tag, String att) { |
126 |
XML = XML.trim(); |
127 |
|
128 |
int start = XML.indexOf(tag) + tag.length();
|
129 |
int end = XML.indexOf(">"); |
130 |
|
131 |
while ((end < start) && (end != -1)) |
132 |
end = XML.indexOf(">", end + 1); |
133 |
|
134 |
String subcadena = XML.substring(start, end);
|
135 |
start = subcadena.indexOf(att) + att.length() + 1;
|
136 |
|
137 |
if (subcadena.indexOf(att) != -1) { |
138 |
end = subcadena.indexOf("\"", start + 1); |
139 |
|
140 |
//System.out.println(start+" "+end);
|
141 |
if (end == -1) { |
142 |
end = subcadena.length(); |
143 |
} |
144 |
|
145 |
return subcadena.substring(start + 1, end); |
146 |
} else {
|
147 |
return new String(""); |
148 |
} |
149 |
} |
150 |
|
151 |
/**
|
152 |
* Obtiene el nombre del siguiente tag XML
|
153 |
* @return Nombre del siguiente tag
|
154 |
*/
|
155 |
public String getTag() { |
156 |
XML = XML.trim(); |
157 |
|
158 |
int start = XML.indexOf("<"); |
159 |
|
160 |
if (start == -1) { |
161 |
return new String(""); |
162 |
} |
163 |
|
164 |
int end = XML.indexOf(" "); |
165 |
|
166 |
if ((end > XML.indexOf(">")) || (end == -1)) { |
167 |
end = XML.indexOf(">");
|
168 |
} |
169 |
|
170 |
if (end == -1) { |
171 |
return new String(""); |
172 |
} |
173 |
|
174 |
return XML.substring(start + 1, end); |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Dado un nombre de un tag devuelve el n?mero de estos en el XML
|
179 |
* @param tag Nombre del tag a contar
|
180 |
* @return N?mero de tags coincidentes
|
181 |
*/
|
182 |
public int getNumTags(String tag) { |
183 |
int numTags = 0; |
184 |
int index = XML.indexOf("<" + tag); |
185 |
|
186 |
while (index != -1) { |
187 |
numTags++; |
188 |
index = XML.indexOf("<" + tag, index + 1); |
189 |
} |
190 |
|
191 |
return numTags;
|
192 |
} |
193 |
} |