Revision 2711
Added by sgarg about 19 years ago
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
157 | 157 |
|
158 | 158 |
public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation"; |
159 | 159 |
|
160 |
public static final String NAMESPACEKEYWORD = "xmlns"; |
|
161 |
|
|
160 | 162 |
public static final String EML2KEYWORD = ":eml"; |
161 | 163 |
|
162 | 164 |
public static final String XMLFORMAT = "xml"; |
... | ... | |
1712 | 1714 |
// set a dtd base validation parser |
1713 | 1715 |
String rule = DocumentImpl.DTD; |
1714 | 1716 |
documentWrapper = new DocumentImplWrapper(rule, validate); |
1715 |
} else if (needSchemaValidation(xml)) {
|
|
1716 |
// for eml2 |
|
1717 |
} else { |
|
1718 |
|
|
1717 | 1719 |
String namespace = findNamespace(xml); |
1718 |
if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0 |
|
1719 |
|| namespace.compareTo( |
|
1720 |
DocumentImpl.EML2_0_1NAMESPACE) == 0) { |
|
1721 |
// set eml2 base validation parser |
|
1722 |
String rule = DocumentImpl.EML200; |
|
1723 |
// using emlparser to check id validation |
|
1724 |
EMLParser parser = new EMLParser(doctext[0]); |
|
1725 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1726 |
} else if (namespace.compareTo( |
|
1720 |
|
|
1721 |
if (namespace != null) { |
|
1722 |
if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0 |
|
1723 |
|| namespace.compareTo( |
|
1724 |
DocumentImpl.EML2_0_1NAMESPACE) == 0) { |
|
1725 |
// set eml2 base validation parser |
|
1726 |
String rule = DocumentImpl.EML200; |
|
1727 |
// using emlparser to check id validation |
|
1728 |
EMLParser parser = new EMLParser(doctext[0]); |
|
1729 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1730 |
} else if (namespace.compareTo( |
|
1727 | 1731 |
DocumentImpl.EML2_1_0NAMESPACE) == 0) { |
1728 |
// set eml2 base validation parser |
|
1729 |
String rule = DocumentImpl.EML210; |
|
1730 |
// using emlparser to check id validation |
|
1731 |
EMLParser parser = new EMLParser(doctext[0]); |
|
1732 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1733 |
} else { |
|
1734 |
// set schema base validation parser |
|
1735 |
String rule = DocumentImpl.SCHEMA; |
|
1736 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1737 |
} |
|
1738 |
} else { |
|
1739 |
documentWrapper = new DocumentImplWrapper("", false); |
|
1732 |
// set eml2 base validation parser |
|
1733 |
String rule = DocumentImpl.EML210; |
|
1734 |
// using emlparser to check id validation |
|
1735 |
EMLParser parser = new EMLParser(doctext[0]); |
|
1736 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1737 |
} else { |
|
1738 |
// set schema base validation parser |
|
1739 |
String rule = DocumentImpl.SCHEMA; |
|
1740 |
documentWrapper = new DocumentImplWrapper(rule, true); |
|
1741 |
} |
|
1742 |
} else { |
|
1743 |
documentWrapper = new DocumentImplWrapper("", false); |
|
1744 |
} |
|
1740 | 1745 |
} |
1741 | 1746 |
|
1742 | 1747 |
String[] action = (String[]) params.get("action"); |
... | ... | |
1868 | 1873 |
// END OF INSERT/UPDATE SECTION |
1869 | 1874 |
|
1870 | 1875 |
/* check if the xml string contains key words to specify schema loocation */ |
1871 |
private boolean needSchemaValidation(StringReader xml) throws IOException |
|
1872 |
{ |
|
1873 |
boolean needSchemaValidate = false; |
|
1874 |
if (xml == null) { |
|
1875 |
logMetacat.warn("Validation for schema is " |
|
1876 |
+ needSchemaValidate); |
|
1877 |
return needSchemaValidate; |
|
1878 |
} |
|
1879 |
logMetacat.info("before get target line"); |
|
1880 |
String targetLine = getSchemaLine(xml); |
|
1881 |
logMetacat.info("after get target line"); |
|
1882 |
// to see if the second line contain some keywords |
|
1883 |
if (targetLine != null |
|
1884 |
&& (targetLine.indexOf(SCHEMALOCATIONKEYWORD) != -1 || targetLine |
|
1885 |
.indexOf(NONAMESPACELOCATION) != -1)) { |
|
1886 |
// if contains schema location key word, should be validate |
|
1887 |
needSchemaValidate = true; |
|
1888 |
} |
|
1889 |
|
|
1890 |
logMetacat.warn("Validation for schema is " |
|
1891 |
+ needSchemaValidate); |
|
1892 |
return needSchemaValidate; |
|
1893 |
|
|
1894 |
} |
|
1895 |
|
|
1896 |
/* check if the xml string contains key words to specify schema loocation */ |
|
1897 | 1876 |
private String findNamespace(StringReader xml) throws IOException |
1898 | 1877 |
{ |
1899 | 1878 |
String namespace = null; |
... | ... | |
1911 | 1890 |
|
1912 | 1891 |
if (targetLine != null) { |
1913 | 1892 |
|
1914 |
int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD); |
|
1893 |
// find if the root element has prefix |
|
1894 |
String prefix = getPrefix(targetLine); |
|
1895 |
int startIndex = 0; |
|
1896 |
|
|
1897 |
if(prefix != null) |
|
1898 |
{ |
|
1899 |
// if prefix found then look for xmlns:prefix |
|
1900 |
// element to find the ns |
|
1901 |
String namespaceWithPrefix = NAMESPACEKEYWORD + ":" + prefix; |
|
1902 |
startIndex = targetLine.indexOf(namespaceWithPrefix); |
|
1903 |
|
|
1904 |
// if xmlns:prefix not found, look |
|
1905 |
// for xmlns attribute to find the ns |
|
1906 |
if(startIndex == -1){ |
|
1907 |
startIndex = targetLine.indexOf(NAMESPACEKEYWORD); |
|
1908 |
} |
|
1909 |
} else { |
|
1910 |
// if prefix not found then look for xmlns |
|
1911 |
// attribute to find the ns |
|
1912 |
startIndex = targetLine.indexOf(NAMESPACEKEYWORD); |
|
1913 |
} |
|
1914 |
|
|
1915 | 1915 |
int start = 1; |
1916 | 1916 |
int end = 1; |
1917 |
String schemaLocation = null;
|
|
1917 |
String namespaceString = null;
|
|
1918 | 1918 |
int count = 0; |
1919 | 1919 |
if (startIndex != -1) { |
1920 | 1920 |
for (int i = startIndex; i < targetLine.length(); i++) { |
... | ... | |
1929 | 1929 |
break; |
1930 | 1930 |
} |
1931 | 1931 |
} |
1932 |
} |
|
1933 |
// else: xmlns not found. namespace = null will be returned |
|
1934 |
|
|
1935 |
if(start < end){ |
|
1936 |
namespaceString = targetLine.substring(start + 1, end); |
|
1932 | 1937 |
} |
1933 |
schemaLocation = targetLine.substring(start + 1, end); |
|
1934 |
logMetacat.info("schemaLocation in xml is: " |
|
1935 |
+ schemaLocation); |
|
1936 |
if (schemaLocation.indexOf(eml2_0_0NameSpace) != -1) { |
|
1938 |
logMetacat.info("namespace in xml is: " |
|
1939 |
+ namespaceString); |
|
1940 |
if (namespaceString.indexOf(eml2_0_0NameSpace) != -1) { |
|
1937 | 1941 |
namespace = eml2_0_0NameSpace; |
1938 |
} else if (schemaLocation.indexOf(eml2_0_1NameSpace) != -1) {
|
|
1942 |
} else if (namespaceString.indexOf(eml2_0_1NameSpace) != -1) {
|
|
1939 | 1943 |
namespace = eml2_0_1NameSpace; |
1940 |
} else if (schemaLocation.indexOf(eml2_1_0NameSpace) != -1) {
|
|
1944 |
} else if (namespaceString.indexOf(eml2_1_0NameSpace) != -1) {
|
|
1941 | 1945 |
namespace = eml2_1_0NameSpace; |
1946 |
} else { |
|
1947 |
namespace = namespaceString; |
|
1942 | 1948 |
} |
1943 | 1949 |
} |
1944 | 1950 |
|
... | ... | |
2000 | 2006 |
return secondLine; |
2001 | 2007 |
} |
2002 | 2008 |
|
2009 |
private String getPrefix(String schemaLine) |
|
2010 |
{ |
|
2011 |
String prefix = null; |
|
2012 |
String rootElement = schemaLine.substring(0, schemaLine.indexOf(" ")); |
|
2013 |
logMetacat.warn("rootElement:" + rootElement); |
|
2014 |
|
|
2015 |
if(rootElement.indexOf(":") > 0){ |
|
2016 |
prefix = rootElement.substring(rootElement.indexOf(":") + 1, |
|
2017 |
rootElement.length()); |
|
2018 |
} |
|
2019 |
logMetacat.warn("prefix:" + prefix); |
|
2020 |
return prefix; |
|
2021 |
} |
|
2022 |
|
|
2003 | 2023 |
/** |
2004 | 2024 |
* Handle the database delete request and delete an XML document from the |
2005 | 2025 |
* database connection |
Also available in: Unified diff
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.