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 Ron W Gilmour <gi...@lycos.com> on 2001/05/30 01:00:29 UTC
displaying output from non-GUI Xerces applet
Greetings!
I have a large XML-encoded bibliography (basically, repeated ENTRY
elements, each with 4 children: AUTHOR, TITLE, SOURCE, ANNOTATION). I
would like to use Xerces to parse the XML in a non-displaying applet, then
display the data as HTML in the browser.
I wrote a java application using Xerces/SAX which happily takes a string
on the command line, finds all of the matching entries and returns them.
I've spent a week trying to make this work in an applet with no luck.
The closest thing I can find to what I'm trying to write is the
transformation applet that comes with Apache Xalan. I notice that in the
source code of that applet, the author (Scott Boag) creates a "trusted
agent" thread to allow the applet to access foreign URL's. Is this
necessary even if the only file that the applet needs to access is in the
same directory as the applet source?
The source code is pasted below. The html page is just a 2 frame deal
with an empty lower frame (for the results to appear in) and with the top
frame containing a blank for the user to enter a search term and a button
to start the search. The following JavaScript is in the head of the upper
frame:
var searchString = "";
var results = "";
function clearFrames() {
document.searchForm.searchBlank.value="";
top.lower.document.open();
top.lower.document.clear();
top.lower.document.close();
}
function doSearch() {
searchString = document.searchForm.searchBlank.value;
results = document.SearchApplet.performSearch(searchString);
top.lower.document.write("Results:<br>" + results);
}
The "clearFrames()" function is called "onLoad" from the body tag and
the "doSearch()" function is called "onClick" from the search button:
<form name="searchForm" method="post">
<input type="text" name="searchBlank" maxlength="100" size="50">
<input type="button" name="searchButton" value="Search"
onClick="doSearch();">
</form>
<applet name="SearchApplet"
code="SearchApplet.class"
archive="xerces.jar"
height="0" width="0">
<param name="uri" value="Royats.xml">
</applet>
When I load all of this (locally, using Internet Explorer 5.5), things
look okay at first, but when I enter a search term and click the button, I
get a javascript error that says "java.lang.NullPointerException".
Nothing appears in the way of output, not even the "There is no output"
message from the applet.
I apologize for the long post, but I'm getting a bit desperate. Any help
will be profoundly appreciated!
Sincerely,
Ron Gilmour
gilmr@lycos.com
import java.io.*;
import java.util.*;
import java.applet.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;
public class SearchApplet extends Applet {
public String results;
public static String searchString;
public String uri;
public void init() {
uri = getParameter("uri");
}
public String performSearch(String searchString) {
this.searchString = searchString;
ContentHandler contentHandler = new MyContentHandler();
try {
XMLReader parser = new SAXParser();
parser.setContentHandler(contentHandler);
parser.parse(uri);
} catch (IOException e) {
System.out.println("Error reading URI: " + e.getMessage());
} catch (SAXException e) {
System.out.println("Error parsing URI: " + e.getMessage());
}
results = MyContentHandler.getOutput();
return results;
}
}
// Class for reading and processing the XML data
class MyContentHandler implements ContentHandler {
private Locator locator;
private Vector entries;
private String author;
private String title;
private String source;
private String annotation;
private CharArrayWriter contents;
private Entry current;
private String tempAuthor;
private static String output;
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
public void startDocument() throws SAXException {
entries = new Vector();
contents = new CharArrayWriter();
}
public void startElement(String namespaceURI,
String localName, String rawName,
Attributes atts) throws SAXException {
contents.reset();
}
public void characters(char[] ch, int start, int end) throws
SAXException {
contents.write(ch, start, end);
}
public void endElement (String namespaceURI, String localName,
String rawName) throws SAXException {
if (localName.equals("author")) {
author = contents.toString();
}
else if (localName.equals("title")) {
title = contents.toString();
}
else if (localName.equals("source")) {
source = contents.toString();
}
else if (localName.equals("annotation")) {
annotation = contents.toString();
}
else if (localName.equals("entry")) {
entries.addElement(new Entry(author, title, source,
annotation));
}
}
public void endDocument() throws SAXException {
for(int i=0; i<entries.size(); i++) {
current = (Entry) entries.elementAt(i);
tempAuthor = current.getAuthor().toUpperCase();
if (tempAuthor.indexOf(SearchApplet.searchString) >= 0) {
output += (current.getAuthor() + "<br>" +
current.getTitle() + "<br>" +
current.getSource() + "<br>" +
current.getAnnotation()+ "<br><br>");
}
}
}
public void processingInstruction(String target,
String
data) throws SAXException {}
public void startPrefixMapping(String prefix, String uri) {}
public void endPrefixMapping(String prefix) {}
public void ignorableWhitespace(char[] ch, int start,
int end) throws SAXException {}
public void skippedEntity(String name) throws SAXException {}
public static String getOutput() {
if (output.length() > 0) {
return output;
} else {
return("There is no output!");
}
}
}
// Class for holding Entry information
class Entry {
private String author, title, source, annotation;
// Constructor
public Entry (String author, String title,
String source, String annotation) {
this.author = author;
this.title = title;
this.source = source;
this.annotation = annotation;
}
// Accessor methods
public String getAuthor() { return author; }
public String getTitle() { return title; }
public String getSource() { return source; }
public String getAnnotation() { return annotation; }
}
---
You are currently subscribed to java_xml as: gilmr@lycos.com
To unsubscribe send a blank email to leave-java_xml-542774O@p2p.wrox.com
--------- End Forwarded Message ---------
Get 250 color business cards for FREE!
http://businesscards.lycos.com/vp/fastpath/
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org