1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.applications.manager;
12
13 import org.astrogrid.applications.CeaException;
14 import org.astrogrid.applications.component.ProvidesVODescription;
15 import org.astrogrid.common.bean.v1.Namespaces;
16 import org.astrogrid.component.descriptor.ComponentDescriptor;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.exolab.castor.xml.Marshaller;
21
22 import java.io.InputStream;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25
26 import javax.xml.transform.Result;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Templates;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.stream.StreamResult;
33 import javax.xml.transform.stream.StreamSource;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37
38 /*** Standard implementation of the {@link org.astrogrid.applications.manager.MetadataService} component
39 * @author Noel Winstanley nw@jb.man.ac.uk 21-May-2004
40 * @TODO need to rationalize with the {@link org.astrogrid.applications.description.registry} classes which is the definitive source....
41 */
42 public class DefaultMetadataService implements MetadataService, ComponentDescriptor {
43 private static final Log logger = LogFactory.getLog(DefaultMetadataService.class);
44
45 private static final String FORMATTER_XSL = "registryFormatter.xsl";
46
47 /*** Construct a new StandardCEAMetaData
48 * @param provider a component that provides a VODescription, on which this Metadata component works.
49 *
50 */
51 public DefaultMetadataService( ProvidesVODescription provider) {
52 this.provider = provider;
53 }
54
55 protected final ProvidesVODescription provider;
56
57
58
59 public String returnRegistryEntry() throws CeaException {
60
61 StringWriter sw = null, swt = null;
62 InputStream formatterXSL = null;
63
64 try {
65 sw = new StringWriter();
66 Marshaller mar = new Marshaller(sw);
67 mar.setMarshalExtendedType(true);
68 mar.setSuppressXSIType(false);
69 mar.setMarshalAsDocument(true);
70
71 mar.setNamespaceMapping("cea", Namespaces.VOCEA);
72 mar.setNamespaceMapping("ceapd", Namespaces.CEAPD);
73 mar.setNamespaceMapping("ceab", Namespaces.CEAB);
74 mar.setNamespaceMapping("vr", Namespaces.VORESOURCE);
75 mar.marshal(provider.getVODescription());
76 sw.close();
77
78
79
80 TransformerFactory fac = TransformerFactory.newInstance();
81 String xslpath = this.getClass().getPackage()+FORMATTER_XSL;
82 formatterXSL = this.getClass().getResourceAsStream(FORMATTER_XSL);
83 Source formatter = new StreamSource(formatterXSL);
84 Templates template = fac.newTemplates(formatter);
85
86 Transformer xformer = template.newTransformer();
87 Source source = new StreamSource(new StringReader(sw.toString()));
88 swt = new StringWriter();
89 Result result = new StreamResult(swt);
90 xformer.transform(source, result);
91
92 return swt.toString();
93
94 }
95 catch (Exception e) {
96 logger.debug(e);
97 throw new CeaException("problem returning registry entry", e);
98 }
99 }
100
101 /***
102 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
103 */
104 public String getName() {
105 return "Standard CEA Server Description";
106 }
107 /***
108 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
109 */
110 public String getDescription() {
111 return "Standard implementation of the service description component";
112 }
113 /***
114 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
115 */
116 public Test getInstallationTest() {
117 return new InstallationTest("testGetRegistryEntry");
118 }
119
120 public class InstallationTest extends TestCase {
121
122 public InstallationTest(String arg0) {
123 super(arg0);
124 }
125 public void testGetRegistryEntry() throws Exception {
126 String entry = returnRegistryEntry();
127 assertNotNull(entry);
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