View Javadoc

1   /*
2    * $Id: VoResourceSupport.java,v 1.17 2006/10/17 10:11:41 clq2 Exp $
3    *
4    * (C) Copyright Astrogrid...
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 = "vor:Resource";
47  
48     public static final String VORESOURCE_ELEMENT_NAMESPACES = 
49       "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
50       "xmlns:vor=\"http://www.ivoa.net/xml/RegistryInterface/v0.1\" " +
51       "xmlns:vr=\"http://www.ivoa.net/xml/VOResource/v0.10\" " +
52       // This one is the default namespace
53       "xmlns=\"http://www.ivoa.net/xml/VOResource/v0.10\" ";
54  
55     public static final String VORESOURCE_ELEMENT_SCHEMALOCATIONS = 
56        "http://www.ivoa.net/xml/VOResource/v0.10 http://software.astrogrid.org/schema/vo-resource-types/VOResource/v0.10/VOResource.xsd" + " " + 
57        "http://www.ivoa.net/xml/RegistryInterface/v0.1 http://software.astrogrid.org/schema/registry/RegistryInterface/v0.1/RegistryInterface.xsd"; 
58  
59     /*** Constructs a miniumum set of core elements for a VOresource from the given
60      * strings 
61      * @TOFIX : Add property for logo and proper creator name 
62      * */
63     public String makeCore(String title, String id, String publisher, String contactName, String contactEmail, String description, String refUrl, String type) {
64     
65        String core =
66           "\n<title>"+title+"</title>\n"+
67           "<identifier>"+id+"</identifier>\n"+
68           "<curation>\n"+
69             "<publisher>"+publisher+"</publisher>\n"+
70             "<creator>\n"+
71             // NB THIS NEXT SHOULD REALLY BE DATASET CREATOR'S NAME?
72             "<name>"+contactName+"</name>\n" +
73             "<logo>" +refUrl + "/logo.gif</logo>\n" +
74             "</creator>\n"+
75             "<contact>\n"+
76               "<name>"+contactName+"</name>\n"+
77               "<email>"+contactEmail+"</email>\n"+
78             "</contact>\n"+
79           "</curation>\n"+
80           "<content>\n"+
81             "<description>"+description+"</description>\n"+
82             "<referenceURL>"+refUrl+"</referenceURL>\n"+
83             "<type>"+type+"</type>\n"+
84           "</content>\n";
85           
86        return core;
87     }
88  
89     /*** Constructs  a VoResource opening tag with the given resource type */
90     public String makeVoResourceElement(String vorType, String namespaces, String schemaLocations) {
91       String nsSp, slSp;
92       if (namespaces.equals("")) {
93         nsSp = "";
94       } else {
95         nsSp = " ";
96       }
97       if (schemaLocations.equals("")) {
98         slSp = "";
99       } else {
100        slSp = " ";
101      }
102      return "<" + VORESOURCE_ELEMENT + " " + 
103          VORESOURCE_ELEMENT_NAMESPACES + nsSp + 
104          namespaces + " " +
105          "xsi:schemaLocation=\"" +
106          VORESOURCE_ELEMENT_SCHEMALOCATIONS + slSp +
107          schemaLocations + 
108          "\"" +
109          " status='active' updated='"+toRegistryForm(new Date())+"' xsi:type='"+vorType+"'"+
110          ">";
111    }
112 
113    /*** Constructs core VOResource elements with given string appended to the
114     * identifier.  Looks at config file to see if a pre-prepared voresource core
115     * file exists and uses that (incl validation) if so, and if not uses settings
116     * in the configuration file to create it.
117     */
118    public String makeCore(String idEnd) throws IOException {
119       String coreFile = ConfigFactory.getCommonConfig().getString("dataserver.metadata.core", null);
120       if ( coreFile != null) {
121          //use an on-disk resource file
122          //schemaLocations + 
123          try {
124             Document coreDoc = DomHelper.newDocument(ConfigReader.resolveFilename(coreFile));//validate & load
125 
126             //set ID
127             NodeList idNodes = coreDoc.getElementsByTagName("identifier");
128             if (idNodes.getLength() != 1) {
129                throw new MetadataException("Should be exactly one identifier node in core resource document at "+coreFile);
130             }
131             Element idNode = (Element) idNodes.item(0);
132             DomHelper.setElementValue(idNode, DomHelper.getValueOf(idNode)+idEnd);
133             
134             return DomHelper.DocumentToString(coreDoc);
135          }
136          catch (SAXException e) {
137             throw new MetadataException(e+" parsing Core Resource/Metadata document at "+coreFile,e);
138          }
139 
140       }
141       else {
142          return makeConfigCore(idEnd);
143       }
144    }
145 
146    /*** Constructs core VOResource elements from settings in the configuration file */
147    private String makeConfigCore(String idEnd) throws IOException {
148       
149          return makeCore(
150             DataServer.getDatacenterName(),
151             makeId(idEnd),
152             ConfigFactory.getCommonConfig().getString("datacenter.publisher",null),
153             ConfigFactory.getCommonConfig().getString("datacenter.contact.name", ""),
154             ConfigFactory.getCommonConfig().getString("datacenter.contact.email", ""),
155             ConfigFactory.getCommonConfig().getString("data.description", ""),
156             ConfigFactory.getCommonConfig().getString("datacenter.url", ""),
157             "Catalog"
158          );
159    }
160    
161    /*** Constructs an IVORN ID from an authority key and a resource key and the given extension */
162    public String makeId(String idEnd) throws IOException {
163       // KEA: Need to check for rogue "/"
164       String authID = ConfigFactory.getCommonConfig().getString(AUTHID_KEY);
165       String resKey = ConfigFactory.getCommonConfig().getString(RESKEY_KEY);
166       if ( (authID == null) || ("".equals(authID)) ) {
167         throw new IOException("Expected configuration key " + AUTHID_KEY +
168               " is not set, please check your configuration.");
169       }
170       if ( (resKey == null) || ("".equals(resKey)) ) {
171         throw new IOException("Expected configuration key " + RESKEY_KEY +
172               " is not set, please check your configuration.");
173       }
174       // The checking for '/' below is probably unnecessary, but best
175       // to be sure.
176       String ivornID = "ivo://" + authID;
177       if (authID.charAt(authID.length()-1) != '/') {
178         ivornID = ivornID + "/"; // Append a '/' if not present
179       }
180       ivornID = ivornID + resKey;
181       if (resKey.charAt(resKey.length()-1) != '/') {
182         ivornID = ivornID + "/"; // Append a '/' if not present
183       }
184       ivornID = ivornID + idEnd;
185       return ivornID;
186    }
187 
188    /*** Constructs an IVORN ID for the corresponding Authority ID registration
189     * from an authority key */
190    public static String makeAuthorityId() throws IOException {
191       // KEA: Need to check for rogue "/"
192       String authID = ConfigFactory.getCommonConfig().getString(AUTHID_KEY);
193       if ( (authID == null) || ("".equals(authID)) ) {
194         throw new IOException("Expected configuration key " + AUTHID_KEY +
195               " is not set, please check your configuration.");
196       }
197       // The checking for '/' below is probably unnecessary, but best
198       // to be sure.
199       String ivornID = "ivo://" + authID;
200       if (authID.charAt(authID.length()-1) != '/') {
201         ivornID = ivornID + "/"; // Append a '/' if not present
202       }
203       ivornID = ivornID + "authority";
204       return ivornID;
205    }
206    
207    
208    /*** Converts date to string suitable for registry */
209    public String toRegistryForm(Date givenDate) {
210 /*
211  *
212       //deprecated methods
213       //long minsOffset = aDate.getTimezoneOffset();
214       //Date gmtDate = new Date( aDate.getTime() + aDate.getTimezoneOffset());
215       //this also leaves us with a date that has it's original timezone, but a new value.  It's really a different time...
216       
217       //cludge to get timezone of given date; write it out and then parse it...
218       SimpleDateFormat offsetGetter = new SimpleDateFormat("Z");
219       String offsetName = offsetGetter.format(givenDate);
220       TimeZone givenZone = TimeZone.getTimeZone(offsetName);
221       int offset = givenZone.getOffset(givenDate.getTime());
222 //      Date gmtDate = new Date(givenZone.getOffset();
223       
224       
225       //convert to GMT
226       Calendar localCalender = Calendar.getInstance();
227       TimeZone localZone = localCalender.getTimeZone();
228       Calendar ukcalender = new GregorianCalendar(Locale.ENGLISH);
229       TimeZone gmtZone = TimeZone.getTimeZone("GMT");
230  *
231  */
232       //
233       // Copied from registry code (probably won't work outside the UK).
234       /*
235       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
236       return sdf.format(
237           new Date()
238           );
239      */
240 
241       // Adjusted by KEA just to report date (not time) - see
242       // www.astrogrid.org/bugzilla ticket 1920.
243       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
244       return sdf.format(
245           new Date()
246           );
247     }
248 }
249