comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/xml/SchemaHandler.java @ 10:59ff47d1e237

TEI Unterst?tzung, Fehlerbehebungen, externe Objekte
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Fri, 11 Mar 2011 13:33:26 +0100
parents 408254cf2f1d
children 257f67be5c00
comparison
equal deleted inserted replaced
9:1ec29fdd0db8 10:59ff47d1e237
21 21
22 public class SchemaHandler { 22 public class SchemaHandler {
23 23
24 /** 24 /**
25 * 25 *
26 * @param fileName 26 * @param fileName local file name to validate
27 * @param docOperation 27 * @param docOperation
28 * @return doc root node of xml file 28 * @return doc root node of xml file
29 * @throws ApplicationException 29 * @throws ApplicationException
30 */ 30 */
31 public Node validate(String fileName, MpdlDocOperation docOperation) throws ApplicationException { 31 public Node validate(String fileName, MpdlDocOperation docOperation) throws ApplicationException {
67 mdRecord = getMetadataRecordArchimedes(documentNode); 67 mdRecord = getMetadataRecordArchimedes(documentNode);
68 if (mdRecord != null) { 68 if (mdRecord != null) {
69 String id = getIdByExistId(eXistIdentifier); 69 String id = getIdByExistId(eXistIdentifier);
70 mdRecord.setIdentifier("ARCHIMEDES:" + id + ".xml"); 70 mdRecord.setIdentifier("ARCHIMEDES:" + id + ".xml");
71 } 71 }
72 } else if (docBase != null && docBase.equals("tei")) {
73 mdRecord = getMetadataRecordTEI(documentNode);
74 if (mdRecord != null) {
75 String id = getIdByExistId(eXistIdentifier);
76 mdRecord.setIdentifier("TEI:" + id + ".xml");
77 }
72 } 78 }
73 if (mdRecord != null) { 79 if (mdRecord != null) {
74 mdRecord.setEXistIdentifier(eXistIdentifier); 80 mdRecord.setEXistIdentifier(eXistIdentifier);
75 mdRecord.setMediaType("fulltext"); 81 mdRecord.setMediaType("fulltext");
76 } 82 }
128 private void validate(Node docNode, String docBase) throws ApplicationException { 134 private void validate(Node docNode, String docBase) throws ApplicationException {
129 XmlUtil xmlUtil = XmlUtil.getInstance(); 135 XmlUtil xmlUtil = XmlUtil.getInstance();
130 NamespaceContext nsContext = getEchoNsContext(); 136 NamespaceContext nsContext = getEchoNsContext();
131 String echoTest = null; 137 String echoTest = null;
132 String archimedesTest = null; 138 String archimedesTest = null;
139 String teiTest = null;
133 try { 140 try {
134 echoTest = xmlUtil.evaluateToString(docNode, "/echo:echo/echo:metadata", nsContext); 141 echoTest = xmlUtil.evaluateToString(docNode, "/echo:echo/echo:metadata", nsContext);
135 archimedesTest = xmlUtil.evaluateToString(docNode, "/archimedes/info", null); 142 archimedesTest = xmlUtil.evaluateToString(docNode, "/archimedes/info", null);
143 teiTest = xmlUtil.evaluateToString(docNode, "/TEI/teiHeader", null);
136 } catch (ApplicationException e) { 144 } catch (ApplicationException e) {
137 throw new ApplicationException("Your source file is not an \"echo\" or \"archimedes\" file. Please proof that file."); 145 throw new ApplicationException("Your source file is not an \"echo\" or \"archimedes\" file. Please proof that file.");
138 } 146 }
139 if (docBase.equals("echo") && archimedesTest != null) 147 if (docBase.equals("echo") && archimedesTest != null)
140 throw new ApplicationException("Your source file is an \"archimedes\" file. " + "Please specify \"archimedes\" in your destination document base."); 148 throw new ApplicationException("Your source file is an \"archimedes\" file. " + "Please specify \"archimedes\" in your destination document base.");
149 if (docBase.equals("echo") && teiTest != null)
150 throw new ApplicationException("Your source file is a \"TEI\" file. " + "Please specify \"TEI\" in your destination document base.");
141 if (docBase.equals("archimedes") && echoTest != null) 151 if (docBase.equals("archimedes") && echoTest != null)
152 throw new ApplicationException("Your source file is an \"echo\" file. " + "Please specify \"echo\" in your destination document base.");
153 if (docBase.equals("archimedes") && teiTest != null)
154 throw new ApplicationException("Your source file is a \"archimedes\" file. " + "Please specify \"TEI\" in your destination document base.");
155 if (docBase.equals("tei") && archimedesTest != null)
156 throw new ApplicationException("Your source file is an \"archimedes\" file. " + "Please specify \"archimedes\" in your destination document base.");
157 if (docBase.equals("tei") && echoTest != null)
142 throw new ApplicationException("Your source file is an \"echo\" file. " + "Please specify \"echo\" in your destination document base."); 158 throw new ApplicationException("Your source file is an \"echo\" file. " + "Please specify \"echo\" in your destination document base.");
143 } 159 }
144 160
145 private void validateByRelaxNGSchema(File destFile, String docBase) throws ApplicationException { 161 private void validateByRelaxNGSchema(File destFile, String docBase) throws ApplicationException {
146 XmlUtil xmlUtil = XmlUtil.getInstance(); 162 XmlUtil xmlUtil = XmlUtil.getInstance();
147 if (docBase.equals("echo")) { 163 if (docBase.equals("echo")) {
148 URL echoSchemaUrl = getEchoRelaxNGSchemaUrl(); 164 URL echoSchemaUrl = getEchoRelaxNGSchemaUrl();
149 xmlUtil.validateByRelaxNG(destFile, echoSchemaUrl); 165 xmlUtil.validateByRelaxNG(destFile, echoSchemaUrl);
166 } else if (docBase.equals("tei")) {
167 URL teiSchemaUrl = getTeiLiteRelaxNGSchemaUrl();
168 xmlUtil.validateByRelaxNG(destFile, teiSchemaUrl);
150 } 169 }
151 } 170 }
152 171
153 private URL getEchoRelaxNGSchemaUrl() throws ApplicationException { 172 private URL getEchoRelaxNGSchemaUrl() throws ApplicationException {
154 String echoSchemaUrlStr = "http://" + MpdlConstants.MPDL_EXIST_HOST_NAME + ":" + MpdlConstants.MPDL_EXIST_PORT + MpdlConstants.MPDL_ECHO_RELAXNG_PATH; 173 String echoSchemaUrlStr = "http://" + MpdlConstants.MPDL_EXIST_HOST_NAME + ":" + MpdlConstants.MPDL_EXIST_PORT + MpdlConstants.MPDL_ECHO_RELAXNG_PATH;
157 echoSchemaUrl = new URL(echoSchemaUrlStr); 176 echoSchemaUrl = new URL(echoSchemaUrlStr);
158 } catch (MalformedURLException e) { 177 } catch (MalformedURLException e) {
159 throw new ApplicationException(e); 178 throw new ApplicationException(e);
160 } 179 }
161 return echoSchemaUrl; 180 return echoSchemaUrl;
181 }
182
183 private URL getTeiLiteRelaxNGSchemaUrl() throws ApplicationException {
184 String schemaUrlStr = "http://" + MpdlConstants.MPDL_EXIST_HOST_NAME + ":" + MpdlConstants.MPDL_EXIST_PORT + MpdlConstants.MPDL_TEILITE_RELAXNG_PATH;
185 URL schemaUrl = null;
186 try {
187 schemaUrl = new URL(schemaUrlStr);
188 } catch (MalformedURLException e) {
189 throw new ApplicationException(e);
190 }
191 return schemaUrl;
162 } 192 }
163 193
164 private void validate(MetadataRecord mdRecord) throws ApplicationException { 194 private void validate(MetadataRecord mdRecord) throws ApplicationException {
165 String identifier = mdRecord.getIdentifier(); 195 String identifier = mdRecord.getIdentifier();
166 String creator = mdRecord.getCreator(); 196 String creator = mdRecord.getCreator();
245 String rights = "open access"; 275 String rights = "open access";
246 String license = "http://echo.mpiwg-berlin.mpg.de/policy/oa_basics/declaration"; 276 String license = "http://echo.mpiwg-berlin.mpg.de/policy/oa_basics/declaration";
247 String accessRights = "free"; 277 String accessRights = "free";
248 MetadataRecord mdRecord = new MetadataRecord(identifier, language, creator, title, null, null, "text/xml", rights, date); 278 MetadataRecord mdRecord = new MetadataRecord(identifier, language, creator, title, null, null, "text/xml", rights, date);
249 mdRecord.setDocBase("archimedes"); 279 mdRecord.setDocBase("archimedes");
280 mdRecord.setLicense(license);
281 mdRecord.setAccessRights(accessRights);
282 return mdRecord;
283 }
284
285 private MetadataRecord getMetadataRecordTEI(Node documentNode) throws ApplicationException {
286 XmlUtil xmlUtil = XmlUtil.getInstance();
287 NamespaceContext nsContext = getTeiNsContext();
288 String creator = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:fileDesc/TEI:titleStmt/TEI:author", nsContext);
289 if (creator != null)
290 creator = StringUtilEscapeChars.deresolveXmlEntities(creator);
291 String title = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:fileDesc/TEI:titleStmt/TEI:title", nsContext);
292 if (title != null)
293 title = StringUtilEscapeChars.deresolveXmlEntities(title);
294 String language = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:profileDesc/TEI:langUsage/TEI:language/@ident", nsContext);
295 if (language != null)
296 language = StringUtilEscapeChars.deresolveXmlEntities(language);
297 String yearStr = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:fileDesc/TEI:publicationStmt/TEI:date", nsContext);
298 Date date = null;
299 if (yearStr != null && ! yearStr.equals("")) {
300 yearStr = StringUtilEscapeChars.deresolveXmlEntities(yearStr);
301 yearStr = new Util().toYearStr(yearStr); // test if possible etc
302 if (yearStr != null)
303 date = XmlUtil.getInstance().toDate(yearStr + "-01-01T00:00:00.000Z");
304 }
305 String rights = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:fileDesc/TEI:publicationStmt/TEI:availability", nsContext);
306 if (rights == null)
307 rights = "open access";
308 rights = StringUtilEscapeChars.deresolveXmlEntities(rights);
309 String license = "http://echo.mpiwg-berlin.mpg.de/policy/oa_basics/declaration";
310 String accessRights = xmlUtil.evaluateToString(documentNode, "/TEI:TEI/TEI:teiHeader/TEI:fileDesc/TEI:publicationStmt/TEI:availability/@status", nsContext);
311 if (accessRights == null)
312 accessRights = "free";
313 accessRights = StringUtilEscapeChars.deresolveXmlEntities(accessRights);
314 MetadataRecord mdRecord = new MetadataRecord(null, language, creator, title, null, null, "text/xml", rights, date);
315 mdRecord.setDocBase("tei");
250 mdRecord.setLicense(license); 316 mdRecord.setLicense(license);
251 mdRecord.setAccessRights(accessRights); 317 mdRecord.setAccessRights(accessRights);
252 return mdRecord; 318 return mdRecord;
253 } 319 }
254 320
362 } 428 }
363 }; 429 };
364 return nsContext; 430 return nsContext;
365 } 431 }
366 432
433 public NamespaceContext getTeiNsContext() {
434 NamespaceContext nsContext = new NamespaceContext() {
435 public String getNamespaceURI(String prefix) {
436 String uri;
437 if (prefix.equals("TEI"))
438 uri = "http://www.tei-c.org/ns/1.0";
439 else if (prefix.equals("xhtml"))
440 uri = "http://www.w3.org/1999/xhtml";
441 else if (prefix.equals("xlink"))
442 uri = "http://www.w3.org/1999/xlink";
443 else if (prefix.equals("mml"))
444 uri = "http://www.w3.org/1998/Math/MathML";
445 else
446 uri = null;
447 return uri;
448 }
449
450 public String getPrefix(String uri) {
451 if (uri.equals("http://www.tei-c.org/ns/1.0"))
452 return "TEI";
453 else if (uri.equals("http://www.w3.org/1999/xhtml"))
454 return "xhtml";
455 else if (uri.equals("http://www.w3.org/1999/xlink"))
456 return "xlink";
457 else if (uri.equals("http://www.w3.org/1998/Math/MathML"))
458 return "mml";
459 else
460 return null;
461 }
462
463 public Iterator getPrefixes(String namespace) {
464 return null;
465 }
466 };
467 return nsContext;
468 }
367 469
368 } 470 }