Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / utils / Utilities.java @ 3377

History | View | Annotate | Download (6.55 KB)

1
package org.gvsig.remoteClient.utils;
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

    
43
import java.io.IOException;
44
import java.io.InputStream;
45
import java.io.OutputStream;
46

    
47
import java.rmi.NoSuchObjectException;
48
import java.util.StringTokenizer;
49
import java.util.Vector;
50

    
51

    
52
/**
53
* Clase con m?todos de utilidad en el protocolo WMS
54
*
55
* @author Fernando Gonz?lez Cort?s
56
*/
57
public class Utilities {
58
   /**
59
    * Copia el contenido de un InputStream en un OutputStream
60
    *
61
    * @param in InputStream
62
    * @param out OutputStream
63
    */
64
   public static void serializar(InputStream in, OutputStream out) {
65
       byte[] buffer = new byte[102400];
66

    
67
       int n;
68

    
69
       try {
70
           while ((n = in.read(buffer)) != -1) {
71
               out.write(buffer, 0, n);
72
           }
73
       } catch (IOException e) {
74
           e.printStackTrace();
75
       }
76
   }
77

    
78
   /**
79
    * Elimina del xml la declaraci?n del DTD
80
    *
81
    * @param bytes bytes del fichero XML de respuesta a getCapabilities
82
    * @param startTag Tag raiz del xml respuesta a getCapabilities
83
    *
84
    * @return bytes del fichero XML sin la declaraci?n del DTD
85
    */
86
   public static byte[] eliminarDTD(byte[] bytes, String startTag) {
87
       String text = new String(bytes);
88
       int index1 = text.indexOf("?>") + 2;
89
       int index2;
90

    
91
       try {
92
           index2 = findBeginIndex(bytes, startTag);
93
       } catch (NoSuchObjectException e) {
94
           return bytes;
95
       }
96

    
97
       byte[] buffer = new byte[bytes.length - (index2 - index1)];
98
       System.arraycopy(bytes, 0, buffer, 0, index1);
99
       System.arraycopy(bytes, index2, buffer, index1, bytes.length - index2);
100

    
101
       return buffer;
102
   }
103

    
104
   /**
105
    * Obtiene el ?ndice del comienzo del xml
106
    *
107
    * @param bytes bytes del fichero XML en el que se busca
108
    * @param tagRaiz Tag raiz del xml respuesta a getCapabilities
109
    *
110
    * @return ?ndice donde empieza el tag raiz
111
    *
112
    * @throws NoSuchObjectException Si no se encuentra el tag
113
    */
114
   private static int findBeginIndex(byte[] bytes, String tagRaiz)
115
       throws NoSuchObjectException {
116
       try {
117
           int nodo = 0;
118
           int ret = -1;
119

    
120
           int i = 0;
121

    
122
           while (true) {
123
               switch (nodo) {
124
                   case 0:
125

    
126
                       if (bytes[i] == '<') {
127
                           ret = i;
128
                           nodo = 1;
129
                       }
130

    
131
                       break;
132

    
133
                   case 1:
134

    
135
                       if (bytes[i] == ' ') {
136
                       } else if (bytes[i] == tagRaiz.charAt(0)) {
137
                           nodo = 2;
138
                       } else {
139
                           nodo = 0;
140
                       }
141

    
142
                       break;
143

    
144
                   case 2:
145

    
146
                       String aux = new String(bytes, i, 18);
147

    
148
                       if (aux.equalsIgnoreCase(tagRaiz.substring(1))) {
149
                           return ret;
150
                       }
151

    
152
                       nodo = 0;
153

    
154
                       break;
155
               }
156

    
157
               i++;
158
           }
159
       } catch (Exception e) {
160
           throw new NoSuchObjectException("No se pudo parsear el xml");
161
       }
162
   }
163
   
164
   /**
165
    * Converts the contents of a Vector to a comma separated list
166
    * 
167
    * */
168
   public static String Vector2CS(Vector v)
169
   {
170
           String str = new String();
171
           int i;
172
           for (i=0; i<v.size() ;i++)
173
           {
174
                   str = str + (String)v.elementAt(i);
175
                   if (i<v.size()-1)
176
                           str = str + ",";
177
           }
178
           return str;
179
   }
180
   
181
        public static boolean isValidVersion(String version)
182
        {
183
         if(version.trim().length() == 5)
184
          {
185
                if ( (version.charAt(1)=='.') && (version.charAt(3)=='.'))
186
                {
187
                  char x = version.charAt(0);
188
                  char y = version.charAt(2);
189
                  char z = version.charAt(4);
190

    
191
                  if ((Character.isDigit(x)) && (Character.isDigit(y)) && (Character.isDigit(z)))
192
                  {
193
                        return true;
194
                  }
195
                  else
196
                  {
197
                        return false;
198
                  }
199
                }
200
                else
201
                {
202
                  return false;
203
                }
204
          }
205
          else
206
          {
207
                return false;
208
          }
209
        }
210
   
211
   /**
212
    * Checks if a String is a number or not
213
    * 
214
    * @param String, s
215
    * @return boolean, true if s is a number
216
    */
217
   public static boolean isNumber(String s) 
218
    {
219
      int pointCount = 0;
220
      int commaCount = 0;
221
      
222
      char[] c = s.toCharArray();
223
      for (int i = c.length; --i >= 0; ) 
224
      {
225
        char cc = c[i];
226

    
227
        if (Character.isDigit(cc))
228
        {
229
          continue;
230
        }
231
        else
232
        {
233
          /* 'cc' is not a digit. */
234

    
235
          if (cc == '.')
236
          {
237
            pointCount++;
238
            if (pointCount > 1)
239
            {
240
              return false;
241
            }
242
          }
243
          else if (cc == ',')
244
          {
245
            commaCount++;
246
            if (commaCount > 1)
247
            {
248
              return false;
249
            }
250
          }
251
          else if (cc != '-')
252
          {
253
            return false;
254
          }
255
        }
256
      }
257
      return true;
258
    }   
259
        /**
260
         * Parses the String containing different items [character] separated and
261
         * creates a vector with them.
262
         * @param str String contains item1[c]item2[c]item3...
263
         * @param c is the string value for separating the items
264
         * @return Vector containing all the items
265
         */
266
        public static Vector createVector(String str, String c)
267
        {
268
                StringTokenizer tokens = new StringTokenizer(str, c);
269
                Vector v = new Vector();
270
                try
271
                {
272
                        while (tokens.hasMoreTokens())
273
                        {
274
                                v.addElement(tokens.nextToken());
275
                        }
276
                        return v;
277
                }
278
                catch (Exception e)
279
                {
280
                        return new Vector();
281
                }
282
        }
283
   
284
}