You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Jean-Guillaume LALANNE <je...@coming.fr> on 2001/03/15 16:40:15 UTC
Access to the java schema object : SchemaGrammar ????
Hi,
I'd like to know if it is possible to get access to the java schema representation
(Schema Grammar object) by using the xerces parsers (SAX or DOM).
I actually want to use this java object for dynamically creating code, gui and other
stuff...
I don' t like to parse my schema file, and to traverse my schema DOM if already,
it exits a java representation of it...!!
I have tried with the following code to access the SchemaGrammar but I failed ...
the main of my testing class:
WHIPSAXParser parser = new WHIPSAXParser();
XMLValidator validator = parser.getValidator();
GrammarResolver gr = new GrammarResolverImpl();
validator.setGrammarResolver(gr);
try
{
validator.setSchemaValidationEnabled( true );
validator.setDynamicValidationEnabled( true );
validator.setNamespacesEnabled( true );
validator.setValidationEnabled( true );
}
catch( Exception e )
{
e.printStackTrace();
}
InputSource source = ...
try
{
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
parser.parse( source );
}
catch (SAXNotRecognizedException snre)
{
snre.printStackTrace();
}
catch (SAXNotSupportedException snse)
{
snse.printStackTrace();
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
catch( SAXException saxe )
{
saxe.printStackTrace();
}
String nsKey = null;
Enumeration enum = gr.nameSpaceKeys();
while( enum.hasMoreElements() )
{
nsKey = (String) enum.nextElement();
System.out.println( "name space key : " + nsKey );
}
And my parser wrapper class:
public class WHIPSAXParser extends SAXParser
{
public XMLValidator getValidator()
{
return fValidator;
}
}
I don't get anything from my XML files ... no namespaces ...
Where am I wrong ?
If someone knows how to get the schema grammar object, I very very interested ...!!!
Many thanks
Jean-Guillaume LALANNE
ps: another quick question : how to validate a DOM object against a schema (stream) ???
----- Original Message -----
From: "Umesh Subramanian" <Um...@msdw.com>
To: <xe...@xml.apache.org>
Sent: Thursday, March 15, 2001 3:45 PM
Subject: schema validator caching
> Hi:
> Is it possible (or planned in future releases of xerces) to cache the
> schema validator objects, once the schema is loaded? In my application,
> I expect to validate XML instances of the same schema many times over
> and would like to reduce the overhead of reading the schema file
> everytime.
> Thanks
> -Umesh
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
problems with encoding in XSLT
Posted by Maris Orbidans <sm...@apollo.lv>.
hello
I use version 2.0.0.0 of Xalan/Xerces, I have a problem that I can't use other character encodings in XSL
document but UTF-8. But I want to use cp-1257 and cp-1252 encoding.
If I change "encoding" in XSL header then the parser throws "unsupported encoding" exception. But
I have seen Xalan/Xerces ( some older version ) working with stylesheets in cp-1257 encoding.
Could you help me ?
thanx
Maris Orbidans
--------------------------------------------------------------------------------
// THESE METHODS THAT I WROTE THROW THE EXCEPTION
public void writeDocumentTo(Writer out) throws IOException
{
xmlDoc.write(out);
}
public void writeDocumentTo(Writer out, String encoding) throws IOException
{
xmlDoc.write(out, encoding);
}
public String writeDoc2Str() throws IOException
{
StringWriter writer = new StringWriter();
xmlDoc.write(writer);
return writer.toString();
}
public String transformToHtmlString (String stXslUri) throws Exception
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(stXslUri));
ByteArrayOutputStream theOutputStream = new ByteArrayOutputStream();
StreamResult theResult= new StreamResult(theOutputStream);
StringWriter swXmlDoc = new StringWriter();
writeDocumentTo(swXmlDoc);
StringReader srXmlDoc = new StringReader(swXmlDoc.toString());
transformer.transform(new StreamSource(srXmlDoc),theResult);
return (theOutputStream.toString());
}
public String transformToHtmlString (String stXslUri,String encoding) throws Exception
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(stXslUri));
ByteArrayOutputStream theOutputStream = new ByteArrayOutputStream();
StreamResult theResult= new StreamResult(theOutputStream);
StringWriter swXmlDoc = new StringWriter();
writeDocumentTo(swXmlDoc,encoding);
StringReader srXmlDoc = new StringReader(swXmlDoc.toString());
transformer.transform(new StreamSource(srXmlDoc),theResult);
return (theOutputStream.toString(encoding));
}
-----------------------------------------
[14/03/2001 00:48:34:010 GMT+02:00] BooksDb: error in login :
javax.xml.transform.TransformerConfigurationException:
The encoding "windows-1257" is not supported.
---------------------------------------
<main_frame_2 username="abc">
<search />
</main_frame_2>
-----------------------
<?xml version="1.0" encoding="cp-1257"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=cp-1257"/>
<meta http-equiv="expires" content="0"/>
<title>Testi ?Zeogr?üfij?ü (metodisks l?½dzeklis). I.?Cbols</title>
</head>
<body leftmargin="0" bgcolor="#e2e1c2" rightmargin="0" topmargin="0" bottommargin="0" link="#0000aa" alink="#0000aa" vlink="#0000aa">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
-----------------------------------------
// THESE FUNCTIONS WORK WELL WITH NON UTF-8 XSL FILES !!!
// ( I DIDNT WRITE THEM )
// BUT THEY USE SOME OLD VERSION ( PERHAPS 1.0.0) OF XALAN
// I CANT FIGURE OUT WHY
// I HAVE LATEST VERSION OF XALAN/XERCES ONLY
/**
Applies XSL to XML document and convert it to HTML in specified encoding
@param url XSL file name
@param encoding encoding of the result HTML file
*/
public Writer applyXSL(String uri, String encoding) throws SAXException
{
StringWriter writer = new StringWriter();
StringWriter writer2 = new StringWriter();
this.writeDocumentTo(writer, encoding);
StringReader reader = new StringReader(writer.toString());
InputStream xsl = getClass().getResourceAsStream(uri);
XSLTInputSource theXSLTInputSource1 = new XSLTInputSource(reader);
file://XSLTInputSource theXSLTInputSource2 = new XSLTInputSource(uri);
XSLTInputSource theXSLTInputSource2 = new XSLTInputSource(xsl);
XSLTResultTarget theXSLTResultTarget = new XSLTResultTarget(writer2);
XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
processor.process(theXSLTInputSource1,
theXSLTInputSource2,
theXSLTResultTarget);
return (Writer) writer2;
}
/** Applies XSL to XML document ,convert it to HTML and returns string
@param url XSL file name
*/
public String applyXSL2String(String uri) throws SAXException
{
String html = "";
try
{
html = stringFilter(this.applyXSL(uri).toString());
}
catch (NumberFormatException e)
{
throw new SAXException(e);
}
return html;
}
/** Applies XSL to XML document ,convert it to HTML and returns string in specified encoding
@param url XSL file name
@param encoding - encoding of HTML
*/
public String applyXSL2String(String uri, String encoding) throws SAXException
{
String html = "";
try
{
html = stringFilter(this.applyXSL(uri, encoding).toString());
}
catch (NumberFormatException e)
{
throw new SAXException(e);
}
return html;
}
/** Is used for filtering such expressions like х in HTML output.Converts such
expressions back to the character representation.
@param in input string to filter
*/
protected static String stringFilter(String in) throws NumberFormatException
{
if(in != null && in.trim().length() > 0)
{
StringBuffer out = new StringBuffer();
String tempStr;
Character symbol;
char tempChar1;
char tempChar2;
char tempChar3;
int i = 0;
int j;
int inLength = in.length();
do
{
tempChar1 = in.charAt(i);
if (tempChar1 == '&')
{
i++;
tempChar2 = in.charAt(i);
if (tempChar2 == '#')
{
i++;
tempChar3 = in.charAt(i);
if (tempChar3 == 'x')
{
i++;
j = 0;
tempStr = "";
while(in.charAt(i) != ';')
{
tempStr = tempStr + new Character(in.charAt(i)).toString();
j++;
if (j > 3)
{
out.append(tempChar1);
out.append(tempChar2);
out.append(tempChar3);
out.append(tempStr);
tempStr = "";
break;
}
i++;
}
if (tempStr.length() > 0)
{
symbol = new Character((char)Integer.parseInt(tempStr,16));
out.append(symbol.charValue());
}
}
else
{
System.out.println("IN" + tempChar3);
if (Character.isDigit(tempChar3))
{
i++;
j = 0;
tempStr = new Character(tempChar3).toString();
while(in.charAt(i) != ';')
{
tempStr = tempStr + new Character(in.charAt(i)).toString();
j++;
if ((j > 4) || (!Character.isDigit(in.charAt(i))))
{
out.append(tempChar1);
out.append(tempChar2);
out.append(tempStr);
tempStr = "";
break;
}
i++;
}
if (tempStr.length() > 0)
{
System.out.println("OUT" + tempStr);
symbol = new Character((char)Integer.parseInt(tempStr,10));
out.append(symbol.charValue());
}
}
else
{
out.append(tempChar1);
out.append(tempChar2);
out.append(tempChar3);
}
}
}
else
{
out.append(tempChar1);
out.append(tempChar2);
}
}
else
{
out.append(tempChar1);
}
i++;
}
while (i != inLength);
return new String(out);
}
else return null;
}
------------------------------------------
--------------------------------------------------------------------------------