Statistics
| Revision:

root / trunk / libraries / libWMSv0 / src / com / iver / wmsclient / WMSUtilities.java @ 2954

History | View | Annotate | Download (4.66 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.wmsclient;
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 WMSUtilities {
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
    
162
    public static String checkHostString(String hostString)
163
    {
164
        if (hostString.indexOf("?") == -1)
165
        {
166
            // Viene sin el interrogante
167
            hostString += "?";
168
        }
169
        else
170
        {
171
            if (!hostString.endsWith("?")) {
172
                hostString += "&";
173
            }
174
        }
175
        return hostString;
176
    }
177
}