Custom XSLT with Oracle XDK library in ORACLE DB
понедельник, 1 ноября 2010 г.
0
коммент.
Another transformer based on the Oracle XDK. The transformer provides several useful features not included in XSLT 1.0.
About the new features I'll discuss in the next post.
Java Source:
create or replace and compile java source named ora_20000_xmlutility as
package ru.ora_20000.xml;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.v2.XSLException;
import oracle.xml.parser.v2.XSLProcessor;
import oracle.xml.parser.v2.XSLStylesheet;
import oracle.xml.parser.v2.XMLParseException;
import oracle.sql.CLOB;
public class xmlutility {
private static XMLDocument parseDocument(String xmlText) throws Exception
{
DOMParser parser = new DOMParser();
parser.setPreserveWhitespace(true);
StringReader sr_xml = new StringReader(xmlText);
try {
parser.parse(sr_xml);
} catch (XMLParseException e) {
throw new Exception("parseDocument ERROR-XMLParseException: " + e.getMessage());
} catch (SAXException e) {
throw new Exception("parseDocument ERROR-SAXException: " + e.getMessage());
} catch (IOException e) {
throw new Exception("parseDocument ERROR-IOException: " + e.getMessage());
}
return parser.getDocument();
}
private static String processXSL(XMLDocument xmlDocument, XMLDocument xsltDocument) throws Exception
{
XSLProcessor processor = new XSLProcessor();
XSLStylesheet xslStylesheet;
StringWriter strWriter;
try {
xslStylesheet = processor.newXSLStylesheet(xsltDocument);
processor.showWarnings(true);
processor.setErrorStream(System.err);
strWriter = new StringWriter();
processor.processXSL(xslStylesheet, xmlDocument, new PrintWriter(strWriter));
} catch (XSLException e) {
String message = "";
for(int i=0; i < e.getNumMessages(); i++)
{
message = message + "Message=" + e.getMessage(i) + ", Line=" + e.getLineNumber(i) + ", Row=" + e.getLineNumber(i) + "URI=" + e.getSystemId(i);
}
throw new Exception("processXSL ERROR-XSLException: " + message);
} catch (IOException e) {
throw new Exception("processXSL ERROR-IOException: " + e.getMessage());
}
return strWriter.getBuffer().toString();
}
public static String getStringFromClob(CLOB clob) throws Exception
{
Reader reader;
StringBuffer stringBuffer;
try
{
reader = clob.getCharacterStream();
stringBuffer = new StringBuffer();
int numchars;
char[] buffer = new char[clob.getChunkSize()];
while ((numchars = reader.read(buffer, 0, clob.getChunkSize())) != -1)
{
stringBuffer.append(buffer, 0, numchars);
}
reader.close();
} catch (SQLException e) {
throw new Exception("getStringFromClob ERROR-SQLException: " + e.getMessage());
} catch (IOException e) {
throw new Exception("getStringFromClob ERROR-IOException: " + e.getMessage());
}
return stringBuffer.toString();
}
public static CLOB getClobFromString(String string) throws Exception
{
CLOB clob = null;
Writer writer = null;
try {
Connection connection = DriverManager.getConnection("jdbc:default:connection:");
clob = CLOB.createTemporary(connection, true, CLOB.DURATION_SESSION);
writer = clob.setCharacterStream(1L);
if (string != null) { writer.write(string); }
writer.flush();
writer.close();
} catch (SQLException e) {
throw new Exception("getClobFromString ERROR-SQLException: " + e.getMessage());
} catch (IOException e) {
throw new Exception("getClobFromString ERROR-IOException: " + e.getMessage());
} finally {
if (writer != null) { writer.close(); }
}
return clob;
}
public static String transformXmlString(String xmlText, String xslText) throws Exception
{
if (xmlText == null || xslText == null)
{
return null;
}
XMLDocument xmlDoc = parseDocument(xmlText);
XMLDocument xslDoc = parseDocument(xslText);
return processXSL(xmlDoc, xslDoc);
}
public static CLOB transformXmlClob(CLOB xmlClob, CLOB xslClob) throws Exception
{
if (xmlClob == null || xslClob == null)
{
return null;
}
String xmlText = getStringFromClob(xmlClob);
String xslText = getStringFromClob(xslClob);
String xmlResult = transformXmlString(xmlText, xslText);
return getClobFromString(xmlResult);
}
}
PL\SQL wrapper:
create or replace package ora_20000_xmlutility isЧитать полностью
function XMLTransformClob( xmlText clob
,xslText clob
) return clob;
function XMLTransform( xmlText varchar2
,xslText varchar2
) return varchar2;
end ora_20000_xmlutility;
/
create or replace package body ora_20000_xmlutility is
function XMLTransformClob( xmlText clob
,xslText clob
) return clob
is
language java name 'ru.ora_20000.xml.xmlutility.transformXmlClob(oracle.sql.CLOB, oracle.sql.CLOB) return oracle.sql.CLOB';
function XMLTransform( xmlText varchar2
,xslText varchar2
) return varchar2
is
language java name 'ru.ora_20000.xml.xmlutility.transformXmlString(java.lang.String, java.lang.String) return java.lang.String';
end ora_20000_xmlutility;
/
Мой список блогов