wrap gwt DOM into SAX parser
This commit is contained in:
parent
0c0b376c1e
commit
ec03add2a6
@ -0,0 +1,83 @@
|
||||
package org.oscim.backend;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
|
||||
import com.google.gwt.xml.client.NamedNodeMap;
|
||||
import com.google.gwt.xml.client.Node;
|
||||
|
||||
public class MyAttributes implements Attributes{
|
||||
private NamedNodeMap map;
|
||||
|
||||
public MyAttributes(Node n){
|
||||
map = n.getAttributes();
|
||||
}
|
||||
|
||||
public String getValue(int i) {
|
||||
return map.item(i).getNodeValue();
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return map.getLength();
|
||||
}
|
||||
|
||||
public String getLocalName(int i) {
|
||||
return map.item(i).getNodeName();
|
||||
}
|
||||
|
||||
public String getValue(String string) {
|
||||
Node n = map.getNamedItem(string);
|
||||
if (n == null)
|
||||
return null;
|
||||
|
||||
return n.getNodeValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getURI(int paramInt) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQName(int paramInt) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType(int paramInt) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(String paramString1, String paramString2) {
|
||||
Log.d("..", "missing");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(String paramString) {
|
||||
Log.d("..", "missing");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType(String paramString1, String paramString2) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType(String paramString) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue(String paramString1, String paramString2) {
|
||||
Log.d("..", "missing");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
package org.oscim.backend;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.oscim.backend.Log;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import com.google.gwt.xml.client.Document;
|
||||
import com.google.gwt.xml.client.Node;
|
||||
import com.google.gwt.xml.client.NodeList;
|
||||
import com.google.gwt.xml.client.XMLParser;
|
||||
|
||||
public class MyXMLReader {
|
||||
public void parse(InputStream is) throws SAXException {
|
||||
Log.d("..", "read theme");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
byte[] buf = new byte[8192];
|
||||
int read;
|
||||
try {
|
||||
while ((read = is.read(buf)) >= 0) {
|
||||
if (read > 0)
|
||||
sb.append(new String(buf, 0, read));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Document d = XMLParser.parse(sb.toString());
|
||||
handleElement(d.getFirstChild());
|
||||
mHandler.endDocument();
|
||||
}
|
||||
|
||||
int level = 0;
|
||||
|
||||
void handleElement(Node n) throws SAXException {
|
||||
if (n == null) {
|
||||
//Log.d("..", "null element");
|
||||
return;
|
||||
}
|
||||
//Log.d("..", "handle element " + n.getNodeName());
|
||||
|
||||
if (n.getNodeType() == Node.ELEMENT_NODE) {
|
||||
|
||||
String localName = n.getNodeName();
|
||||
mHandler.startElement(null, localName, null, new MyAttributes(n));
|
||||
|
||||
if (n.hasChildNodes()) {
|
||||
NodeList l = n.getChildNodes();
|
||||
for (int i = 0, len = l.getLength(); i < len; i++) {
|
||||
//Log.d("..", "get child " + i + "/" + len);
|
||||
handleElement(l.item(i));
|
||||
}
|
||||
}
|
||||
mHandler.endElement(null, localName, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private DefaultHandler mHandler;
|
||||
|
||||
public void setContentHandler(DefaultHandler handler) {
|
||||
mHandler = handler;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.oscim.backend;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
public class XMLReaderAdapter {
|
||||
public void parse(DefaultHandler handler, InputStream is) throws IOException, SAXException {
|
||||
|
||||
MyXMLReader xmlReader = new MyXMLReader();
|
||||
xmlReader.setContentHandler(handler);
|
||||
xmlReader.parse(is);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package org.xml.sax;
|
||||
|
||||
|
||||
public abstract interface Attributes {
|
||||
public abstract int getLength();
|
||||
|
||||
public abstract String getURI(int paramInt);
|
||||
|
||||
public abstract String getLocalName(int paramInt);
|
||||
|
||||
public abstract String getQName(int paramInt);
|
||||
|
||||
public abstract String getType(int paramInt);
|
||||
|
||||
public abstract String getValue(int paramInt);
|
||||
|
||||
public abstract int getIndex(String paramString1, String paramString2);
|
||||
|
||||
public abstract int getIndex(String paramString);
|
||||
|
||||
public abstract String getType(String paramString1, String paramString2);
|
||||
|
||||
public abstract String getType(String paramString);
|
||||
|
||||
public abstract String getValue(String paramString1, String paramString2);
|
||||
|
||||
public abstract String getValue(String paramString);
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package org.xml.sax;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SAXException extends IOException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SAXException(String str) {
|
||||
super(str);
|
||||
}
|
||||
public SAXException(String str, Throwable throwable)
|
||||
{
|
||||
super(str);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package org.xml.sax;
|
||||
|
||||
public class SAXParseException extends SAXException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SAXParseException(String str) {
|
||||
super(str);
|
||||
}
|
||||
public SAXParseException(String str, Throwable throwable)
|
||||
{
|
||||
super(str);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package org.xml.sax.helpers;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
public class DefaultHandler {
|
||||
public void endDocument() {
|
||||
|
||||
}
|
||||
|
||||
public void error(SAXParseException exception) {
|
||||
|
||||
}
|
||||
public void warning(SAXParseException exception) {
|
||||
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName,
|
||||
Attributes attributes) throws SAXException {
|
||||
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user