当前页面: 开发资料首页 → J2EE 专题 → 生产-消费模式的XML解析
摘要:
public synchronized void put(Object data) {
// check to see if the length is 2
while (list.size() >= 2) {
try {
System.out.println("Waiting to put data");
wait();
}
catch (Exception ex) {
}
}
list.add(data);
notifyAll();
}
public synchronized Object take() {
// wait until there is data to get
// come out if the end of file signaled
while (list.size() <= 0 && (eof != true)) {
try {
System.out.println("Waiting to consume data");
wait();
} catch (Exception ex) {
}
}
Object obj = null;
if (list.size() > 0) {
obj = list.remove(0);
} else {
System.out.println("Woke up because end of document");
}
notifyAll();
return obj;
}public void startElement( String namespaceURI, String localName,
String qName, Attributes atts )
throws SAXException {
System.out.println(
" startElement local names............." +
localName + " " + qName);
if (qName.equalsIgnoreCase(elemmark)) {
doc = new Hashtable();
}
elem = qName;
}
public void endElement( String namespaceURI, String localName,
String qName )
throws SAXException {
String s = sbData.toString();
System.out.println("element " + elem + " character " + s);
if ((doc != null) & (s != null) & !(s.trim().equals("")))
doc.put(elem, s);
sbData = new StringBuffer();
System.out.println(" endElement ending element............." + qName);
if (qName.equalsIgnoreCase(elemmark)) {
System.out.println(
" endElement ending element............." + localName);
smartQueue.put(doc);
doc = null;
}
}
public void endDocument() throws SAXException {
smartQueue.end();
System.out.println("End Document.............");
}public void run() {
while (!queue.isEmpty() || !queue.onEnd()) {
Hashtable val = (Hashtable) queue.take();
System.out.println("Obtained by " + this.getName() + " " + val);
// try {
// System.out.println("Simulate lengthy
processing...........");
// Thread.sleep(2000);
// }
// catch(Exception ex){}
}
}