comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/escidoc/TestESciDoc.java @ 0:408254cf2f1d

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 24 Nov 2010 17:24:23 +0100
parents
children 2396a569e446
comparison
equal deleted inserted replaced
-1:000000000000 0:408254cf2f1d
1 package de.mpg.mpiwg.berlin.mpdl.escidoc;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.Scanner;
10
11 import javax.xml.XMLConstants;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.transform.Source;
15 import javax.xml.transform.dom.DOMSource;
16 import javax.xml.transform.sax.SAXSource;
17 import javax.xml.validation.Schema;
18 import javax.xml.validation.SchemaFactory;
19 import javax.xml.validation.Validator;
20
21 import org.quartz.impl.StdSchedulerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Node;
24 import org.xml.sax.InputSource;
25 import org.xml.sax.SAXException;
26
27 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
28 import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants;
29 import de.mpg.mpiwg.berlin.mpdl.schedule.MpdlChainScheduler;
30 import de.mpg.mpiwg.berlin.mpdl.util.XmlUtil;
31
32
33 public class TestESciDoc {
34 private String cookieId;
35 private ESciDocRestSession eSciDocRestSession;
36 private String organizationalUnit = MpdlConstants.MPDL_ESCIDOC_OUM_ID;
37 private String fullContextId = MpdlConstants.MPDL_ESCIDOC_CONTEXT_ID;
38 private String archimedesContainerId = MpdlConstants.MPDL_ESCIDOC_ARCHIMEDES_CONTAINER_ID;
39 private String echoContainerId = MpdlConstants.MPDL_ESCIDOC_ECHO_CONTAINER_ID;
40
41 public static void main(String[] args) {
42 try {
43 /*
44 byte[] ligatureBytes = new byte[5];
45 ligatureBytes[0] = (byte) Integer.parseInt("61", 16);
46 ligatureBytes[1] = (byte) Integer.parseInt("74", 16);
47 ligatureBytes[2] = (byte) Integer.parseInt("EE", 16);
48 ligatureBytes[3] = (byte) Integer.parseInt("A2", 16);
49 ligatureBytes[4] = (byte) Integer.parseInt("BF", 16);
50
51 String ligature = new String(ligatureBytes, "utf-8");
52 char[] chars = new char[3];
53 chars[0] = ligature.charAt(0);
54 chars[1] = ligature.charAt(1);
55 chars[2] = ligature.charAt(2);
56 int codepoint = Character.codePointAt(chars, 2);
57 int num = Character.getNumericValue(ligature.charAt(2));
58 int type = Character.getType(ligature.charAt(2));
59 */
60
61 TestESciDoc test = new TestESciDoc();
62 test.init("jwillenborg"); // init eSciDoc-Session with cookie as user jwillenborg
63
64 // test.grant("aeisemann", "admin");
65 String uid = test.getUserId("aeisemann");
66 String users = test.getAllUsers();
67 String grantAdmin = test.getGrantHrefByUserNameAndRoleName("aeisemann", "escidoc:role-system-administrator");
68 String grants = test.getGrantsByUserName("aeisemann");
69 String bla = "";
70
71 // test.testSchemaValidation();
72
73 // test.deleteItem("/ir/item/escidoc:48488");
74 // test.deleteContainer("/ir/container/escidoc:48486");
75 /*
76 String containerId = test.createContainer("testJoey1");
77 System.out.println("Begin: " + (new Date()).getTime());
78 Date successDate = test.addMembersToContainer("/ir/container/escidoc:41646");
79 System.out.println("End: " + (new Date()).getTime());
80 */
81
82 // String contextId = test.createContext(test.organizationalUnit, "MPDL-XML-Test", "MPDL-XML-Test", "MpdlType");
83 // test.openContext("escidoc:38600");
84
85 // String containerId = test.createContainer("eXistArchimedesContainer");
86 // System.out.println(containerId);
87 // String containerId = test.createContainer("eXistEchoContainer");
88
89 /*
90 for (int i=0; i< 443; i++) {
91 MetadataRecord mdRecordImage = new MetadataRecord();
92 mdRecordImage.setIdentifier("file_" + i);
93 mdRecordImage.setTitle("ECHO scanned page: " + i);
94 String fileName = "000" + i;
95 String srcUrl = "http://echo.mpiwg-berlin.mpg.de/zogilib?fn=/permanent/library/" + "163127KK" + "/pageimg/" + fileName; // TODO
96 String itemId = test.createItem("mpiwg:47114711", mdRecordImage, fileName, "image/jpeg", "JPEG_DEFAULT", "external-url", srcUrl);
97 System.out.println(i + ". " + itemId + " created");
98 }
99 */
100 /*
101 String srcUrlAlvarus = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/alvarus_1509_lat_V40_10.xml";
102 Date pubYearAlvarus = XmlUtil.getInstance().toDate("1509-01-01T00:00:00.000Z");
103 MetadataRecord mdAlvarus = new MetadataRecord("/echo/la/alvarus_1509_lat_V40_10.xml", "la", "Alvarus, Thomas", "Liber de triplici motu proportionibus annexis magiſtri Aluari Thome Ulixboneñ philoſophicas Suiſeth calculationes ex parte declarans", null, null, "text/xml", pubYearAlvarus);
104 String result = test.createItemInContainer(test.echoContainerId, mdAlvarus.getIdentifier(), mdAlvarus, srcUrlAlvarus);
105
106 String srcUrlBenedetti = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/Benedetti_1585.xml";
107 Date pubYearBenedetti = XmlUtil.getInstance().toDate("1585-01-01T00:00:00.000Z");
108 MetadataRecord mdBenedetti = new MetadataRecord("/echo/la/Benedetti_1585.xml", "la", "Benedetti, Giovanni Battista de", "Diversarum Speculationum mathematicum, & physicarum liber", null, null, "text/xml", pubYearBenedetti);
109 String result = test.createItemInContainer(test.echoContainerId, mdBenedetti.getIdentifier(), mdBenedetti, srcUrlBenedetti);
110
111 String srcUrlEuclid = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/el/Euclid-300.xml";
112 Date pubYearEuclid = XmlUtil.getInstance().toDate("0300-01-01T00:00:00.000Z");
113 MetadataRecord mdEuclid = new MetadataRecord("/echo/el/Euclid-300.xml", "el", "Euclid", "Στοιχεῖα", null, null, "text/xml", pubYearEuclid);
114 String result = test.createItemInContainer(test.echoContainerId, mdEuclid.getIdentifier(), mdEuclid, srcUrlEuclid);
115
116 String srcUrlEuclid2 = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/zh/Euclid_1966_V8.xml";
117 Date pubYearEuclid2 = XmlUtil.getInstance().toDate("0300-01-01T00:00:00.000Z");
118 MetadataRecord mdEuclid2 = new MetadataRecord("/echo/zh/Euclid_1966_V8.xml", "zh", "Euclid", "Jihe yuanben, 幾何原本", null, null, "text/xml", pubYearEuclid2);
119 String result = test.createItemInContainer(test.echoContainerId, mdEuclid2.getIdentifier(), mdEuclid2, "/echo/zh/Euclid_1966_V8.xml", "text/xml", "any fulltext", "internal-managed", srcUrlEuclid2);
120
121 String items = test.getItemsByContainerIdAndFilter(test.echoContainerId, null);
122 */
123
124 } catch (Exception e) {
125 e.printStackTrace();
126 }
127 }
128
129 private void init(String userName) throws ApplicationException {
130 Scanner in = new Scanner(System.in);
131 System.out.print("Username: " + userName + ", Password: ");
132 String password = in.nextLine();
133 in.close();
134 cookieId = ESciDocRestSession.login(userName, password);
135 eSciDocRestSession = ESciDocRestSession.getInstance(cookieId);
136 fullContextId = MpdlConstants.MPDL_ESCIDOC_CONTEXT_ID;
137 }
138
139 private void testSchemaValidation() throws ApplicationException {
140 String[] rncSchemaFiles = {
141 "echo/echo.rnc",
142 "echo/modules/echo-datatype.rnc", "echo/modules/echo-handwritten.rnc", "echo/modules/echo-start.rnc",
143 "echo/modules/echo-attribute.rnc", "echo/modules/echo-de.rnc", "echo/modules/echo-import-mathml.rnc", "echo/modules/echo-text.rnc",
144 "echo/modules/echo-block-scholarly.rnc", "echo/modules/echo-div.rnc", "echo/modules/echo-import-xhtml.rnc", "echo/modules/echo-textflows.rnc",
145 "echo/modules/echo-block.rnc", "echo/modules/echo-figure.rnc", "echo/modules/echo-mathematics.rnc",
146 "echo/modules/echo-chinese-text.rnc", "echo/modules/echo-float.rnc", "echo/modules/echo-metadata.rnc",
147 "echo/modules/echo-content-scholarly.rnc", "echo/modules/echo-gap.rnc", "echo/modules/echo-milestone.rnc",
148 "echo/modules/echo-content.rnc", "echo/modules/echo-gis.rnc", "echo/modules/echo-note.rnc",
149 "xhtml/xhtml-datatypes.rnc", "xhtml/xhtml-list.rnc", "xhtml/xhtml-attribs.rnc", "xhtml/xhtml-basic-table.rnc"
150 };
151 String[] schemas = {
152 "echo-schema/dcterms.xsd", "echo-schema/echo-datatype.xsd", "echo-schema/echo-handwritten.xsd", "echo-schema/echo-start.xsd", "echo-schema/xhtml-datatypes.xsd",
153 "echo-schema/echo-attribute.xsd", "echo-schema/echo-de.xsd", "echo-schema/ echo-import-mathml.xsd", "echo-schema/echo-text.xsd", "echo-schema/xhtml-list.xsd",
154 "echo-schema/echo-block-scholarly.xsd", "echo-schema/echo-div.xsd", "echo-schema/echo-import-xhtml.xsd", "echo-schema/echo-textflows.xsd", "echo-schema/xlink.xsd",
155 "echo-schema/echo-block.xsd", "echo-schema/echo-figure.xsd", "echo-schema/echo-mathematics.xsd", "echo-schema/echo.xsd", "echo-schema/xml.xsd",
156 "echo-schema/echo-chinese-text.xsd", "echo-schema/echo-float.xsd", "echo-schema/ echo-metadata.xsd", "echo-schema/local.xsd", "echo-schema/xsi.xsd",
157 "echo-schema/echo-content-scholarly.xsd", "echo-schema/echo-gap.xsd", "echo-schema/echo-milestone.xsd", "echo-schema/xhtml-attribs.xsd",
158 "echo-schema/echo-content.xsd", "echo-schema/echo-gis.xsd", "echo-schema/echo-note.xsd", "echo-schema/xhtml-basic-table.xsd"
159 };
160 File xmlFile = new File("/Users/jwillenborg/texts/echo/SongYingxing_1637.xml");
161 // validate(xmlFile, schemas);
162 validateByRelaxNG(xmlFile, rncSchemaFiles);
163 }
164
165 private void validateDocumentBuilder(File xmlFile, String[] schemaFileNames) throws ApplicationException {
166 String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
167 String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
168 String W3C_XML_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI;
169 Node root = null;
170 try {
171 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
172 dbf.setNamespaceAware(true);
173 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
174 dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaFileNames);
175 DocumentBuilder db = dbf.newDocumentBuilder();
176 Document doc = db.parse(xmlFile);
177 root = doc.getFirstChild();
178 String bla = "";
179 } catch (Exception e) {
180 throw new ApplicationException(e);
181 }
182 }
183
184 public void validateByRelaxNG(File xmlFile, String[] schemaFileNames) throws ApplicationException {
185 // System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory");
186 System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory");
187 // RELAX NG factory
188 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
189 // Compile the schema.
190 Schema schema = null;
191 try {
192 URL schemaUrl = new URL("http://mpdl-test.mpiwg-berlin.mpg.de:30030/exist/rest/db/mpdl/schema/echo/echo.rnc");
193 schema = factory.newSchema(schemaUrl);
194 } catch (SAXException e) {
195 throw new ApplicationException(e);
196 } catch (MalformedURLException e) {
197 throw new ApplicationException(e);
198 }
199 // Get a validator from the schema.
200 Validator validator = schema.newValidator();
201 // Check the document
202 InputSource inputSource = new InputSource(xmlFile.getPath());
203 Source source = new SAXSource(inputSource);
204 try {
205 validator.validate(source);
206 } catch (SAXException e) {
207 throw new ApplicationException(e);
208 } catch (IOException e) {
209 throw new ApplicationException(e);
210 }
211 }
212
213 private void validate(File xmlFile, String[] schemaFileNames) throws ApplicationException {
214 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
215 // Compile the schema.
216 Schema schema = null;
217 Source[] schemaInputSources = new SAXSource[schemaFileNames.length];
218 for (int i=0; i<schemaFileNames.length; i++) {
219 String schemaFileName = "mpdl/extensions/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/escidoc/" + schemaFileNames[i];
220 InputSource inputSource = new InputSource(schemaFileName);
221 Source source = new SAXSource(inputSource);
222 schemaInputSources[i] = source;
223 }
224 try {
225 schema = factory.newSchema(schemaInputSources);
226 } catch (SAXException e) {
227 throw new ApplicationException(e);
228 }
229 // Get a validator from the schema.
230 Validator validator = schema.newValidator();
231 // Check the document
232 InputSource inputSource = new InputSource(xmlFile.getPath());
233 Source source = new SAXSource(inputSource);
234 // DOMSource source = new DOMSource(docNode); // TODO evtl. nur per Node aufrufen
235 try {
236 validator.validate(source);
237 } catch (SAXException e) {
238 throw new ApplicationException(e);
239 } catch (IOException e) {
240 throw new ApplicationException(e);
241 }
242 }
243
244 private String getUserId(String userName) throws ApplicationException {
245 String userId = eSciDocRestSession.getUserId(userName);
246 return userId;
247 }
248
249 private String getAllUsers() throws ApplicationException {
250 String users = eSciDocRestSession.getAllUsers();
251 return users;
252 }
253
254 private String getGrantsByUserName(String userName) throws ApplicationException {
255 String grant = eSciDocRestSession.getGrantsByUserName(userName);
256 return grant;
257 }
258
259 private String getGrantHrefByUserNameAndRoleName(String userName, String roleName) throws ApplicationException {
260 String grant = eSciDocRestSession.getGrantHrefByUserNameAndRoleName(userName, roleName);
261 return grant;
262 }
263
264 private void grant(String userId, String role) throws ApplicationException {
265 eSciDocRestSession.grant(userId, role);
266 }
267
268 private String createContext(String organizationalUnit, String name, String description, String type) throws ApplicationException {
269 fullContextId = eSciDocRestSession.createContext(organizationalUnit, name, description, type);
270 return fullContextId;
271 }
272
273 private void openContext(String contextId) throws ApplicationException {
274 eSciDocRestSession.openContext(contextId);
275 }
276
277 private String createContainer(String containerName) throws ApplicationException {
278 MetadataRecord mdContainerEcho = new MetadataRecord(containerName, null, null, null, null, null, null, null, null);
279 String containerId = eSciDocRestSession.createContainer(containerName, mdContainerEcho);
280 // String nextContainerId = eSciDocRestSession.createContainerInContainer(containerName, mdContainerEcho, "/ir/container/escidoc:25163");
281 // String bla = eSciDocRestSession.createItemInContainer("bla", mdContainerEcho, "bla.xml", "text/xml", "any fulltext", "internal-managed", "/ir/container/escidoc:25164", "http://test.com");
282 String pageImageUrl = "http://nausikaa2.rz-berlin.mpg.de/digitallibrary/servlet/Scaler?fn=/permanent/archimedes/monte_mecha_037_it_1581/037-01-pageimg&amp;pn=1&amp;dh=600"; // TODO
283 // String pageItemId = eSciDocRestSession.createItemInContainer(containerId, "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO
284 // pageItemId = eSciDocRestSession.createItemInContainer("/ir/container/escidoc:25164", "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO
285 // pageItemId = eSciDocRestSession.createItemInContainer("/ir/container/escidoc:25164", "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO
286 return containerId;
287 }
288
289 private Item createItemInContainer(String containerId, String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException {
290 Item result = eSciDocRestSession.createItemInContainer(containerId, pid, mdRecord, components);
291 return result;
292 }
293
294 private Item createItem(String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException {
295 Item result = eSciDocRestSession.createItem(pid, mdRecord, components);
296 return result;
297 }
298
299 private Date addMembersToContainer(String containerId) throws ApplicationException {
300 String modDateStr = "2010-04-16T15:00:53.409Z";
301 Date modDate = XmlUtil.getInstance().toDate(modDateStr);
302 ArrayList<String> memberIds = new ArrayList<String>();
303 for (int i=40761; i<= 41645; i = i + 2) {
304 String memberId = "" + i;
305 memberIds.add(memberId);
306 }
307 Date result = eSciDocRestSession.addMembers(containerId, modDate, memberIds);
308 return result;
309 }
310
311 private String getItemsByContainerIdAndFilter(String containerId, String filter) throws ApplicationException {
312 String members = eSciDocRestSession.getMembersByContainerIdAndFilter(containerId, filter);
313 return members;
314 }
315
316 private void deleteContainer(String containerId) throws ApplicationException {
317 eSciDocRestSession.deleteContainer(containerId);
318 }
319
320 private void deleteItem(String itemId) throws ApplicationException {
321 eSciDocRestSession.deleteItem(itemId);
322 }
323
324 private void test() {
325 /*
326 XmlUtil xmlUtil = XmlUtil.getInstance();
327 ClientSession eSciDocClientSession = new ClientSession("http", "xserve07.mpiwg-berlin.mpg.de", 8080, "jwillenborg", "pucki123");
328 String contextId = "/ir/context/escidoc:23002";
329 String filterDetail = "<filter></filter><order-by sorting=\"ascending\">/id</order-by>";
330 String filter = "<param>" + filterDetail + "</param>";
331 String members = eSciDocClientSession.getItemsByContainerIdAndFilter("escidoc:23003", filter);
332 System.out.println(members);
333
334 String item = eSciDocClientSession.getItem("escidoc:13003");
335 String itemId = xmlUtil.getFirstElementAttributeValue(item, "prop:latest-version", "xlink:href");
336 System.out.println(itemId);
337 /*
338 Date pubYearAlvarus = xmlUtil.toDate("1509-01-01T00:00:00.000Z");
339 MetadataRecord mdAlvarus = new MetadataRecord("/archimedes/la/alvarus_1509_lat_V40_10.xml", "la", "Alvarus, Thomas", "Liber de triplici motu proportionibus annexis magiſtri Aluari Thome Ulixboneñ philoſophicas Suiſeth calculationes ex parte declarans", null, null, "text/xml", pubYearAlvarus);
340 String srcUrlAlvarus = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/alvarus_1509_lat_V40_10.xml";
341 String result = eSciDocClientSession.createItemInContainer(contextId, "MPDL-XML-Test", "abcdefg", mdAlvarus, "escidoc:23003", srcUrlAlvarus);
342 */
343
344 /*
345 Date pubYearAgricola = xmlUtil.toDate("1912-01-01T00:00:00.000Z");
346 MetadataRecord mdAgricola = new MetadataRecord("/archimedes/en/agric_remet_002_en.xml", "la", "Agricola, Georgius", "De re metallica", null, "London", "text/xml", pubYearAgricola);
347 String srcUrlAgricola = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/archimedes/en/agric_remet_002_en.xml";
348 String item = eSciDocClientSession.getItemById("escidoc:23012");
349 String lastVersionDateStr = xmlUtil.getFirstElementValue(item, "version:date");
350 Date lastModificationDate = xmlUtil.toDate(lastVersionDateStr);
351 String lastModificationDateStr = xmlUtil.toXsDate(lastModificationDate);
352 String itemXmlResult = eSciDocClientSession.updateItem(contextId, "MPDL-XML-Test", "abcdefg", mdAgricola, srcUrlAgricola, "escidoc:23012", lastModificationDate);
353 */
354 /*
355 String itemIdAgricola = xmlUtil.getFirstElementAttributeValue(itemXmlResult, "prop:latest-version", "xlink:href");
356 eSciDocClientSession.deleteItem(contextId, "escidoc:23010");
357 */
358 /*
359 String containerName = "testEXistEcho";
360 MetadataRecord mdRecord = new MetadataRecord("testEXistArchimedes", "Echo document container");
361 String containerXmlResult = eSciDocClientSession.addContainer("/ir/context/escidoc:23002", "MPDL-XML-Test", containerPid, mdRecord);
362 String containerId = xmlUtil.getFirstElementAttributeValue(containerXmlResult, "prop:latest-version", "xlink:href");
363 System.out.println(containerId);
364 */
365 // eSciDocClientSession.deleteItem(contextId, "escidoc:23012");
366 }
367 }