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