Показаны сообщения с ярлыком Java. Показать все сообщения
Показаны сообщения с ярлыком Java. Показать все сообщения
, , , , ,

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;
/
Читать полностью

, , , ,

Java properties in Oracle DB

четверг, 28 октября 2010 г. 1 коммент.

Sometimes you need to know Java-machine settings for the proper coding.

To do this, use the function dbms_java.get_ojvm_property.


SQL> set pagesize 1000
SQL>
SQL> with java_properties as
2 (
3 select 'java.version' as property, q'~Java Runtime Environment version~' as description from dual union all
4 select 'java.vendor' as property, q'~Java Runtime Environment vendor~' as description from dual union all
5 select 'java.vendor.url' as property, q'~Java vendor URL~' as description from dual union all
6 select 'java.home' as property, q'~Java installation directory~' as description from dual union all
7 select 'java.vm.specification.version' as property, q'~Java Virtual Machine specification version~' as description from dual union all
8 select 'java.vm.specification.vendor' as property, q'~Java Virtual Machine specification vendor~' as description from dual union all
9 select 'java.vm.specification.name' as property, q'~Java Virtual Machine specification name~' as description from dual union all
10 select 'java.vm.version' as property, q'~Java Virtual Machine implementation version~' as description from dual union all
11 select 'java.vm.vendor' as property, q'~Java Virtual Machine implementation vendor~' as description from dual union all
12 select 'java.vm.name' as property, q'~Java Virtual Machine implementation name~' as description from dual union all
13 select 'java.specification.version' as property, q'~Java Runtime Environment specification version~' as description from dual union all
14 select 'java.specification.vendor' as property, q'~Java Runtime Environment specification vendor~' as description from dual union all
15 select 'java.specification.name' as property, q'~Java Runtime Environment specification name~' as description from dual union all
16 select 'java.class.version' as property, q'~Java class format version number~' as description from dual union all
17 select 'java.class.path' as property, q'~Java class path~' as description from dual union all
18 select 'java.library.path' as property, q'~List of paths to search when loading libraries~' as description from dual union all
19 select 'java.io.tmpdir' as property, q'~Default temp file path~' as description from dual union all
20 select 'java.compiler' as property, q'~Name of JIT compiler to use~' as description from dual union all
21 select 'java.ext.dirs' as property, q'~Path of extension directory or directories~' as description from dual union all
22 select 'os.name' as property, q'~Operating system name~' as description from dual union all
23 select 'os.arch' as property, q'~Operating system architecture~' as description from dual union all
24 select 'os.version' as property, q'~Operating system version~' as description from dual union all
25 select 'file.separator' as property, q'~File separator ("/" on UNIX)~' as description from dual union all
26 select 'path.separator' as property, q'~Path separator (":" on UNIX)~' as description from dual union all
27 select 'line.separator' as property, q'~Line separator ("\n" on UNIX)~' as description from dual union all
28 select 'user.name' as property, q'~User's account name~' as description from dual union all
29 select 'user.home' as property, q'~User's home directory~' as description from dual union all
30 select 'user.dir' as property, q'~User's current working directory~' as description from dual
31 )
32 select dbms_java.get_ojvm_property(java_properties.property) as value, java_properties.property, java_properties.description
33 from java_properties
34 /

VALUE PROPERTY DESCRIPTION
-------------------------------------------------------------------------------- ----------------------------- ----------------------------------------------
1.5.0_10 java.version Java Runtime Environment version
Oracle Corporation java.vendor Java Runtime Environment vendor
http://www.oracle.com/java/ java.vendor.url Java vendor URL
D:\ORACLE\DB112\JAVAVM\ java.home Java installation directory
1.0 java.vm.specification.version Java Virtual Machine specification version
Sun Microsystems Inc. java.vm.specification.vendor Java Virtual Machine specification vendor
Java Virtual Machine Specification java.vm.specification.name Java Virtual Machine specification name
1.5.0_01 java.vm.version Java Virtual Machine implementation version
Oracle Corporation java.vm.vendor Java Virtual Machine implementation vendor
JServer VM java.vm.name Java Virtual Machine implementation name
1.5 java.specification.version Java Runtime Environment specification version
Sun Microsystems Inc. java.specification.vendor Java Runtime Environment specification vendor
Java Platform API Specification java.specification.name Java Runtime Environment specification name
48.0 java.class.version Java class format version number
java.class.path Java class path
d:\oracle\db112\bin;.;C:\WINDOWS\system32;C:\WINDOWS;D:\oracle\db112\bin;C:\WIND java.library.path List of paths to search when loading libraries
C:\WINDOWS\TEMP\ java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
Windows XP os.name Operating system name
x86 os.arch Operating system architecture
5.1 os.version Operating system version
\ file.separator File separator ("/" on UNIX)
; path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
D:\ORACLE\DB112 user.dir User's current working directory

28 rows selected

SQL>
Читать полностью