Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCEJWSD
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Web Services Certification (OCEJWSD)
Question about the xslt example in Ivan's study guide
Umesh Balasubramaniam
Greenhorn
Posts: 3
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am using the May 22, 2010 version of the study guide and I have a question about the xslt example code on page 303. Here is the code in the document:
/** * Transforms a key-value file to XML using XSLT transformation with * a custom-written parser. * * @see KeyValueFileReader * @throws Exception If error occurs reading file. */ public void doTransformKeyPropFile2XML() throws Exception { /* Specify from which file to read key-value data. */ FileReader theKeyValueFileReader = new FileReader(KEY_VALUE_FILE_NAME); /* Buffer the data read from the key-value file. */ BufferedReader theKeyValueBufReader = new BufferedReader(theKeyValueFileReader); /* * Create an input source for the key-value file. * The input source is later used by a SAXSource object to * read the data of the key-value file. */ InputSource theKeyValueInputSource = new InputSource(theKeyValueBufReader); /* * Create a source that reads data from the input source created * above and parses it using our key-value file parser. */ KeyValueFileReader theKeyValueFileParser = new KeyValueFileReader(); SAXSource theSAXSource = new SAXSource(theKeyValueFileParser, theKeyValueInputSource); /* Get the XSLT transformer factory and create a new transformer. */ TransformerFactory theXFormFactory = TransformerFactory.newInstance(); Transformer theXFormer = theXFormFactory.newTransformer(); /* Configure transformer to produce indented output. */ theXFormer.setOutputProperty(OutputKeys.INDENT, "yes"); /* * Transformer is to output the result of the transformation to * the console. */ StreamResult theResult = new StreamResult(System.out); /* Do the transform. */ theXFormer.transform(theSAXSource, theResult); }
My question is where is KeyValueFileReader defined?
Thanks in advance,
Umesh
Ivan Krizsan
Ranch Hand
Posts: 2198
1
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi!
Ooops, I indeed forgot to include it in the study notes! Strange that no one else complained until now.
Here it is:
package com.ivan.jaxpexamples; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.DTDHandler; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.AttributesImpl; /** * This class implements a reader that reads a key-value file and * produces an XML representation of its contents. * * @author Ivan A Krizsan */ public class KeyValueFileReader implements XMLReader { /* Constant(s): */ private final static String ROOT_ELEMENT_NAME = "person"; private final static String INDENT = "\n "; private final static String NAMESPACE_URI = "http://www.ivan.com/somenamespace"; /* Instance variable(s): */ private ContentHandler mContentHandler; private final Attributes mAttributes = new AttributesImpl(); /* (non-Javadoc) * @see org.xml.sax.XMLReader#getContentHandler() */ public ContentHandler getContentHandler() { return mContentHandler; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#getDTDHandler() */ public DTDHandler getDTDHandler() { return null; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#getEntityResolver() */ public EntityResolver getEntityResolver() { return null; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#getErrorHandler() */ public ErrorHandler getErrorHandler() { return null; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#getFeature(java.lang.String) */ public boolean getFeature(final String inName) throws SAXNotRecognizedException, SAXNotSupportedException { return false; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#getProperty(java.lang.String) */ public Object getProperty(final String inName) throws SAXNotRecognizedException, SAXNotSupportedException { return null; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource) */ public void parse(final InputSource inInputSource) throws IOException, SAXException { final Reader theCharReader = inInputSource.getCharacterStream(); final BufferedReader theBufferedReader = new BufferedReader(theCharReader); String theFileLine; /* Cannot parse if we do not have a content handler. */ if (mContentHandler == null) { throw new SAXException("No content handler!"); } /* * Tell the content handler that a new document is started * and what root element it has. */ mContentHandler.startDocument(); mContentHandler.startElement(NAMESPACE_URI, ROOT_ELEMENT_NAME, ROOT_ELEMENT_NAME, mAttributes); /* Read lines with data and parse them. */ theFileLine = theBufferedReader.readLine(); while ((null != theFileLine) && (theFileLine.length() != 0)) { String theStrings[]; String theKey; String theValue; theStrings = theFileLine.split(":"); theKey = theStrings[0].trim(); theValue = theStrings[1].trim(); /* Add C/R to improve readability of the resulting document. */ mContentHandler.ignorableWhitespace(INDENT.toCharArray(), 0, INDENT .length()); /* * Create an element which name is the key and which * contents is the value. */ mContentHandler.startElement(NAMESPACE_URI, theKey, theKey, mAttributes); mContentHandler.characters(theValue.toCharArray(), 0, theValue .length()); mContentHandler.endElement(NAMESPACE_URI, theKey, theKey); theFileLine = theBufferedReader.readLine(); } /* * Tell content handler that the root element has ended and * that the document also has ended. */ mContentHandler.endElement(NAMESPACE_URI, ROOT_ELEMENT_NAME, ROOT_ELEMENT_NAME); mContentHandler.endDocument(); } /* (non-Javadoc) * @see org.xml.sax.XMLReader#parse(java.lang.String) */ public void parse(final String inSystemId) throws IOException, SAXException { } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setContentHandler(org.xml.sax.ContentHandler) */ public void setContentHandler(final ContentHandler inContentHandler) { mContentHandler = inContentHandler; } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setDTDHandler(org.xml.sax.DTDHandler) */ public void setDTDHandler(final DTDHandler inHandler) { } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setEntityResolver(org.xml.sax.EntityResolver) */ public void setEntityResolver(final EntityResolver inResolver) { } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setErrorHandler(org.xml.sax.ErrorHandler) */ public void setErrorHandler(final ErrorHandler inHandler) { } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean) */ public void setFeature(final String inName, final boolean inValue) throws SAXNotRecognizedException, SAXNotSupportedException { } /* (non-Javadoc) * @see org.xml.sax.XMLReader#setProperty(java.lang.String, java.lang.Object) */ public void setProperty(final String inName, final Object inValue) throws SAXNotRecognizedException, SAXNotSupportedException { } }
Best wishes!
My free books and tutorials:
http://www.slideshare.net/krizsan
We're all out of roofs. But we still have tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
XSL-FO Multiple Page Sequences
Convert SOAP response to SOAP XmL string using JAX WS
XML Encoding problem
SOAPMessage.writeTo() - No line breaks
generic parser
More...