You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ia...@apache.org on 2004/09/19 00:47:00 UTC
cvs commit: ws-axis/contrib/j2me/xml/samples/simple-parsing sample.xml
ias 2004/09/18 15:46:59
Added: contrib/j2me/xml/samples/memo-viewer/src MemoViewer.java
contrib/j2me/xml/samples/simple-parsing/src
SimpleSAXTest.java
contrib/j2me/xml/samples/simple-parsing sample.xml
Log:
Move simple test to samples and add memo viewer example based on MIDP.
Revision Changes Path
1.1 ws-axis/contrib/j2me/xml/samples/memo-viewer/src/MemoViewer.java
Index: MemoViewer.java
===================================================================
import java.io.InputStream;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class MemoViewer extends MIDlet implements CommandListener {
private static final String MEMO_URL = "http://www.iasandcb.pe.kr/memo.xml";
private static final Command CMD_EXIT =
new Command("Exit", Command.EXIT, 1);
private static final Command CMD_VIEW =
new Command("View", Command.ITEM, 1);
private Display display;
private SAXParserFactory factory;
private SAXParser parser;
private MemoHandler memoHandler;
private TextBox memoBox;
public MemoViewer() {
memoBox = new TextBox("Memo Viewer", "Disconnected", 100, TextField.ANY);
factory = SAXParserFactory.newInstance();
try {
parser = factory.newSAXParser();
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
}
memoHandler = new MemoHandler(memoBox);
}
protected void startApp() {
display = Display.getDisplay(this);
memoBox.addCommand(CMD_EXIT);
memoBox.addCommand(CMD_VIEW);
memoBox.setCommandListener(this);
display.setCurrent(memoBox);
}
public void commandAction(Command c, Displayable d) {
if (c == CMD_EXIT) {
destroyApp(false);
notifyDestroyed();
}
else if (c == CMD_VIEW) {
StreamConnection con = null;
InputStream in = null;
try {
con = (StreamConnection) Connector.open(MEMO_URL);
in = con.openInputStream();
try {
parser.parse(in, memoHandler);
}
catch (SAXParseException se) {
}
catch (SAXException se) {
}
} catch (IOException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (con != null) {
try {
con.close();
} catch (IOException e) {
}
}
}
}
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
private class MemoHandler extends DefaultHandler {
private StringBuffer textBuffer;
private TextBox memoBox;
MemoHandler(TextBox memoBox) {
this.memoBox = memoBox;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals("subject")) {
textBuffer = null;
} else if (localName.equals("content")) {
textBuffer = null;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equals("subject")) {
memoBox.setTitle(textBuffer.toString());
} else if (localName.equals("content")) {
memoBox.setString(textBuffer.toString());
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
String s = new String(ch, start, length);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
}
else {
textBuffer.append(s);
}
}
}
}
1.1 ws-axis/contrib/j2me/xml/samples/simple-parsing/src/SimpleSAXTest.java
Index: SimpleSAXTest.java
===================================================================
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* A simple tester for J2ME JAXP
*
* @author Ias (iasandcb@tmax.co.kr)
*/
public class SimpleSAXTest {
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
Writer out = new OutputStreamWriter(System.out);
SimpleHandler handler = new SimpleHandler(out);
// For testing on a specific platform, you need to modify the following line.
InputSource is = new InputSource("file:/home/zaurus/Documents/sample.xml");
try {
parser.parse(is, handler);
}
catch (SAXParseException se) {
se.printStackTrace();
System.out.println(se.getMessage());
System.out.println(se.getLineNumber());
System.out.println(se.getColumnNumber());
}
}
private static class SimpleHandler extends DefaultHandler {
private Writer out;
private String indentString = " "; // Amount to indent
private int indentLevel = 0;
private StringBuffer textBuffer;
SimpleHandler(Writer out) {
this.out = out;
}
private void emit(String s) throws SAXException {
try {
out.write(s);
out.flush();
}
catch (IOException e) {
throw new SAXException("I/O error");
}
}
private void echoText() throws SAXException {
if (textBuffer == null)
return;
nl();
emit("CHARS: ");
String s = "" + textBuffer;
if (!s.trim().equals(""))
emit(s);
textBuffer = null;
}
private void nl() throws SAXException {
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
for (int i = 0; i < indentLevel; i++)
out.write(indentString);
}
catch (IOException e) {
throw new SAXException("I/O error");
}
}
public void setDocumentLocator(Locator locator) {
try {
emit("LOCATOR SET");
nl();
}
catch (Exception e) {
// Ignore errors
}
}
public void startDocument() throws SAXException {
emit("<?xml version='1.0' encoding='UTF-8'?>");
nl();
}
public void endDocument() throws SAXException {
try {
nl();
out.flush();
}
catch (IOException e) {
throw new SAXException("I/O error");
}
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
echoText();
indentLevel++;
nl();
emit("ELEMENT: ");
String eName = localName; // element name
if ("".equals(eName))
eName = qName; // not namespaceAware
emit("<" + eName);
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
String aName = attributes.getLocalName(i); // Attr name
if ("".equals(aName))
aName = attributes.getQName(i);
nl();
emit(" ATTR: ");
emit(aName);
emit("\t\"");
emit(attributes.getValue(i));
emit("\"");
}
}
if (attributes.getLength() > 0)
nl();
emit(">");
}
public void endElement(String uri, String localName, String qName) throws SAXException {
echoText();
nl();
emit("END_ELM: ");
String eName = localName; // element name
if ("".equals(eName))
eName = qName; // not namespaceAware
emit("</" + eName + ">");
indentLevel--;
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
System.out.println("start prefix uri " + uri);
System.out.println("start prefix prefix " + prefix);
}
public void endPrefixMapping(String prefix) throws SAXException {
System.out.println("end prefix prefix " + prefix);
}
public void characters(char[] ch, int start, int length) throws SAXException {
String s = new String(ch, start, length);
if (textBuffer == null) {
textBuffer = new StringBuffer(s);
}
else {
textBuffer.append(s);
}
}
public void ignorableWhitespace(char buf[], int offset, int Len) throws SAXException {
nl();
emit("IGNORABLE");
}
public void processingInstruction(String target, String data) throws SAXException {
nl();
emit("PROCESS: ");
emit("<?" + target + " " + data + "?>");
nl();
}
public void skippedEntity(String name) throws SAXException {
nl();
emit("SKIPPED ENTITY: ");
emit("<!" + name + ">");
nl();
}
}
}
1.1 ws-axis/contrib/j2me/xml/samples/simple-parsing/sample.xml
Index: sample.xml
===================================================================
<?xml version="1.0" encoding="US-ASCII"?>
<?my.presentation.Program QUERY="exec, tech, all"?>
<!DOCTYPE slideshow SYSTEM "slideshow1a.dtd">
<!DOCTYPE slideshow [
<!ELEMENT FOO (bar)* >
<!ELEMENT ok (#PCDATA) >
<!ATTLIST ok
att ID #REQUIRED>
<!ENTITY test "this is a
test">
]>
<!-- test -->
<FOO>
<![CDATA[Diagram ...
....
.............
...
dfjaldf
jfdlas
]]]]
]]>
<ok att=" a a "/>
<bar>aa -a&E &test;</bar>
<bar><foo></foo></bar>
<bar>aaa<foo>bbb</foo>ccc</bar>
<bar a="1
2
3"></bar>
<bar xmlns="aaa" att="b"></bar>
<bar xmlns:p="aaa" p:attr="c"></bar>
<p:bar xmlns:p="aaa" xmlns:b="ttt"></p:bar>
<bar xmlns="kkk" xmlns:p="aaa" xmlns:b="ttt"></bar>
<p:bar xmlns:p="aaa" xmlns:b="ttt"></p:bar>
</FOO>