1package com.mot.dm.dbtool; 2 3import java.io.*; 4import java.util.*; 5 6import javax.xml.parsers.*; 7import org.w3c.dom.*; 8import org.xml.sax.InputSource; 9 10public class WbxmlEncoder { 11 12 public int currentTab = 0x00; 13 public ArrayList arrBytes = new ArrayList(); 14 15 public WbxmlEncoder() { 16 arrBytes.add(new Byte( (byte) 0x03)); 17 arrBytes.add(new Byte( (byte) 0x0b)); 18 arrBytes.add(new Byte( (byte) 0x6a)); 19 arrBytes.add(new Byte( (byte) 0x00)); 20 } 21 22 public byte[] encode(File fileXml) throws Exception { 23 FileInputStream streamer = new FileInputStream(fileXml); 24 byte[] byteArray = new byte[streamer.available()]; 25 streamer.read(byteArray); 26 streamer.close(); 27 streamer=null; 28 return encode(new String(byteArray)); 29 } 30 31 public byte[] encode(String xml) throws Exception { 32 NodeList list; 33 Document document = getDocument("<root>\n" + xml + "\n</root>"); 34 list = document.getDocumentElement().getChildNodes(); 35 36 for (int i = 0; i < list.getLength(); i++) { 37 Node node = list.item(i); 38 if (node.getNodeType() == Node.ELEMENT_NODE) { 39 proceedNode(node); 40 } 41 } 42 return listToByteArr(); 43 } 44 45 public void proceedNode(Node node) throws Exception { 46 String nodeName = node.getNodeName(); 47 NamedNodeMap attributes = node.getAttributes(); 48 NodeList list = node.getChildNodes(); 49 50 // proceed tag. 51 proceedTag(nodeName, node.hasAttributes(), node.hasChildNodes()); 52 53 //proceed all attributes 54 for (int i = 0; i < attributes.getLength(); i++) { 55 Node attribute = attributes.item(i); 56 proceedAttribute(attribute.getNodeName(), attribute.getNodeValue()); 57 } 58 // close tag after all attributes if required... 59 if (node.hasAttributes()) { 60 arrBytes.add(new Byte( (byte) 0x01)); 61 } 62 63 //procced all children 64 Node n; 65 for (int i = 0; i < list.getLength(); i++) { 66 n = list.item(i); 67 if (n.getNodeType() == Node.ELEMENT_NODE) { 68 proceedNode(list.item(i)); 69 } 70 } 71 // close tag after all childrens if required... 72 if (node.hasChildNodes()) { 73 arrBytes.add(new Byte( (byte) 0x01)); 74 } 75 } 76 77 public void proceedTag(String nodeName, boolean hasAttrs, boolean hasChildren) throws 78 Exception { 79 int shift = 0; 80 if (hasAttrs) { 81 shift = shift | 0x80; 82 } 83 if (hasChildren) { 84 shift = shift | 0x40; 85 } 86 ValTab tag = OMACP.encodeTag(nodeName, shift); 87 if (tag.encVal == null) { 88 throw new Exception(" Error: Unsupported tag '" + nodeName + 89 "' has been used."); 90 } 91 // check for table 92 checkTablesSetting(tag); 93 //add encoded tag 94 arrBytes.add(tag.encVal); 95 } 96 97 public void proceedAttribute(String attrName, String attrValue) throws 98 Exception { 99 100 //get encoded "attribute=value" if exists 101 ValTab attrVal = OMACP.encodeAttr(attrName + "=" + attrValue); 102 if (attrVal.encVal != null) { 103 checkTablesSetting(attrVal); 104 //add encoded attribute and value 105 arrBytes.add(attrVal.encVal); 106 return; 107 } 108 109 //get encoded attribute 110 ValTab attrN = OMACP.encodeAttr(attrName); 111 if (attrN.encVal == null) { 112 throw new Exception(" Error: Unsupported attribute '" + attrName + 113 "' has been used."); 114 } 115 checkTablesSetting(attrN); 116 arrBytes.add(attrN.encVal); 117 118 //get encoded value if exists 119 ValTab attrV = OMACP.encodeVal(attrValue); 120 if (attrV.encVal != null) { 121 checkTablesSetting(attrV); 122 arrBytes.add(attrV.encVal); 123 return; 124 } 125 else { 126 // add string value 127 arrBytes.add(new Byte( (byte) 0x03)); 128 //convert string to bytes 129 byte[] b = attrValue.getBytes(); 130 for (int i = 0; i < b.length; i++) { 131 arrBytes.add(new Byte(b[i])); 132 } 133 //end string 134 arrBytes.add(new Byte( (byte) 0x00)); 135 } 136 137 } 138 139 public void checkTablesSetting(ValTab tag) { 140 if (tag.table != currentTab) { 141 arrBytes.add(new Byte( (byte) 0x00)); 142 arrBytes.add(new Byte( (byte) tag.table)); 143 currentTab = tag.table; 144 } 145 } 146 147 private Document getDocument(String xml) throws Exception { 148 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 149 factory.setValidating(false); 150 byte[] utf8_bytes = xml.getBytes("UTF-8"); 151 return factory.newDocumentBuilder().parse(new InputSource(new 152 ByteArrayInputStream(utf8_bytes))); 153 } 154 155 public byte[] listToByteArr() { 156 Byte b; 157 byte[] wbxml = new byte[arrBytes.size()]; 158 for (int i = 0; i < arrBytes.size(); i++) { 159 b = (Byte) arrBytes.get(i); 160 wbxml[i] = b.byteValue(); 161 } 162 return wbxml; 163 } 164 165 public static void main(String[] args) throws Exception { 166 FileInputStream fs = new FileInputStream("WAP.xml"); 167 byte[] arr = new byte[fs.available()]; 168 fs.read(arr); 169 String s = new String(arr); 170 171 WbxmlEncoder encoder = new WbxmlEncoder(); 172 byte[] wbxml = encoder.encode(s); 173 FileOutputStream fos = new FileOutputStream("WAP.wbxml"); 174 fos.write(wbxml); 175 fos.flush(); 176 } 177} 178