0
|
1 package de.mpiwg.itgroup.metadataManager.pid;
|
|
2
|
|
3 import java.io.ByteArrayInputStream;
|
|
4 import java.io.IOException;
|
|
5 import java.io.InputStream;
|
|
6 import java.io.StringReader;
|
|
7 import java.io.UnsupportedEncodingException;
|
|
8 import java.net.MalformedURLException;
|
|
9 import java.net.URL;
|
|
10
|
|
11 import javax.xml.parsers.DocumentBuilder;
|
|
12 import javax.xml.parsers.DocumentBuilderFactory;
|
|
13
|
|
14 import org.apache.log4j.Logger;
|
|
15 import org.apache.xmlrpc.XmlRpcException;
|
|
16 import org.apache.xmlrpc.client.XmlRpcClient;
|
|
17 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
|
|
18 import org.jdom.Document;
|
|
19 import org.jdom.Element;
|
|
20 import org.jdom.JDOMException;
|
|
21 import org.jdom.input.SAXBuilder;
|
|
22 import org.jdom.xpath.XPath;
|
|
23
|
|
24 import org.xml.sax.InputSource;
|
|
25
|
|
26
|
|
27
|
|
28 public class DCTransformer {
|
|
29 static public String MPIWG = "http://www.mpiwg-berlin.mpg.de/ns/mpiwg";
|
|
30
|
|
31 public static String converterProvider = "http://127.0.0.1:28080/metadataMain";
|
|
32 private String metaDataLink;
|
|
33 private Logger logger = Logger.getLogger("transformerLogger");
|
|
34
|
|
35 public DCTransformer(String metaDataLink) {
|
|
36
|
|
37 this.metaDataLink=metaDataLink;
|
|
38 }
|
|
39
|
|
40 public Element getDCFromIndexMeta() {
|
|
41 //Verbinde mit metadataprovider
|
|
42 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
|
|
43
|
|
44
|
|
45 try {
|
|
46 config.setServerURL(new URL(converterProvider));
|
|
47 } catch (MalformedURLException e) {
|
|
48 // TODO Auto-generated catch block
|
|
49 e.printStackTrace();
|
|
50 return null;
|
|
51 }
|
|
52
|
|
53 XmlRpcClient client = new XmlRpcClient();
|
|
54 client.setConfig(config);
|
|
55 Object[] params = new Object[] { metaDataLink };
|
|
56
|
|
57 String result;
|
|
58 try {
|
|
59 result = (String) client.execute("getDCFormatted", params);
|
|
60 } catch (XmlRpcException e) {
|
|
61 // TODO Auto-generated catch block
|
|
62 logger.error("CANNOT connector to:"+converterProvider);
|
|
63 e.printStackTrace();
|
|
64 return null;
|
|
65 }
|
|
66 logger.debug("dC:"+result);
|
|
67
|
|
68 SAXBuilder db = new SAXBuilder();
|
|
69 InputStream resultStream;
|
|
70 try {
|
|
71 resultStream = new ByteArrayInputStream(result.getBytes("utf-8"));
|
|
72 } catch (UnsupportedEncodingException e) {
|
|
73 // TODO Auto-generated catch block
|
|
74 e.printStackTrace();
|
|
75 return null;
|
|
76 }
|
|
77 Document dc;
|
|
78 try {
|
|
79 dc = db.build(resultStream);
|
|
80 } catch (JDOMException e) {
|
|
81 // TODO Auto-generated catch block
|
|
82 e.printStackTrace();
|
|
83 return null;
|
|
84 } catch (IOException e) {
|
|
85 // TODO Auto-generated catch block
|
|
86 e.printStackTrace();
|
|
87 return null;
|
|
88 }
|
|
89
|
|
90 return dc.getRootElement();
|
|
91 }
|
|
92
|
|
93 public Element getContentOfMetaTag() {
|
|
94 SAXBuilder db = new SAXBuilder();
|
|
95
|
|
96 Document dc;
|
|
97 try {
|
|
98 dc = db.build(metaDataLink);
|
|
99 } catch (JDOMException e) {
|
|
100 // TODO Auto-generated catch block
|
|
101 e.printStackTrace();
|
|
102 return null;
|
|
103 } catch (IOException e) {
|
|
104 // TODO Auto-generated catch block
|
|
105 e.printStackTrace();
|
|
106 return null;
|
|
107 }
|
|
108
|
|
109 Element retElement = null;
|
|
110 try {
|
|
111 XPath xp = getXPath("//meta");
|
|
112 retElement = (Element) xp.selectSingleNode(dc);
|
|
113
|
|
114 } catch (JDOMException e) {
|
|
115 //nothing to be done
|
|
116 }
|
|
117 if (retElement==null) { // versuche noch mal mit namespace
|
|
118 try {
|
|
119
|
|
120 XPath xp = getXPath("//mpiwg:meta");
|
|
121 retElement = (Element) xp.selectSingleNode(dc);
|
|
122
|
|
123 } catch (JDOMException e) {
|
|
124 //nothing to be done
|
|
125 }
|
|
126
|
|
127 }
|
|
128 return retElement;
|
|
129 }
|
|
130
|
|
131
|
|
132 private XPath getXPath(String xpath) throws JDOMException{
|
|
133 XPath xpathResources = XPath.newInstance(xpath);
|
|
134
|
|
135 xpathResources.addNamespace("mpiwg",MPIWG);
|
|
136
|
|
137 return xpathResources;
|
|
138 }
|
|
139
|
|
140 public String getImagePathFromIndexMeta() {
|
|
141 SAXBuilder db = new SAXBuilder();
|
|
142
|
|
143 Document dc;
|
|
144 try {
|
|
145 dc = db.build(metaDataLink);
|
|
146 } catch (JDOMException e) {
|
|
147 // TODO Auto-generated catch block
|
|
148 e.printStackTrace();
|
|
149 return null;
|
|
150 } catch (IOException e) {
|
|
151 // TODO Auto-generated catch block
|
|
152 e.printStackTrace();
|
|
153 return null;
|
|
154 }
|
|
155
|
|
156 Element retElement = null;
|
|
157 try {
|
|
158 XPath xp = getXPath("//texttool/image");
|
|
159 retElement = (Element) xp.selectSingleNode(dc);
|
|
160
|
|
161 } catch (JDOMException e) {
|
|
162 //nothing to be done
|
|
163 }
|
|
164 if (retElement==null) { // versuche noch mal mit namespace
|
|
165 try {
|
|
166 XPath xp = getXPath("//mpiwg:texttool/mpiwg:image");
|
|
167 retElement = (Element) xp.selectSingleNode(dc);
|
|
168
|
|
169 } catch (JDOMException e) {
|
|
170 //nothing to be done
|
|
171 }
|
|
172
|
|
173 }
|
|
174 if (retElement==null)
|
|
175 return "";
|
|
176 return retElement.getTextTrim();
|
|
177 }
|
|
178
|
|
179 }
|