Statistics
| Revision:

root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClientOld / utils / Utilities.java @ 3321

History | View | Annotate | Download (4.18 KB)

1
package org.gvsig.remoteClientOld.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

    
49

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

    
65
       int n;
66

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

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

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

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

    
99
       return buffer;
100
   }
101

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

    
118
           int i = 0;
119

    
120
           while (true) {
121
               switch (nodo) {
122
                   case 0:
123

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

    
129
                       break;
130

    
131
                   case 1:
132

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

    
140
                       break;
141

    
142
                   case 2:
143

    
144
                       String aux = new String(bytes, i, 18);
145

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

    
150
                       nodo = 0;
151

    
152
                       break;
153
               }
154

    
155
               i++;
156
           }
157
       } catch (Exception e) {
158
           throw new NoSuchObjectException("No se pudo parsear el xml");
159
       }
160
   }
161
}