Mercurial > hg > mpdl-group
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/escidoc/TestESciDoc.java Wed Nov 24 17:24:23 2010 +0100 @@ -0,0 +1,367 @@ +package de.mpg.mpiwg.berlin.mpdl.escidoc; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Date; +import java.util.Scanner; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + +import org.quartz.impl.StdSchedulerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; +import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; +import de.mpg.mpiwg.berlin.mpdl.schedule.MpdlChainScheduler; +import de.mpg.mpiwg.berlin.mpdl.util.XmlUtil; + + +public class TestESciDoc { + private String cookieId; + private ESciDocRestSession eSciDocRestSession; + private String organizationalUnit = MpdlConstants.MPDL_ESCIDOC_OUM_ID; + private String fullContextId = MpdlConstants.MPDL_ESCIDOC_CONTEXT_ID; + private String archimedesContainerId = MpdlConstants.MPDL_ESCIDOC_ARCHIMEDES_CONTAINER_ID; + private String echoContainerId = MpdlConstants.MPDL_ESCIDOC_ECHO_CONTAINER_ID; + + public static void main(String[] args) { + try { + /* + byte[] ligatureBytes = new byte[5]; + ligatureBytes[0] = (byte) Integer.parseInt("61", 16); + ligatureBytes[1] = (byte) Integer.parseInt("74", 16); + ligatureBytes[2] = (byte) Integer.parseInt("EE", 16); + ligatureBytes[3] = (byte) Integer.parseInt("A2", 16); + ligatureBytes[4] = (byte) Integer.parseInt("BF", 16); + + String ligature = new String(ligatureBytes, "utf-8"); + char[] chars = new char[3]; + chars[0] = ligature.charAt(0); + chars[1] = ligature.charAt(1); + chars[2] = ligature.charAt(2); + int codepoint = Character.codePointAt(chars, 2); + int num = Character.getNumericValue(ligature.charAt(2)); + int type = Character.getType(ligature.charAt(2)); + */ + + TestESciDoc test = new TestESciDoc(); + test.init("jwillenborg"); // init eSciDoc-Session with cookie as user jwillenborg + + // test.grant("aeisemann", "admin"); + String uid = test.getUserId("aeisemann"); + String users = test.getAllUsers(); + String grantAdmin = test.getGrantHrefByUserNameAndRoleName("aeisemann", "escidoc:role-system-administrator"); + String grants = test.getGrantsByUserName("aeisemann"); + String bla = ""; + + // test.testSchemaValidation(); + + // test.deleteItem("/ir/item/escidoc:48488"); + // test.deleteContainer("/ir/container/escidoc:48486"); + /* + String containerId = test.createContainer("testJoey1"); + System.out.println("Begin: " + (new Date()).getTime()); + Date successDate = test.addMembersToContainer("/ir/container/escidoc:41646"); + System.out.println("End: " + (new Date()).getTime()); + */ + + // String contextId = test.createContext(test.organizationalUnit, "MPDL-XML-Test", "MPDL-XML-Test", "MpdlType"); + // test.openContext("escidoc:38600"); + + // String containerId = test.createContainer("eXistArchimedesContainer"); + // System.out.println(containerId); + // String containerId = test.createContainer("eXistEchoContainer"); + + /* + for (int i=0; i< 443; i++) { + MetadataRecord mdRecordImage = new MetadataRecord(); + mdRecordImage.setIdentifier("file_" + i); + mdRecordImage.setTitle("ECHO scanned page: " + i); + String fileName = "000" + i; + String srcUrl = "http://echo.mpiwg-berlin.mpg.de/zogilib?fn=/permanent/library/" + "163127KK" + "/pageimg/" + fileName; // TODO + String itemId = test.createItem("mpiwg:47114711", mdRecordImage, fileName, "image/jpeg", "JPEG_DEFAULT", "external-url", srcUrl); + System.out.println(i + ". " + itemId + " created"); + } + */ + /* + String srcUrlAlvarus = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/alvarus_1509_lat_V40_10.xml"; + Date pubYearAlvarus = XmlUtil.getInstance().toDate("1509-01-01T00:00:00.000Z"); + 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); + String result = test.createItemInContainer(test.echoContainerId, mdAlvarus.getIdentifier(), mdAlvarus, srcUrlAlvarus); + + String srcUrlBenedetti = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/Benedetti_1585.xml"; + Date pubYearBenedetti = XmlUtil.getInstance().toDate("1585-01-01T00:00:00.000Z"); + MetadataRecord mdBenedetti = new MetadataRecord("/echo/la/Benedetti_1585.xml", "la", "Benedetti, Giovanni Battista de", "Diversarum Speculationum mathematicum, & physicarum liber", null, null, "text/xml", pubYearBenedetti); + String result = test.createItemInContainer(test.echoContainerId, mdBenedetti.getIdentifier(), mdBenedetti, srcUrlBenedetti); + + String srcUrlEuclid = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/el/Euclid-300.xml"; + Date pubYearEuclid = XmlUtil.getInstance().toDate("0300-01-01T00:00:00.000Z"); + MetadataRecord mdEuclid = new MetadataRecord("/echo/el/Euclid-300.xml", "el", "Euclid", "Στοιχεῖα", null, null, "text/xml", pubYearEuclid); + String result = test.createItemInContainer(test.echoContainerId, mdEuclid.getIdentifier(), mdEuclid, srcUrlEuclid); + + String srcUrlEuclid2 = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/zh/Euclid_1966_V8.xml"; + Date pubYearEuclid2 = XmlUtil.getInstance().toDate("0300-01-01T00:00:00.000Z"); + MetadataRecord mdEuclid2 = new MetadataRecord("/echo/zh/Euclid_1966_V8.xml", "zh", "Euclid", "Jihe yuanben, 幾何原本", null, null, "text/xml", pubYearEuclid2); + String result = test.createItemInContainer(test.echoContainerId, mdEuclid2.getIdentifier(), mdEuclid2, "/echo/zh/Euclid_1966_V8.xml", "text/xml", "any fulltext", "internal-managed", srcUrlEuclid2); + + String items = test.getItemsByContainerIdAndFilter(test.echoContainerId, null); + */ + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void init(String userName) throws ApplicationException { + Scanner in = new Scanner(System.in); + System.out.print("Username: " + userName + ", Password: "); + String password = in.nextLine(); + in.close(); + cookieId = ESciDocRestSession.login(userName, password); + eSciDocRestSession = ESciDocRestSession.getInstance(cookieId); + fullContextId = MpdlConstants.MPDL_ESCIDOC_CONTEXT_ID; + } + + private void testSchemaValidation() throws ApplicationException { + String[] rncSchemaFiles = { + "echo/echo.rnc", + "echo/modules/echo-datatype.rnc", "echo/modules/echo-handwritten.rnc", "echo/modules/echo-start.rnc", + "echo/modules/echo-attribute.rnc", "echo/modules/echo-de.rnc", "echo/modules/echo-import-mathml.rnc", "echo/modules/echo-text.rnc", + "echo/modules/echo-block-scholarly.rnc", "echo/modules/echo-div.rnc", "echo/modules/echo-import-xhtml.rnc", "echo/modules/echo-textflows.rnc", + "echo/modules/echo-block.rnc", "echo/modules/echo-figure.rnc", "echo/modules/echo-mathematics.rnc", + "echo/modules/echo-chinese-text.rnc", "echo/modules/echo-float.rnc", "echo/modules/echo-metadata.rnc", + "echo/modules/echo-content-scholarly.rnc", "echo/modules/echo-gap.rnc", "echo/modules/echo-milestone.rnc", + "echo/modules/echo-content.rnc", "echo/modules/echo-gis.rnc", "echo/modules/echo-note.rnc", + "xhtml/xhtml-datatypes.rnc", "xhtml/xhtml-list.rnc", "xhtml/xhtml-attribs.rnc", "xhtml/xhtml-basic-table.rnc" + }; + String[] schemas = { + "echo-schema/dcterms.xsd", "echo-schema/echo-datatype.xsd", "echo-schema/echo-handwritten.xsd", "echo-schema/echo-start.xsd", "echo-schema/xhtml-datatypes.xsd", + "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", + "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", + "echo-schema/echo-block.xsd", "echo-schema/echo-figure.xsd", "echo-schema/echo-mathematics.xsd", "echo-schema/echo.xsd", "echo-schema/xml.xsd", + "echo-schema/echo-chinese-text.xsd", "echo-schema/echo-float.xsd", "echo-schema/ echo-metadata.xsd", "echo-schema/local.xsd", "echo-schema/xsi.xsd", + "echo-schema/echo-content-scholarly.xsd", "echo-schema/echo-gap.xsd", "echo-schema/echo-milestone.xsd", "echo-schema/xhtml-attribs.xsd", + "echo-schema/echo-content.xsd", "echo-schema/echo-gis.xsd", "echo-schema/echo-note.xsd", "echo-schema/xhtml-basic-table.xsd" + }; + File xmlFile = new File("/Users/jwillenborg/texts/echo/SongYingxing_1637.xml"); + // validate(xmlFile, schemas); + validateByRelaxNG(xmlFile, rncSchemaFiles); + } + + private void validateDocumentBuilder(File xmlFile, String[] schemaFileNames) throws ApplicationException { + String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; + String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; + String W3C_XML_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI; + Node root = null; + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); + dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaFileNames); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(xmlFile); + root = doc.getFirstChild(); + String bla = ""; + } catch (Exception e) { + throw new ApplicationException(e); + } + } + + public void validateByRelaxNG(File xmlFile, String[] schemaFileNames) throws ApplicationException { + // System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.XMLSyntaxSchemaFactory"); + System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); + // RELAX NG factory + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); + // Compile the schema. + Schema schema = null; + try { + URL schemaUrl = new URL("http://mpdl-test.mpiwg-berlin.mpg.de:30030/exist/rest/db/mpdl/schema/echo/echo.rnc"); + schema = factory.newSchema(schemaUrl); + } catch (SAXException e) { + throw new ApplicationException(e); + } catch (MalformedURLException e) { + throw new ApplicationException(e); + } + // Get a validator from the schema. + Validator validator = schema.newValidator(); + // Check the document + InputSource inputSource = new InputSource(xmlFile.getPath()); + Source source = new SAXSource(inputSource); + try { + validator.validate(source); + } catch (SAXException e) { + throw new ApplicationException(e); + } catch (IOException e) { + throw new ApplicationException(e); + } + } + + private void validate(File xmlFile, String[] schemaFileNames) throws ApplicationException { + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + // Compile the schema. + Schema schema = null; + Source[] schemaInputSources = new SAXSource[schemaFileNames.length]; + for (int i=0; i<schemaFileNames.length; i++) { + String schemaFileName = "mpdl/extensions/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/escidoc/" + schemaFileNames[i]; + InputSource inputSource = new InputSource(schemaFileName); + Source source = new SAXSource(inputSource); + schemaInputSources[i] = source; + } + try { + schema = factory.newSchema(schemaInputSources); + } catch (SAXException e) { + throw new ApplicationException(e); + } + // Get a validator from the schema. + Validator validator = schema.newValidator(); + // Check the document + InputSource inputSource = new InputSource(xmlFile.getPath()); + Source source = new SAXSource(inputSource); + // DOMSource source = new DOMSource(docNode); // TODO evtl. nur per Node aufrufen + try { + validator.validate(source); + } catch (SAXException e) { + throw new ApplicationException(e); + } catch (IOException e) { + throw new ApplicationException(e); + } + } + + private String getUserId(String userName) throws ApplicationException { + String userId = eSciDocRestSession.getUserId(userName); + return userId; + } + + private String getAllUsers() throws ApplicationException { + String users = eSciDocRestSession.getAllUsers(); + return users; + } + + private String getGrantsByUserName(String userName) throws ApplicationException { + String grant = eSciDocRestSession.getGrantsByUserName(userName); + return grant; + } + + private String getGrantHrefByUserNameAndRoleName(String userName, String roleName) throws ApplicationException { + String grant = eSciDocRestSession.getGrantHrefByUserNameAndRoleName(userName, roleName); + return grant; + } + + private void grant(String userId, String role) throws ApplicationException { + eSciDocRestSession.grant(userId, role); + } + + private String createContext(String organizationalUnit, String name, String description, String type) throws ApplicationException { + fullContextId = eSciDocRestSession.createContext(organizationalUnit, name, description, type); + return fullContextId; + } + + private void openContext(String contextId) throws ApplicationException { + eSciDocRestSession.openContext(contextId); + } + + private String createContainer(String containerName) throws ApplicationException { + MetadataRecord mdContainerEcho = new MetadataRecord(containerName, null, null, null, null, null, null, null, null); + String containerId = eSciDocRestSession.createContainer(containerName, mdContainerEcho); + // String nextContainerId = eSciDocRestSession.createContainerInContainer(containerName, mdContainerEcho, "/ir/container/escidoc:25163"); + // String bla = eSciDocRestSession.createItemInContainer("bla", mdContainerEcho, "bla.xml", "text/xml", "any fulltext", "internal-managed", "/ir/container/escidoc:25164", "http://test.com"); + String pageImageUrl = "http://nausikaa2.rz-berlin.mpg.de/digitallibrary/servlet/Scaler?fn=/permanent/archimedes/monte_mecha_037_it_1581/037-01-pageimg&pn=1&dh=600"; // TODO + // String pageItemId = eSciDocRestSession.createItemInContainer(containerId, "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO + // pageItemId = eSciDocRestSession.createItemInContainer("/ir/container/escidoc:25164", "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO + // pageItemId = eSciDocRestSession.createItemInContainer("/ir/container/escidoc:25164", "bla", mdContainerEcho, "/permanent/archimedes/monte_mecha_037_it_1581", "image/jpeg", "JPEG_DEFAULT", "external-url", pageImageUrl); // TODO + return containerId; + } + + private Item createItemInContainer(String containerId, String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException { + Item result = eSciDocRestSession.createItemInContainer(containerId, pid, mdRecord, components); + return result; + } + + private Item createItem(String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException { + Item result = eSciDocRestSession.createItem(pid, mdRecord, components); + return result; + } + + private Date addMembersToContainer(String containerId) throws ApplicationException { + String modDateStr = "2010-04-16T15:00:53.409Z"; + Date modDate = XmlUtil.getInstance().toDate(modDateStr); + ArrayList<String> memberIds = new ArrayList<String>(); + for (int i=40761; i<= 41645; i = i + 2) { + String memberId = "" + i; + memberIds.add(memberId); + } + Date result = eSciDocRestSession.addMembers(containerId, modDate, memberIds); + return result; + } + + private String getItemsByContainerIdAndFilter(String containerId, String filter) throws ApplicationException { + String members = eSciDocRestSession.getMembersByContainerIdAndFilter(containerId, filter); + return members; + } + + private void deleteContainer(String containerId) throws ApplicationException { + eSciDocRestSession.deleteContainer(containerId); + } + + private void deleteItem(String itemId) throws ApplicationException { + eSciDocRestSession.deleteItem(itemId); + } + + private void test() { + /* + XmlUtil xmlUtil = XmlUtil.getInstance(); + ClientSession eSciDocClientSession = new ClientSession("http", "xserve07.mpiwg-berlin.mpg.de", 8080, "jwillenborg", "pucki123"); + String contextId = "/ir/context/escidoc:23002"; + String filterDetail = "<filter></filter><order-by sorting=\"ascending\">/id</order-by>"; + String filter = "<param>" + filterDetail + "</param>"; + String members = eSciDocClientSession.getItemsByContainerIdAndFilter("escidoc:23003", filter); + System.out.println(members); + + String item = eSciDocClientSession.getItem("escidoc:13003"); + String itemId = xmlUtil.getFirstElementAttributeValue(item, "prop:latest-version", "xlink:href"); + System.out.println(itemId); + /* + Date pubYearAlvarus = xmlUtil.toDate("1509-01-01T00:00:00.000Z"); + 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); + String srcUrlAlvarus = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/echo/la/alvarus_1509_lat_V40_10.xml"; + String result = eSciDocClientSession.createItemInContainer(contextId, "MPDL-XML-Test", "abcdefg", mdAlvarus, "escidoc:23003", srcUrlAlvarus); + */ + + /* + Date pubYearAgricola = xmlUtil.toDate("1912-01-01T00:00:00.000Z"); + MetadataRecord mdAgricola = new MetadataRecord("/archimedes/en/agric_remet_002_en.xml", "la", "Agricola, Georgius", "De re metallica", null, "London", "text/xml", pubYearAgricola); + String srcUrlAgricola = "http://mpdl-proto.mpiwg-berlin.mpg.de/exist/rest/db/mpdl/documents/standard/archimedes/en/agric_remet_002_en.xml"; + String item = eSciDocClientSession.getItemById("escidoc:23012"); + String lastVersionDateStr = xmlUtil.getFirstElementValue(item, "version:date"); + Date lastModificationDate = xmlUtil.toDate(lastVersionDateStr); + String lastModificationDateStr = xmlUtil.toXsDate(lastModificationDate); + String itemXmlResult = eSciDocClientSession.updateItem(contextId, "MPDL-XML-Test", "abcdefg", mdAgricola, srcUrlAgricola, "escidoc:23012", lastModificationDate); + */ + /* + String itemIdAgricola = xmlUtil.getFirstElementAttributeValue(itemXmlResult, "prop:latest-version", "xlink:href"); + eSciDocClientSession.deleteItem(contextId, "escidoc:23010"); + */ + /* + String containerName = "testEXistEcho"; + MetadataRecord mdRecord = new MetadataRecord("testEXistArchimedes", "Echo document container"); + String containerXmlResult = eSciDocClientSession.addContainer("/ir/context/escidoc:23002", "MPDL-XML-Test", containerPid, mdRecord); + String containerId = xmlUtil.getFirstElementAttributeValue(containerXmlResult, "prop:latest-version", "xlink:href"); + System.out.println(containerId); + */ + // eSciDocClientSession.deleteItem(contextId, "escidoc:23012"); + } +}