aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/voclient/voclientd/VOTResourceArray.java
blob: 932f4b66dca0a1a4d4d4027e57252c667659b3b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
**   VOTResourceArray.java
**
**   M. Fitzpatrick, NOAO, July 2008
**   
*/

package voclient;

import java.io.*;                       // general classes
import java.net.*;
import java.util.*;

import net.ivoa.www.xml.VOTable.v1_1.*; // for parsing the VOTable result


/**
 *
 */
public class VOTResourceArray {

    private static final boolean DEBUG	= false;
    private static final boolean VDEBUG	= false;

    private VOTResource[] votResource = null;



    public VOTResourceArray (VOTABLE vot, String svcType, String waveband,
	boolean DALOnly, String[] sterms, boolean doSort)
    {
	ArrayList <VOTResource> VRarry = new ArrayList <VOTResource> ();

	Resource[] res = vot.getRESOURCE();
	Table[] tab = res[0].getTABLE();
	TableData tdat = tab[0].getDATA().getTABLEDATA();
	Field[] fields = tab[0].getFIELD();
	int  nfields = fields.length;
	String nam =  null;
	String val =  null;

	ArrayList names = new ArrayList(); 		// Read the header
        for (int i=0;  i < nfields;  i++) {
	    names.add (fields[i].getID());
	}

	Tr[] tr = tdat.getTR();
	int nrows = tr.length;

	if (DEBUG)
	    System.err.println ("nrows="+nrows+"   nfields="+nfields);

        for (int i=0;  i < nrows;  i++) { 		// Read the table data
	    VOTResource vr = new VOTResource (i);

	    Td[] td = tr[i].getTD();
            for (int j=0;  j < nfields;  j++) {

		/* Get the name of column j and the associated value.
		*/
		nam =  (String) fields[j].getID().toString();
		val =  (String) td[j].get_value();

		if (VDEBUG)
		    System.err.println (":"+nam+":=:"+val+":");
		    

		// kludge = FIXME	////////////////////////////
		//		 				  //
		if ( nam.equals("regionOfRegard") )		  //
		    continue;					  //
		if ( nam.equals("maxRadius") )		 	  //
		    continue;		 	 	 	  //
		if ( nam.equals("maxRecords") )		 	  //
		    break;		 	 	 	  //
	        if (j==(nfields-2)&&nam.equals("referenceURL")) { //
		    vr.setByFieldName ( nam, val );		  //
	            break;		 			  //
		}		  				  //
		//		 				  //
		// kludge = FIXME	////////////////////////////

		vr.setByFieldName ( nam, val );
	    }

	    try {
		if (doSort)
	            vr.setRank (sterms);	// Set result ranking.
	    } catch (Exception ex) {
		;
	    }


	    /* Check whether the resource matches the waveband and resource
	    ** type constraints.
	    */
	    if ( waveband != null ) { 
		String[] w = vr.getWaveband();
		if (w != null) {
		    String s = arrayToStr ( w ) ;
		    if ( s != null && s.contains ( waveband ) ) {
			if (DEBUG)
			    System.err.println ("Skipping waveband :"+s+":");
	    	        continue;
		    }
		}
	    }
	    if ( svcType != null) {
		String[] c = vr.getCapabilityStandardID();
		if (c != null && c.length > 0) {
		    String s = arrayToStr (c).toLowerCase();
		    if (s != null && 
			( s.contains(svcType.toLowerCase()) ||
			  svcType.contains(s.toLowerCase()) ||
			  s.equals(svcType.toLowerCase())) ) {
	        	      ;		// added to the list below
		    }
		} else {
		    if (DEBUG)
		        System.err.println ("Skipping svc :"+svcType+":");
		    continue;
		}
	    }

	    /*  If we're asking for DAL resources, expand the resource record
	    **  so there is one entry for each capability/interface. 
	    */ 
	    if ( DALOnly ) {
		VOTResource[] vra = vr.expandResources();

		for (int k = 0; k < vra.length; k++) {
		    String std = arrayToStr(vra[k].getCapabilityStandardID());

		    if (std != null && std.contains("std")) {
	        	VRarry.add (vra[k]);		// add to the list
		    }
		}

	    } else {
	        VRarry.add (vr);			// add to the list
	    }
	}

	if (doSort)
	    Collections.sort(VRarry);			// sort by ranking

	votResource  = new VOTResource[VRarry.size()];	// convert to array
	VRarry.toArray( votResource );
    }


    public VOTResource[] getVOTResource () {
        return votResource;
    }

    public void setVOTResource (VOTResource[] votResource) {
        this.votResource = votResource;
    }

    public VOTResource getVOTResource (int i) {
        return votResource[i];
    }

    public void setVOTResource (int i, VOTResource value) {
        this.votResource[i] = value;
    }



    /** 
     *  Utility to convert a String array to a CSV of the list
     *  @param in       input String array
     *  @return         a CSV String of the array contents
     */
    private String arrayToStr (java.lang.String[] in)
    {
        String  out = "";

        if (in == null || in.length == 0)
	    return (null);
        for (int i=0; i < in.length; i++) {
            out += in[i];
            if (i < (in.length-1))
                out += ",";
        }

        return ( new String (out) );
    }

    private boolean __hashCodeCalc = false;
    public synchronized int hashCode () 
    {
        if (__hashCodeCalc) {
            return 0;
        }

        __hashCodeCalc = true;
        int _hashCode = 1;
        if (getVOTResource() != null) {
            for (int i=0;
                 i<java.lang.reflect.Array.getLength(getVOTResource());
                 i++) {
                    java.lang.Object obj = 
		        java.lang.reflect.Array.get(getVOTResource(), i);

                    if (obj != null && !obj.getClass().isArray()) {
                        _hashCode += obj.hashCode();
                    }
            }
        }
        __hashCodeCalc = false;
        return _hashCode;
    }
}