[metacat-cvs] metacat/src/edu/ucsb/nceas/metacat MetaCatServlet.java
Saurabh Garg
sgarg at ecoinformatics.org
Tue Nov 1 14:09:29 PST 2005
sgarg 05/11/01 14:09:29
Modified: src/edu/ucsb/nceas/metacat MetaCatServlet.java
Log:
Bugfix for 2091. Removed the function needValidation(). Added function getPrefix which returns the prefix. And use the following steps to find out the namespace
1. Find if the root element has prefix (e.g. <eml:eml>). If found, go to step
2, otheriwse go to step 3.
2. Look for xmlns:prefix element to find the ns
(e.g.:xmlns:eml="eml://ecoinformatics.org/eml-2.0.0")
2a. If xmlns:prefix not found, go to step 3
3. Look for xmlns element to find the ns (e.g.:
xmlns="eml://ecoinformatics.org/eml-2.0.0")
4. If no xmlns element found, you the generic schemea.
Revision Changes Path
1.232 +79 -59 metacat/src/edu/ucsb/nceas/metacat/MetaCatServlet.java
Index: MetaCatServlet.java
===================================================================
RCS file: /cvs/metacat/src/edu/ucsb/nceas/metacat/MetaCatServlet.java,v
retrieving revision 1.231
retrieving revision 1.232
diff -u -r1.231 -r1.232
--- MetaCatServlet.java 31 Oct 2005 20:56:27 -0000 1.231
+++ MetaCatServlet.java 1 Nov 2005 22:09:28 -0000 1.232
@@ -7,8 +7,8 @@
* Release: @release@
*
* '$Author: sgarg $'
- * '$Date: 2005/10/31 20:56:27 $'
- * '$Revision: 1.231 $'
+ * '$Date: 2005/11/01 22:09:28 $'
+ * '$Revision: 1.232 $'
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -157,6 +157,8 @@
public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation";
+ public static final String NAMESPACEKEYWORD = "xmlns";
+
public static final String EML2KEYWORD = ":eml";
public static final String XMLFORMAT = "xml";
@@ -1712,31 +1714,34 @@
// set a dtd base validation parser
String rule = DocumentImpl.DTD;
documentWrapper = new DocumentImplWrapper(rule, validate);
- } else if (needSchemaValidation(xml)) {
- // for eml2
+ } else {
+
String namespace = findNamespace(xml);
- if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
- || namespace.compareTo(
- DocumentImpl.EML2_0_1NAMESPACE) == 0) {
- // set eml2 base validation parser
- String rule = DocumentImpl.EML200;
- // using emlparser to check id validation
- EMLParser parser = new EMLParser(doctext[0]);
- documentWrapper = new DocumentImplWrapper(rule, true);
- } else if (namespace.compareTo(
+
+ if (namespace != null) {
+ if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
+ || namespace.compareTo(
+ DocumentImpl.EML2_0_1NAMESPACE) == 0) {
+ // set eml2 base validation parser
+ String rule = DocumentImpl.EML200;
+ // using emlparser to check id validation
+ EMLParser parser = new EMLParser(doctext[0]);
+ documentWrapper = new DocumentImplWrapper(rule, true);
+ } else if (namespace.compareTo(
DocumentImpl.EML2_1_0NAMESPACE) == 0) {
- // set eml2 base validation parser
- String rule = DocumentImpl.EML210;
- // using emlparser to check id validation
- EMLParser parser = new EMLParser(doctext[0]);
- documentWrapper = new DocumentImplWrapper(rule, true);
- } else {
- // set schema base validation parser
- String rule = DocumentImpl.SCHEMA;
- documentWrapper = new DocumentImplWrapper(rule, true);
- }
- } else {
- documentWrapper = new DocumentImplWrapper("", false);
+ // set eml2 base validation parser
+ String rule = DocumentImpl.EML210;
+ // using emlparser to check id validation
+ EMLParser parser = new EMLParser(doctext[0]);
+ documentWrapper = new DocumentImplWrapper(rule, true);
+ } else {
+ // set schema base validation parser
+ String rule = DocumentImpl.SCHEMA;
+ documentWrapper = new DocumentImplWrapper(rule, true);
+ }
+ } else {
+ documentWrapper = new DocumentImplWrapper("", false);
+ }
}
String[] action = (String[]) params.get("action");
@@ -1868,32 +1873,6 @@
// END OF INSERT/UPDATE SECTION
/* check if the xml string contains key words to specify schema loocation */
- private boolean needSchemaValidation(StringReader xml) throws IOException
- {
- boolean needSchemaValidate = false;
- if (xml == null) {
- logMetacat.warn("Validation for schema is "
- + needSchemaValidate);
- return needSchemaValidate;
- }
- logMetacat.info("before get target line");
- String targetLine = getSchemaLine(xml);
- logMetacat.info("after get target line");
- // to see if the second line contain some keywords
- if (targetLine != null
- && (targetLine.indexOf(SCHEMALOCATIONKEYWORD) != -1 || targetLine
- .indexOf(NONAMESPACELOCATION) != -1)) {
- // if contains schema location key word, should be validate
- needSchemaValidate = true;
- }
-
- logMetacat.warn("Validation for schema is "
- + needSchemaValidate);
- return needSchemaValidate;
-
- }
-
- /* check if the xml string contains key words to specify schema loocation */
private String findNamespace(StringReader xml) throws IOException
{
String namespace = null;
@@ -1911,10 +1890,31 @@
if (targetLine != null) {
- int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD);
+ // find if the root element has prefix
+ String prefix = getPrefix(targetLine);
+ int startIndex = 0;
+
+ if(prefix != null)
+ {
+ // if prefix found then look for xmlns:prefix
+ // element to find the ns
+ String namespaceWithPrefix = NAMESPACEKEYWORD + ":" + prefix;
+ startIndex = targetLine.indexOf(namespaceWithPrefix);
+
+ // if xmlns:prefix not found, look
+ // for xmlns attribute to find the ns
+ if(startIndex == -1){
+ startIndex = targetLine.indexOf(NAMESPACEKEYWORD);
+ }
+ } else {
+ // if prefix not found then look for xmlns
+ // attribute to find the ns
+ startIndex = targetLine.indexOf(NAMESPACEKEYWORD);
+ }
+
int start = 1;
int end = 1;
- String schemaLocation = null;
+ String namespaceString = null;
int count = 0;
if (startIndex != -1) {
for (int i = startIndex; i < targetLine.length(); i++) {
@@ -1929,16 +1929,22 @@
break;
}
}
+ }
+ // else: xmlns not found. namespace = null will be returned
+
+ if(start < end){
+ namespaceString = targetLine.substring(start + 1, end);
}
- schemaLocation = targetLine.substring(start + 1, end);
- logMetacat.info("schemaLocation in xml is: "
- + schemaLocation);
- if (schemaLocation.indexOf(eml2_0_0NameSpace) != -1) {
+ logMetacat.info("namespace in xml is: "
+ + namespaceString);
+ if (namespaceString.indexOf(eml2_0_0NameSpace) != -1) {
namespace = eml2_0_0NameSpace;
- } else if (schemaLocation.indexOf(eml2_0_1NameSpace) != -1) {
+ } else if (namespaceString.indexOf(eml2_0_1NameSpace) != -1) {
namespace = eml2_0_1NameSpace;
- } else if (schemaLocation.indexOf(eml2_1_0NameSpace) != -1) {
+ } else if (namespaceString.indexOf(eml2_1_0NameSpace) != -1) {
namespace = eml2_1_0NameSpace;
+ } else {
+ namespace = namespaceString;
}
}
@@ -1998,6 +2004,20 @@
xml.reset();
return secondLine;
+ }
+
+ private String getPrefix(String schemaLine)
+ {
+ String prefix = null;
+ String rootElement = schemaLine.substring(0, schemaLine.indexOf(" "));
+ logMetacat.warn("rootElement:" + rootElement);
+
+ if(rootElement.indexOf(":") > 0){
+ prefix = rootElement.substring(rootElement.indexOf(":") + 1,
+ rootElement.length());
+ }
+ logMetacat.warn("prefix:" + prefix);
+ return prefix;
}
/**
More information about the Metacat-cvs
mailing list