Revision 1760
Added by Jing Tao over 21 years ago
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
1572 | 1572 |
} catch (NullPointerException npe) {} |
1573 | 1573 |
} |
1574 | 1574 |
|
1575 |
StringReader xml = null;
|
|
1575 |
StringReader xml = new StringReader(doctext[0]);
|
|
1576 | 1576 |
boolean validate = false; |
1577 | 1577 |
DocumentImplWrapper documentWrapper = null; |
1578 | 1578 |
try { |
1579 | 1579 |
// look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ... > |
1580 | 1580 |
// in order to decide whether to use validation parser |
1581 |
validate = needDTDValidation(doctext[0]);
|
|
1581 |
validate = needDTDValidation(xml);
|
|
1582 | 1582 |
if (validate) |
1583 | 1583 |
{ |
1584 | 1584 |
// set a dtd base validation parser |
1585 | 1585 |
String rule = DocumentImpl.DTD; |
1586 | 1586 |
documentWrapper = new DocumentImplWrapper(rule, validate); |
1587 | 1587 |
} |
1588 |
else if (needSchemaValidation(doctext[0]))
|
|
1588 |
else if (needSchemaValidation(xml))
|
|
1589 | 1589 |
{ |
1590 | 1590 |
// for eml2 |
1591 |
if (needEml2Validation(doctext[0]))
|
|
1591 |
if (needEml2Validation(xml))
|
|
1592 | 1592 |
{ |
1593 | 1593 |
// set eml2 base validation parser |
1594 | 1594 |
String rule = DocumentImpl.EML2; |
... | ... | |
1608 | 1608 |
documentWrapper = new DocumentImplWrapper("", false); |
1609 | 1609 |
} |
1610 | 1610 |
|
1611 |
xml = new StringReader(doctext[0]); |
|
1612 |
|
|
1613 | 1611 |
String[] action = (String[])params.get("action"); |
1614 | 1612 |
String[] docid = (String[])params.get("docid"); |
1615 | 1613 |
String newdocid = null; |
... | ... | |
1628 | 1626 |
getDBConnection("MetaCatServlet.handleInsertOrUpdateAction"); |
1629 | 1627 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
1630 | 1628 |
|
1631 |
|
|
1632 |
// write the document to the database |
|
1629 |
// write the document to the database |
|
1633 | 1630 |
try |
1634 | 1631 |
{ |
1635 | 1632 |
String accNumber = docid[0]; |
... | ... | |
1686 | 1683 |
* Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... > |
1687 | 1684 |
* in order to decide whether to use validation parser |
1688 | 1685 |
*/ |
1689 |
private static boolean needDTDValidation(String xmltext) throws IOException { |
|
1686 |
private static boolean needDTDValidation(StringReader xmlreader) throws |
|
1687 |
IOException |
|
1688 |
{ |
|
1690 | 1689 |
|
1691 |
StringReader xmlreader = new StringReader(xmltext); |
|
1690 |
|
|
1692 | 1691 |
StringBuffer cbuff = new StringBuffer(); |
1693 | 1692 |
java.util.Stack st = new java.util.Stack(); |
1694 | 1693 |
boolean validate = false; |
... | ... | |
1724 | 1723 |
} |
1725 | 1724 |
|
1726 | 1725 |
// close the stream |
1727 |
xmlreader.close();
|
|
1726 |
xmlreader.reset();
|
|
1728 | 1727 |
|
1729 | 1728 |
// check the stack whether it contains the keywords: |
1730 | 1729 |
// "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order |
... | ... | |
1743 | 1742 |
// END OF INSERT/UPDATE SECTION |
1744 | 1743 |
|
1745 | 1744 |
/* check if the xml string contains key words to specify schema loocation*/ |
1746 |
private boolean needSchemaValidation(String xml)
|
|
1745 |
private boolean needSchemaValidation(StringReader xml) throws IOException
|
|
1747 | 1746 |
{ |
1748 | 1747 |
boolean needSchemaValidate =false; |
1749 | 1748 |
if (xml == null) |
... | ... | |
1752 | 1751 |
needSchemaValidate, 10); |
1753 | 1752 |
return needSchemaValidate; |
1754 | 1753 |
} |
1754 |
System.out.println("before get target line"); |
|
1755 | 1755 |
String targetLine = getSchemaLine(xml); |
1756 |
System.out.println("before get target line"); |
|
1756 | 1757 |
// to see if the second line contain some keywords |
1757 | 1758 |
if (targetLine != null && (targetLine.indexOf(SCHEMALOCATIONKEYWORD) != -1|| |
1758 | 1759 |
targetLine.indexOf(NONAMESPACELOCATION) != -1 )) |
... | ... | |
1768 | 1769 |
} |
1769 | 1770 |
|
1770 | 1771 |
/* check if the xml string contains key words to specify schema loocation*/ |
1771 |
private boolean needEml2Validation(String xml)
|
|
1772 |
private boolean needEml2Validation(StringReader xml) throws IOException
|
|
1772 | 1773 |
{ |
1773 | 1774 |
boolean needEml2Validate =false; |
1774 |
String emlNameSpace = "=\""+DocumentImpl.EMLNAMESPACE+"\""; |
|
1775 |
String emlNameSpace =DocumentImpl.EMLNAMESPACE; |
|
1776 |
String schemaLocationContent = null; |
|
1775 | 1777 |
if (xml == null) |
1776 | 1778 |
{ |
1777 | 1779 |
MetaCatUtil.debugMessage("Validation for schema is " + |
... | ... | |
1780 | 1782 |
} |
1781 | 1783 |
String targetLine = getSchemaLine(xml); |
1782 | 1784 |
|
1783 |
if (targetLine != null && targetLine.indexOf(EML2KEYWORD) != -1 && |
|
1784 |
targetLine.indexOf(emlNameSpace) != -1) |
|
1785 |
if (targetLine != null) |
|
1785 | 1786 |
{ |
1786 |
// if contains schema location key word, should be validate |
|
1787 |
needEml2Validate = true; |
|
1787 |
|
|
1788 |
int startIndex = targetLine.indexOf(SCHEMALOCATIONKEYWORD); |
|
1789 |
int start = 1; |
|
1790 |
int end = 1; |
|
1791 |
String schemaLocation = null; |
|
1792 |
int count = 0; |
|
1793 |
if (startIndex != -1) |
|
1794 |
{ |
|
1795 |
for ( int i=startIndex; i<targetLine.length(); i++) |
|
1796 |
{ |
|
1797 |
if (targetLine.charAt(i) =='"') |
|
1798 |
{ |
|
1799 |
count ++; |
|
1800 |
} |
|
1801 |
if (targetLine.charAt(i) =='"' && count == 1) |
|
1802 |
{ |
|
1803 |
start = i; |
|
1804 |
} |
|
1805 |
if (targetLine.charAt(i) =='"' && count == 2) |
|
1806 |
{ |
|
1807 |
end = i; |
|
1808 |
break; |
|
1809 |
} |
|
1810 |
} |
|
1811 |
} |
|
1812 |
schemaLocation = targetLine.substring(start+1, end); |
|
1813 |
MetaCatUtil.debugMessage("schemaLocation in xml is: "+schemaLocation, 30); |
|
1814 |
if ( schemaLocation.indexOf(emlNameSpace) != -1) |
|
1815 |
{ |
|
1816 |
needEml2Validate = true; |
|
1817 |
} |
|
1788 | 1818 |
} |
1789 | 1819 |
|
1790 | 1820 |
MetaCatUtil.debugMessage("Validation for eml is " + |
... | ... | |
1793 | 1823 |
|
1794 | 1824 |
} |
1795 | 1825 |
|
1796 |
private String getSchemaLine(String xml)
|
|
1826 |
private String getSchemaLine(StringReader xml) throws IOException
|
|
1797 | 1827 |
{ |
1798 | 1828 |
// find the line |
1799 | 1829 |
String secondLine = null; |
... | ... | |
1801 | 1831 |
int endIndex = 0; |
1802 | 1832 |
int startIndex = 0; |
1803 | 1833 |
final int TARGETNUM = 2; |
1804 |
for (int i=0; i<xml.length(); i++) |
|
1834 |
StringBuffer buffer = new StringBuffer(); |
|
1835 |
boolean comment =false; |
|
1836 |
char thirdPreviousCharacter = '?'; |
|
1837 |
char secondPreviousCharacter ='?'; |
|
1838 |
char previousCharacter = '?'; |
|
1839 |
char currentCharacter = '?'; |
|
1840 |
|
|
1841 |
while ( (currentCharacter = (char) xml.read()) != -1) |
|
1805 | 1842 |
{ |
1806 |
// didn't count comment |
|
1807 |
if (xml.charAt(i) =='<' && xml.charAt(i+1) != '!') |
|
1843 |
//in a comment |
|
1844 |
if (currentCharacter =='-' && previousCharacter == '-' && |
|
1845 |
secondPreviousCharacter =='!' && thirdPreviousCharacter == '<') |
|
1808 | 1846 |
{ |
1847 |
comment = true; |
|
1848 |
} |
|
1849 |
//out of comment |
|
1850 |
if (comment && currentCharacter == '>' && previousCharacter == '-' && |
|
1851 |
secondPreviousCharacter =='-') |
|
1852 |
{ |
|
1853 |
comment = false; |
|
1854 |
} |
|
1855 |
|
|
1856 |
//this is not comment |
|
1857 |
if (currentCharacter !='!' && previousCharacter == '<' && !comment) |
|
1858 |
{ |
|
1809 | 1859 |
count ++; |
1810 |
//find start index |
|
1811 |
if (count == TARGETNUM) |
|
1812 |
{ |
|
1813 |
startIndex = i; |
|
1814 |
} |
|
1815 |
}//if |
|
1816 |
// find the end index |
|
1817 |
if (count== TARGETNUM && xml.charAt(i) =='>') |
|
1860 |
} |
|
1861 |
// get target line |
|
1862 |
if (count == TARGETNUM && currentCharacter !='>') |
|
1818 | 1863 |
{ |
1819 |
endIndex = i;
|
|
1820 |
break;
|
|
1821 |
}//if
|
|
1822 |
}//for
|
|
1823 |
// get the second line string
|
|
1824 |
MetaCatUtil.debugMessage("The start index for second line: "+startIndex, 25);
|
|
1825 |
MetaCatUtil.debugMessage("The end index for second line: "+endIndex, 25);
|
|
1826 |
if (startIndex != 0 && endIndex != 0)
|
|
1827 |
{
|
|
1828 |
secondLine = xml.substring(startIndex+1, endIndex); |
|
1829 |
|
|
1830 |
}//if
|
|
1864 |
buffer.append(currentCharacter);
|
|
1865 |
}
|
|
1866 |
if (count == TARGETNUM && currentCharacter == '>')
|
|
1867 |
{
|
|
1868 |
break;
|
|
1869 |
}
|
|
1870 |
thirdPreviousCharacter = secondPreviousCharacter;
|
|
1871 |
secondPreviousCharacter = previousCharacter;
|
|
1872 |
previousCharacter = currentCharacter;
|
|
1873 |
|
|
1874 |
} |
|
1875 |
secondLine = buffer.toString();
|
|
1831 | 1876 |
MetaCatUtil.debugMessage("the second line string is: "+secondLine, 25); |
1877 |
xml.reset(); |
|
1832 | 1878 |
return secondLine; |
1833 | 1879 |
} |
1834 | 1880 |
|
Also available in: Unified diff
Change string to stringreader to save memory.