[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.record; |
---|
| 20 | |
---|
| 21 | import java.io.InputStream; |
---|
| 22 | import java.io.IOException; |
---|
| 23 | import java.util.ArrayList; |
---|
| 24 | |
---|
| 25 | import org.xml.sax.*; |
---|
| 26 | import org.xml.sax.helpers.DefaultHandler; |
---|
| 27 | import javax.xml.parsers.SAXParserFactory; |
---|
| 28 | import javax.xml.parsers.SAXParser; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * XML Deserializer. |
---|
| 32 | */ |
---|
| 33 | public class XmlRecordInput implements RecordInput { |
---|
| 34 | |
---|
| 35 | static private class Value { |
---|
| 36 | private String type; |
---|
| 37 | private StringBuffer sb; |
---|
| 38 | |
---|
| 39 | public Value(String t) { |
---|
| 40 | type = t; |
---|
| 41 | sb = new StringBuffer(); |
---|
| 42 | } |
---|
| 43 | public void addChars(char[] buf, int offset, int len) { |
---|
| 44 | sb.append(buf, offset, len); |
---|
| 45 | } |
---|
| 46 | public String getValue() { return sb.toString(); } |
---|
| 47 | public String getType() { return type; } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | private static class XMLParser extends DefaultHandler { |
---|
| 51 | private boolean charsValid = false; |
---|
| 52 | |
---|
| 53 | private ArrayList<Value> valList; |
---|
| 54 | |
---|
| 55 | private XMLParser(ArrayList<Value> vlist) { |
---|
| 56 | valList = vlist; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public void startDocument() throws SAXException {} |
---|
| 60 | |
---|
| 61 | public void endDocument() throws SAXException {} |
---|
| 62 | |
---|
| 63 | public void startElement(String ns, |
---|
| 64 | String sname, |
---|
| 65 | String qname, |
---|
| 66 | Attributes attrs) throws SAXException { |
---|
| 67 | charsValid = false; |
---|
| 68 | if ("boolean".equals(qname) || |
---|
| 69 | "i4".equals(qname) || |
---|
| 70 | "int".equals(qname) || |
---|
| 71 | "string".equals(qname) || |
---|
| 72 | "double".equals(qname) || |
---|
| 73 | "ex:i1".equals(qname) || |
---|
| 74 | "ex:i8".equals(qname) || |
---|
| 75 | "ex:float".equals(qname)) { |
---|
| 76 | charsValid = true; |
---|
| 77 | valList.add(new Value(qname)); |
---|
| 78 | } else if ("struct".equals(qname) || |
---|
| 79 | "array".equals(qname)) { |
---|
| 80 | valList.add(new Value(qname)); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public void endElement(String ns, |
---|
| 85 | String sname, |
---|
| 86 | String qname) throws SAXException { |
---|
| 87 | charsValid = false; |
---|
| 88 | if ("struct".equals(qname) || |
---|
| 89 | "array".equals(qname)) { |
---|
| 90 | valList.add(new Value("/"+qname)); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public void characters(char buf[], int offset, int len) |
---|
| 95 | throws SAXException { |
---|
| 96 | if (charsValid) { |
---|
| 97 | Value v = valList.get(valList.size()-1); |
---|
| 98 | v.addChars(buf, offset, len); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | private class XmlIndex implements Index { |
---|
| 105 | public boolean done() { |
---|
| 106 | Value v = valList.get(vIdx); |
---|
| 107 | if ("/array".equals(v.getType())) { |
---|
| 108 | valList.set(vIdx, null); |
---|
| 109 | vIdx++; |
---|
| 110 | return true; |
---|
| 111 | } else { |
---|
| 112 | return false; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | public void incr() {} |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | private ArrayList<Value> valList; |
---|
| 119 | private int vLen; |
---|
| 120 | private int vIdx; |
---|
| 121 | |
---|
| 122 | private Value next() throws IOException { |
---|
| 123 | if (vIdx < vLen) { |
---|
| 124 | Value v = valList.get(vIdx); |
---|
| 125 | valList.set(vIdx, null); |
---|
| 126 | vIdx++; |
---|
| 127 | return v; |
---|
| 128 | } else { |
---|
| 129 | throw new IOException("Error in deserialization."); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** Creates a new instance of XmlRecordInput */ |
---|
| 134 | public XmlRecordInput(InputStream in) { |
---|
| 135 | try{ |
---|
| 136 | valList = new ArrayList<Value>(); |
---|
| 137 | DefaultHandler handler = new XMLParser(valList); |
---|
| 138 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
---|
| 139 | SAXParser parser = factory.newSAXParser(); |
---|
| 140 | parser.parse(in, handler); |
---|
| 141 | vLen = valList.size(); |
---|
| 142 | vIdx = 0; |
---|
| 143 | } catch (Exception ex) { |
---|
| 144 | throw new RuntimeException(ex); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | public byte readByte(String tag) throws IOException { |
---|
| 149 | Value v = next(); |
---|
| 150 | if (!"ex:i1".equals(v.getType())) { |
---|
| 151 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 152 | } |
---|
| 153 | return Byte.parseByte(v.getValue()); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | public boolean readBool(String tag) throws IOException { |
---|
| 157 | Value v = next(); |
---|
| 158 | if (!"boolean".equals(v.getType())) { |
---|
| 159 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 160 | } |
---|
| 161 | return "1".equals(v.getValue()); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public int readInt(String tag) throws IOException { |
---|
| 165 | Value v = next(); |
---|
| 166 | if (!"i4".equals(v.getType()) && |
---|
| 167 | !"int".equals(v.getType())) { |
---|
| 168 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 169 | } |
---|
| 170 | return Integer.parseInt(v.getValue()); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | public long readLong(String tag) throws IOException { |
---|
| 174 | Value v = next(); |
---|
| 175 | if (!"ex:i8".equals(v.getType())) { |
---|
| 176 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 177 | } |
---|
| 178 | return Long.parseLong(v.getValue()); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public float readFloat(String tag) throws IOException { |
---|
| 182 | Value v = next(); |
---|
| 183 | if (!"ex:float".equals(v.getType())) { |
---|
| 184 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 185 | } |
---|
| 186 | return Float.parseFloat(v.getValue()); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | public double readDouble(String tag) throws IOException { |
---|
| 190 | Value v = next(); |
---|
| 191 | if (!"double".equals(v.getType())) { |
---|
| 192 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 193 | } |
---|
| 194 | return Double.parseDouble(v.getValue()); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | public String readString(String tag) throws IOException { |
---|
| 198 | Value v = next(); |
---|
| 199 | if (!"string".equals(v.getType())) { |
---|
| 200 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 201 | } |
---|
| 202 | return Utils.fromXMLString(v.getValue()); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | public Buffer readBuffer(String tag) throws IOException { |
---|
| 206 | Value v = next(); |
---|
| 207 | if (!"string".equals(v.getType())) { |
---|
| 208 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 209 | } |
---|
| 210 | return Utils.fromXMLBuffer(v.getValue()); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | public void startRecord(String tag) throws IOException { |
---|
| 214 | Value v = next(); |
---|
| 215 | if (!"struct".equals(v.getType())) { |
---|
| 216 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | public void endRecord(String tag) throws IOException { |
---|
| 221 | Value v = next(); |
---|
| 222 | if (!"/struct".equals(v.getType())) { |
---|
| 223 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | public Index startVector(String tag) throws IOException { |
---|
| 228 | Value v = next(); |
---|
| 229 | if (!"array".equals(v.getType())) { |
---|
| 230 | throw new IOException("Error deserializing "+tag+"."); |
---|
| 231 | } |
---|
| 232 | return new XmlIndex(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | public void endVector(String tag) throws IOException {} |
---|
| 236 | |
---|
| 237 | public Index startMap(String tag) throws IOException { |
---|
| 238 | return startVector(tag); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | public void endMap(String tag) throws IOException { endVector(tag); } |
---|
| 242 | |
---|
| 243 | } |
---|