1
2
3
4
5
6
7 package org.astrogrid.dataservice.metadata.v0_10;
8
9 import java.io.IOException;
10 import java.text.SimpleDateFormat;
11 import java.util.Calendar;
12 import java.util.Date;
13 import java.util.GregorianCalendar;
14 import java.util.Locale;
15 import java.util.TimeZone;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.astrogrid.cfg.ConfigReader;
19 import org.astrogrid.cfg.ConfigFactory;
20 import org.astrogrid.dataservice.metadata.MetadataException;
21 import org.astrogrid.dataservice.service.DataServer;
22 import org.astrogrid.xml.DomHelper;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NodeList;
26 import org.xml.sax.SAXException;
27
28 /***
29 * Helper methods for constructing basic VoResource documents
30 *
31 * <p>See package documentation.
32 * <p>
33 * @author M Hill
34 */
35
36 public class VoResourceSupport {
37 protected static Log log = LogFactory.getLog(VoResourceSupport.class);
38
39 public static final String AUTHID_KEY = "datacenter.authorityId";
40 public static final String RESKEY_KEY = "datacenter.resourceKey";
41
42 /*** used to format dates so that the registry can process them. eg 2005-11-04T15:34:22Z -
43 * the date must be GMT */
44 public final static SimpleDateFormat REGISTRY_DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
45
46 public static final String VORESOURCE_ELEMENT = "Resource";
47
48 /*** Constructs a miniumum set of core elements for a VOresource from the given
49 * strings */
50 public String makeCore(String title, String id, String publisher, String contactName, String contactEmail, String description, String refUrl, String type) {
51
52 String core =
53 "<title>"+title+"</title>"+
54 "<identifier>"+id+"</identifier>"+
55 "<curation>"+
56 "<publisher>"+publisher+"</publisher>"+
57 "<contact>"+
58 "<name>"+contactName+"</name>"+
59 "<email>"+contactEmail+"</email>"+
60 "</contact>"+
61 "</curation>"+
62 "<content>"+
63 "<description>"+description+"</description>"+
64 "<referenceURL>"+refUrl+"</referenceURL>"+
65 "<type>"+type+"</type>"+
66 "</content>";
67
68 return core;
69 }
70
71 /*** Constructs a VoResource opening tag with the given resource type */
72 public String makeVoResourceElement(String vorType, String namespaces) {
73 return "<"+VORESOURCE_ELEMENT+
74 " xmlns:vor='http://www.ivoa.net/xml/VOResource/v0.10' "+
75 " xmlns='http://www.ivoa.net/xml/VOResource/v0.10' "+
76 namespaces+
77 " status='active' updated='"+toRegistryForm(new Date())+"' xsi:type='"+vorType+"'"+
78 ">";
79 }
80
81 /*** Constructs core VOResource elements with given string appended to the
82 * identifier. Looks at config file to see if a pre-prepared voresource core
83 * file exists and uses that (incl validation) if so, and if not uses settings
84 * in the configuration file to create it.
85 */
86 public String makeCore(String idEnd) throws IOException {
87 String coreFile = ConfigFactory.getCommonConfig().getString("dataserver.metadata.core", null);
88 if ( coreFile != null) {
89
90 try {
91 Document coreDoc = DomHelper.newDocument(ConfigReader.resolveFilename(coreFile));
92
93
94 NodeList idNodes = coreDoc.getElementsByTagName("identifier");
95 if (idNodes.getLength() != 1) {
96 throw new MetadataException("Should be exactly one identifier node in core resource document at "+coreFile);
97 }
98 Element idNode = (Element) idNodes.item(0);
99 DomHelper.setElementValue(idNode, DomHelper.getValueOf(idNode)+idEnd);
100
101 return DomHelper.DocumentToString(coreDoc);
102 }
103 catch (SAXException e) {
104 throw new MetadataException(e+" parsing Core Resource/Metadata document at "+coreFile,e);
105 }
106
107 }
108 else {
109 return makeConfigCore(idEnd);
110 }
111 }
112
113 /*** Constructs core VOResource elements from settings in the configuration file */
114 private String makeConfigCore(String idEnd) throws IOException {
115
116 return makeCore(
117 DataServer.getDatacenterName(),
118 makeId(idEnd),
119 ConfigFactory.getCommonConfig().getString("datacenter.publisher",null),
120 ConfigFactory.getCommonConfig().getString("datacenter.contact.name", ""),
121 ConfigFactory.getCommonConfig().getString("datacenter.contact.email", ""),
122
123 ConfigFactory.getCommonConfig().getString("data.description", ""),
124 ConfigFactory.getCommonConfig().getString("datacenter.url", ""),
125 "Catalog"
126 );
127 }
128
129 /*** Constructs an IVORN ID from an authority key and a resource key and the given extension */
130 public String makeId(String idEnd) {
131 return "ivo://"+ConfigFactory.getCommonConfig().getString(AUTHID_KEY)+"/"+ConfigFactory.getCommonConfig().getString(RESKEY_KEY)+"/"+idEnd;
132 }
133
134
135 /*** Converts date to string suitable for registry */
136 public String toRegistryForm(Date givenDate) {
137
138
139
140
141
142
143 SimpleDateFormat offsetGetter = new SimpleDateFormat("Z");
144 String offsetName = offsetGetter.format(givenDate);
145 TimeZone givenZone = TimeZone.getTimeZone(offsetName);
146 int offset = givenZone.getOffset(givenDate.getTime());
147
148
149
150
151 Calendar localCalender = Calendar.getInstance();
152 TimeZone localZone = localCalender.getTimeZone();
153 Calendar ukcalender = new GregorianCalendar(Locale.ENGLISH);
154 TimeZone gmtZone = TimeZone.getTimeZone("GMT");
155
156 return "";
157 }
158
159 }
160
161
162
163
164
165
166
167