?? xmlcontenthandler.java
字號:
/** * JWAP - A Java Implementation of the WAP Protocols * Copyright (C) 2001-2004 Niko Bender * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package net.sourceforge.jwap.util.wbxml;import java.io.IOException;import java.util.Enumeration;import java.util.Iterator;import java.util.Stack;import java.util.Vector;import net.sourceforge.jwap.util.TransTable;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.ext.LexicalHandler;import org.xml.sax.helpers.DefaultHandler;/** * <p>This class is used by WBXMLEncoder to encode an XML file into WBXML. * It catches the SAX events as the XML document is parsed. Token stream * is generated by reading the mapped byte codes for the elements and attributes * These mappings are read from the token repository for the given XML vocabulary</p> * * @author <a href="mailto:suvarna@witscale.com">Suvarna Kadam</a> * @see net.sourceforge.jwap.util.wbxml.WBXMLEncoder * @see net.sourceforge.jwap.util.wbxml.TokenRepository */public class XMLContentHandler extends DefaultHandler implements LexicalHandler { private Stack parentTrace; private Vector tokens; private String encoding; private byte parentBitMask = (byte) 0x40; // The bit representation 10000000 private byte attributeBitMask = (byte) 0x80; // The bit representation 01000000 private byte[] stringTable; private int stringTableOffset = 0; private TokenRepository tokenRepository; private boolean switchCodepage; private boolean publicIdTokenAdded = false; /** * Constructor for XMLContentHandler. */ public XMLContentHandler(String encoding) { this.encoding = encoding.toUpperCase(); parentTrace = new Stack(); tokens = new Vector(); } /** * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ public void characters(char[] ch, int start, int length) throws SAXException { // System.out.println("characters"); String inlineString = String.valueOf(ch, start, length).trim(); if (inlineString.length() > 0) { checkForParentTag(); writeInlineString(inlineString); } } private int writeToStringBuf(String aString) { /* int index = 0; for (int j = 0; j < aString.length(); j++) stringTable[]= (byte) aString.charAt(j); writeInlineStringToken((byte) 00); */ return 0; } /** * @see org.xml.sax.ContentHandler#endDocument() */ public void endDocument() throws SAXException {// System.out.println("endDocument... tokens ----");// for (int i = 0; i < tokens.size(); i++) {// Token token = (Token) tokens.elementAt(i);// if (token.getName().equals("STR_INLINE"))// System.out.println((char) token.getValue() + " "+token.getValue());// else// System.out.println(Integer.toHexString(token.getValue()) + " " + token.getName() + token.getValue());// } /*for (int i = 0; i < tokens.size(); i++) { Token token = (Token) tokens.elementAt(i); System.out.print(Integer.toHexString(token.getValue()) + " "); }*/ } /** * @see org.xml.sax.ContentHandler#endElement(String, String, String) */ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { // System.out.println("endElement"); // pop the element from the stack if any if (!parentTrace.isEmpty()) parentTrace.pop(); else tokens.addElement(new TagToken("END element" + localName, GlobalTokens.END)); } /** * @see org.xml.sax.ContentHandler#endPrefixMapping(String) */ public void endPrefixMapping(String prefix) throws SAXException { // System.out.println("endPrefixMapping"); if(switchCodepage) { TokenRepository.switchCodepage(null); switchCodepage = false; } } /** * @see org.xml.sax.ErrorHandler#error(SAXParseException) */ public void error(SAXParseException exception) throws SAXException { //System.out.println("error"); } /** * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException) */ public void fatalError(SAXParseException exception) throws SAXException { //System.out.println("fatal error"); } /** * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) */ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { //System.out.println("ignorableWhitespace"); } /** * @see org.xml.sax.DTDHandler#notationDecl(String, String, String) */ public void notationDecl(String name, String publicId, String systemId) throws SAXException { //System.out.println("notationDecl"); } /** * @see org.xml.sax.ContentHandler#processingInstruction(String, String) */ public void processingInstruction(String target, String data) throws SAXException { //System.out.println("target= " + target + " data " + data); } /** * @throws SAXException * @throws IOException * @see org.xml.sax.EntityResolver#resolveEntity(String, String) */ public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { // System.out.println("resolveEntity" + publicId + systemId); // return new InputSource(systemId); return super.resolveEntity(publicId,systemId); } /** * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator) */ public void setDocumentLocator(Locator locator) { //System.out.println("setDocumentLocator" + locator.getPublicId() + locator.getSystemId()); super.setDocumentLocator(locator); } /** * @see org.xml.sax.ContentHandler#skippedEntity(String) */ public void skippedEntity(String name) throws SAXException { // System.out.println("skippedEntity"); super.skippedEntity(name); } /** * @see org.xml.sax.ContentHandler#startDocument() * as per bnf of wbxml... * ?? from where should the value for wbxml is taken??? application property?? */ public void startDocument() throws SAXException { //System.out.println("startDocument"); Token version = new Token("version",(byte)3); // temporarily hardcoded to wbxml version 1.3 // TODO read wbxml version from externally either from propeerties file or from command line arguments tokens.addElement(version); } /** * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes) */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { // System.out.println("startElement" + qName); if (!publicIdTokenAdded) {// The document does not have Doctype Declaration which means Public ID 1 (for "UNKNOWN") should be addded Token publicIdToken = new Token("publicid",(byte)1); tokens.addElement(publicIdToken); tokenRepository = TokenRepository.getInstance("1"); publicIdTokenAdded = true; } checkForParentTag(); boolean hasAttributes = atts.getLength() > 0; createTagToken(localName, hasAttributes); if (hasAttributes) createAttributeTags(localName, atts); } private void checkForParentTag() { // check if this element has a parent.. if (!parentTrace.isEmpty()) { TagToken parentToken = (TagToken) parentTrace.pop(); if (parentToken != null) { // apply the bit mask for parent tag. byte tokenValue = (byte) (parentToken.getValue() | parentBitMask); parentToken.setValue(tokenValue); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -