Help with using JAXP in storing Properties in XML
843834Feb 10 2003 — edited Feb 11 2003I am trying to store and retreive Properties in XML in using the newwer JAXP API.
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.*;
import java.util.*;
public class XMLProperties extends Properties {
public String PARSER_P = "org.xml.sax.XMLReader";
public boolean debug = false;
abstract class XMLParser implements ContentHandler {
final int IN_NOTHING = 0;
final int IN_DOCUMENT = 1;
final int IN_KEY = 2;
int state = IN_NOTHING;
String key;
StringBuffer value;
XMLReader parser ;
parser(InputSource in)
throws IOException, SAXException
{
state = IN_NOTHING;
value = new StringBuffer();
try {
parser = getParser();
parser.setDocumentLocator(this);
} catch (Exception e) {
e.printStackTrace();
throw new SAXException("can't create parser ");
}
parser.parse(new inputStream (in));
}
public void startElement(String name, Attributes atts)
throws SAXException
{
if (state == IN_NOTHING) {
if (name.equals("properties")) {
state = IN_DOCUMENT;
} else {
throw new SAXException("attempt to find root properties");
}
} else if (state == IN_DOCUMENT) {
if (name.equals("key")) {
state = IN_KEY;
key = atts.getValue("name");
if (key == null) {
throw new SAXException("no name for key "+atts);
}
} else {
throw new SAXException("attempt to find keys");
}
} else {
throw new SAXException("invalid element "+name);
}
}
public void endElement(String name)
throws SAXException
{
if (state == IN_KEY) {
setProperty(key, value.toString());
if (debug) {
System.out.print("<key name=\""+key+"\">");
System.out.println(value.toString()+"</key>\n");
}
state = IN_DOCUMENT;
name = null;
value = new StringBuffer();
} else if (state == IN_DOCUMENT) {
state = IN_NOTHING;
}
}
public void characters(char ch[], int start, int length)
throws SAXException
{
if (state == IN_KEY) {
compute(ch, start, length);
}
}
public void ignorableWhitespace(char ch[], int start, int length)
throws SAXException
{
// nothing to do
}
public void startDocument()
throws SAXException
{
// nothing to do
}
public void endDocument()
throws SAXException
{
// nothing to do
}
public void processingInstruction(String target, String data)
throws SAXException
{
// nothing to do
}
public void setDocumentLocator(Locator locator) {
// nothing to do
}
private void compute(char[] ch, int start, int length) {
int st = start;
int len = length-1;
while (st < length
&& ((ch[st] == '\n') || (ch[st] == '\t') || (ch[st] == ' ')
|| (ch[st] == '\r'))) {
st++;
}
while (len > 0
&& ((ch[len] == '\n')
|| (ch[len] == '\t')
|| (ch[len] == ' ')
|| (ch[len] == '\r'))) {
len--;
}
while (st <= len) {
value.append(ch[st]);
st++;
}
}
} //XMLParser
private Class parser_class = null;
/**
* Reads a property list from an input stream.
* @param in the input stream.
* @exception IOException if an error occurred when reading from the
* input stream.
* @since JDK1.0
*/
public synchronized void load(InputStream in)
throws IOException
{
XMLParser p = null;
try {
p = new XMLParser(in);
} catch (SAXException e) {
throw new IOException(e.getMessage());
}
}
public synchronized void load(File file)
throws IOException
{
InputStream in = new BufferedInputStream(new FileInputStream(file));
XMLParser p = null;
try {
p = new XMLParser(in);
} catch (SAXException e) {
try {
in = new BufferedInputStream(new FileInputStream(file));
super.load(in);
in.close();
} catch (IOException ex) {
throw new IOException(e.getMessage());
}
}
}
public synchronized void store(OutputStream out, String header) {
try
{
//FileOutputStream fileoutputstream = new FileOutputStream(header);
store(out, header);
out.close();
}
catch(Exception exception)
{
System.err.println("Problem " + header + ": " + exception);
}
}
public synchronized void store(OutputStream out, String header)
throws IOException
{
PrintWriter wout = new PrintWriter(out);
wout.println("<?xml version='1.0'?>");
if (header != null) {
wout.println("<!--" + header + "-->");
}
wout.print("<properties>");
for (Enumeration e = keys() ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String val = (String)get(key);
wout.print("\n <key name=\"" + key + "\">");
wout.print(encode(val));
wout.print("</key>");
}
wout.print("\n</properties>");
wout.flush();
}
protected StringBuffer encode(String string) {
int len = string.length();
StringBuffer buffer = new StringBuffer(len);
char c;
for (int i = 0 ; i < len ; i++) {
switch (c = string.charAt(i))
{
case '&':
buffer.append("&");
break;
case '<':
buffer.append("<");
break;
case '>':
buffer.append(">");
break;
default:
buffer.append(c);
}
}
return buffer;
}
private Class getParserClass()
throws ClassNotFoundException
{
if (parser_class == null)
parser_class = Class.forName(PARSER_P);
return parser_class;
}
private XMLReader getParser() {
try {
return (XMLReader) getParserClass().newInstance();
} catch (Exception ex) {
throw new RuntimeException("Unable to intantiate : "+
PARSER_P);
}
}
/**
* Creates an empty property list with no default values.
*/
public XMLProperties() {
super();
}
public XMLProperties(Properties defaults) {
super(defaults);
}
/**
* Creates an empty property list with the specified defaults.
* @param parser the XML Parser classname (default is PARSER_P)
* @param defaults the defaults.
*/
public XMLProperties(String parser, Properties defaults) {
super(defaults);
try {
parser_class = Class.forName(parser);
} catch (ClassNotFoundException ex) {
System.err.println("Unable to instanciate parser class: "+parser);
System.err.println("Using default parser.");
}
}
public void setDebug(boolean debug) {
this.debug = debug;
}
}
Here is the error:
XMLProperties.java:28: invalid method declaration; return type required
parser(InputSource in)
^
XMLProperties.java:200: store(java.io.OutputStream,java.lang.String) is already defined in XMLProperties
public synchronized void store(OutputStream out, String header)
^
XMLProperties.java:35: cannot resolve symbol
symbol : method setDocumentLocator (XMLProperties.XMLParser)
location: interface org.xml.sax.XMLReader
parser.setDocumentLocator(this);
^
XMLProperties.java:40: cannot resolve symbol
symbol : class inputStream
location: class XMLProperties.XMLParser
parser.parse(new inputStream (in));
^
XMLProperties.java:158: XMLProperties.XMLParser is abstract; cannot be instantiated
p = new XMLParser(in);
^
XMLProperties.java:170: XMLProperties.XMLParser is abstract; cannot be instantiated
p = new XMLParser(in);
^
6 errors
Any sugestions is cleaning up these errors would be great?
Thanks!
John