1
2
3
4
5
6
7
8
9
10 package org.astrogrid.tools.votable;
11
12 import java.io.*;
13 import org.astrogrid.tools.xml.*;
14 import java.util.Vector;
15
16 /***
17 * Xml Tag for writing tables using the VOTable XML schema - see the NVO
18 * website and its description of VOTable for details, but it goes something
19 * like this:
20 * <pre>
21 * headers
22 * VOTABLE
23 * DESCRIPTION
24 * DEFINITIONS xN
25 * RESOURCE
26 * PARAM xN <-- the criteria used for the search
27 * TABLE
28 * DESCRIPTION
29 * FIELD xN
30 * DATA
31 * TABLEDATA
32 * TR xN
33 * TD xN
34 * </pre>
35 * @see http://us-vo.org/metadata/conesearch/ for summary of format required
36 * and
37 * @see http://vizier.u-strasbg.fr/doc/VOTable/votable-1-0.htx for general
38 * VOTable format
39 *
40 * VotRootTag is the root votable tag, allowing only the following tags as
41 * children:
42 * - Descriptions
43 * - Definitions
44 * - Resources
45 *
46 * @version %I%
47 * @author M Hill
48 */
49
50 public class VotTag extends AbstractVotTag
51 {
52 public final static String VOT_ROOT = "VOTABLE";
53
54 public VotTag(XmlOutput xmlOut) throws IOException
55 {
56 super(xmlOut.newTag(new XmlTag(VOT_ROOT,"version=\"1.0\"",xmlOut)));
57 }
58
59 public void writeDescription(String description) throws IOException
60 {
61 xmlOut.writeTag("DESCRIPTION",description);
62 }
63
64 public DefinitionTag newDefinitionTag() throws IOException
65 {
66 return (DefinitionTag) newTag(new DefinitionTag(this));
67 }
68
69 public ResourceTag newResourceTag(String attrs) throws IOException
70 {
71 return (ResourceTag) newTag(new ResourceTag(attrs, this));
72 }
73
74
75
76
77 boolean validateValues = false;
78
79 Vector fields = new Vector();
80
81 public final static int NOT_ARRAY = 0;
82 public final static int VARIABLE_ARRAY = -1;
83
84 /*** Called whenever a field tag (ie, table column header) is added. This
85 * can then be used by the writeValue() of the table block to validate the
86 * value being written. Not implemented yet
87 */
88 public void addFieldDescriptor(Field field)
89 {
90 fields.add(field);
91 }
92
93 /*** Returns the number of fields specified in the table so far
94 */
95 public int getNumFields()
96 {
97 return fields.size();
98 }
99
100 /***Set the validation on/off
101 */
102 public void setValidateValues(boolean v)
103 {
104 validateValues = v;
105 }
106
107
108 /*** Inner class. Special 'Definition' tag - allows co-ord sys to be given */
109 public class DefinitionTag extends AbstractVotTag
110 {
111 public DefinitionTag(VotTag parent) throws IOException
112 {
113 super(new XmlTag("DEFINITIONS","",parent.xmlOut));
114 }
115
116 public void writeCooSys(String id, String system, String equinox, String epoch) throws IOException
117 {
118 xmlOut.writeLine("<COOSYS ID=\""+id+"\" system=\""+system+"\" equinox=\""+equinox+"\" epoch=\""+epoch+"\"/>");
119 }
120 }
121
122 /*** Resource tag block - allows parameters and data table to be given */
123 public class ResourceTag extends AbstractVotTag
124 {
125 public ResourceTag(String attrs, VotTag parent) throws IOException
126 {
127 super(new XmlTag("RESOURCE",attrs,parent.xmlOut));
128 }
129
130 public void writeDescription(String description) throws IOException
131 {
132 xmlOut.writeTag("DESCRIPTION",description);
133 }
134
135 public void writeParam(String id, String datatype, String value) throws IOException
136 {
137 xmlOut.writeLine("<PARAM ID=\""+id+"\" datatype=\""+datatype+"\" value=\""+value+"\"/>");
138 }
139 public void writeParam(String id, String datatype, String arraysize, String value) throws IOException
140 {
141 xmlOut.writeLine("<PARAM ID=\""+id+"\" datatype=\""+datatype+"\" arraysize=\""+arraysize+"\" value=\""+value+"\"/>");
142 }
143 public TableTag newTableTag() throws IOException
144 {
145 return (TableTag) newTag(new TableTag(this));
146 }
147 }
148
149 /*** Table tag - allows fields (headers) and data (cell values) to be written*/
150 public class TableTag extends AbstractVotTag
151 {
152 public TableTag(ResourceTag parent) throws IOException
153 {
154 super(new XmlTag("TABLE","",parent.xmlOut));
155 }
156
157 public void writeDescription(String description) throws IOException
158 {
159 xmlOut.writeTag("DESCRIPTION",description);
160 }
161
162 /*** Writes out the column header information */
163 public void writeField(Field fieldDesc) throws IOException
164 {
165 String line = "<FIELD ID=\""+fieldDesc.getId()+ "\""
166 + " name=\""+fieldDesc.getName()+"\""
167 + " datatype=\""+fieldDesc.getDatatype()+"\"";
168
169 if (fieldDesc.getUnits() != null)
170 {
171 line = line + " unit=\""+fieldDesc.getUnits()+"\"";
172 }
173 if (fieldDesc.getUcd() != null)
174 {
175 line = line + " ucd=\""+fieldDesc.getUcd()+"\"";
176 }
177
178 if (fieldDesc.getArraysize() != NOT_ARRAY)
179 if (fieldDesc.getArraysize() == VARIABLE_ARRAY)
180 line = line + " arraysize=\"*\"";
181 else
182 line = line + " arraysize=\""+fieldDesc.getArraysize()+"\"";
183
184 xmlOut.writeLine(line + ">");
185
186 xmlOut.writeIndentedLine(fieldDesc.getDescription());
187
188 xmlOut.writeLine("</FIELD>");
189
190 addFieldDescriptor(fieldDesc);
191 }
192
193 /*** Makes the table data/value tag */
194 public TableDataTag newTableDataTag() throws IOException
195 {
196 return (TableDataTag) newTag(new TableDataTag( this));
197 }
198 }
199
200 /*** Table data tag - really a double-layer tag <DATA> then <TABLEDATA> */
201 public class TableDataTag extends AbstractVotTag
202 {
203 private XmlTag rowTag = null;
204 private int col = 0;
205 private XmlTag tableDataTag = null;
206
207 public TableDataTag(TableTag parent) throws IOException
208 {
209 super(new XmlTag("DATA","",parent.xmlOut));
210 tableDataTag = xmlOut.newTag(new XmlTag("TABLEDATA","", this.xmlOut));
211 }
212
213 public void startNewRow() throws IOException
214 {
215 col = 0;
216 rowTag = tableDataTag.newTag("TR","");
217 }
218
219 public void writeValue(String value) throws IOException
220 {
221 if (validateValues)
222 {
223 ((Field) fields.elementAt(col)).assertValueValid(value.trim());
224 }
225
226 rowTag.writeTag("TD",value);
227 col++;
228 }
229 }
230
231
232 /*** Override close to check that valid fields have been created. Certain
233 * UCDs must have been specified once only (@see
234 * http://us-vo.org/metadata/conesearch/)
235 * - nope - specific to NVO...
236 public void close() throws IOException
237 {
238 super.close();
239
240 for
241 }
242 */
243
244
245
246 }
247