PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/encodersl/encoder-coco/src/com/sun/encoder/coco/runtime/UnmarshalAdaptor.java

https://bitbucket.org/pymma/openesb-components
Java | 395 lines | 308 code | 37 blank | 50 comment | 41 complexity | b51106dea597f8b55f88335093f40bef MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)UnmarshalAdaptor.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.encoder.coco.runtime;
  30. import com.sun.encoder.coco.runtime.messages.ErrorManager;
  31. import com.sun.encoder.coco.runtime.messages.Message;
  32. import com.sun.encoder.coco.runtime.messages.MessageCatalog;
  33. import com.sun.encoder.runtime.provider.Misc;
  34. import com.sun.encoder.tools.xml.EmptyAttributes;
  35. import com.sun.encoder.tools.xml.SchemaLocationAttributes;
  36. import java.io.BufferedInputStream;
  37. import java.io.ByteArrayInputStream;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import javax.xml.XMLConstants;
  41. import org.xml.sax.Attributes;
  42. import org.xml.sax.ContentHandler;
  43. import org.xml.sax.DTDHandler;
  44. import org.xml.sax.EntityResolver;
  45. import org.xml.sax.ErrorHandler;
  46. import org.xml.sax.InputSource;
  47. import org.xml.sax.Locator;
  48. import org.xml.sax.SAXException;
  49. import org.xml.sax.SAXNotRecognizedException;
  50. import org.xml.sax.SAXNotSupportedException;
  51. import org.xml.sax.SAXParseException;
  52. import org.xml.sax.XMLReader;
  53. import java.net.URL;
  54. import java.util.logging.Level;
  55. import java.util.logging.Logger;
  56. /**
  57. * This class implements an unmarshal adaptor for decoding COBOL Copybook
  58. * encoded data. It implements SAX XMLReader interface, but
  59. * the input source is not in XML format but COBOL Copybook
  60. * encoded. The adaptor translates COBOL Copybook encoded data
  61. * into a set of SAX events.
  62. *
  63. * @author Jun Xu
  64. * @since 6.0
  65. */
  66. public final class UnmarshalAdaptor implements XMLReader {
  67. private static final ErrorManager cErrorMgr =
  68. ErrorManager.getManager("OpenESB.encoder.COBOLCopybook."
  69. + UnmarshalAdaptor.class.getName());
  70. private static final Attributes mEmptyAttributes =
  71. new EmptyAttributes();
  72. private final URL mSchemaLocation;
  73. private final RuleNode mTopRule;
  74. private final byte[][] mRedefined;
  75. private final Integer[] mOccursDependOn;
  76. private EntityResolver mEntityResolver;
  77. private DTDHandler mDTDHandler;
  78. private ContentHandler mContentHandler;
  79. private ErrorHandler mErrorHandler;
  80. private InputStream mInStream;
  81. private String mPublicId;
  82. private String mSystemId;
  83. private long mBytesRead;
  84. /** Logger object.*/
  85. private Logger mLog = Logger.getLogger(getClass().getName());
  86. /** Creates a new instance of UnmarshalAdaptor */
  87. public UnmarshalAdaptor(URL schemaLocation, RuleNode topRule) {
  88. if (topRule == null) {
  89. throw new NullPointerException("no top rule.");
  90. }
  91. mSchemaLocation = schemaLocation;
  92. mTopRule = topRule;
  93. mRedefined =
  94. new byte[topRule.getContext().getRedefinedNodes().length][];
  95. mOccursDependOn =
  96. new Integer[topRule.getContext().getOccursDependOnNodes().length];
  97. }
  98. public boolean getFeature(String name)
  99. throws SAXNotRecognizedException, SAXNotSupportedException {
  100. if ("http://xml.org/sax/features/namespaces".equals(name)) {
  101. return true;
  102. }
  103. if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
  104. return false;
  105. }
  106. throw new SAXNotRecognizedException();
  107. }
  108. public void setFeature(String name, boolean value)
  109. throws SAXNotRecognizedException, SAXNotSupportedException {
  110. if ("http://xml.org/sax/features/namespaces".equals(name)) {
  111. if (!value) {
  112. throw new SAXNotSupportedException(
  113. "Feature '" + name + "' with value " + value
  114. + " is not supported.");
  115. }
  116. return;
  117. }
  118. if ("http://xml.org/sax/features/namespace-prefixes".equals(name)) {
  119. if (value) {
  120. throw new SAXNotSupportedException(
  121. "Feature '" + name + "' with value " + value
  122. + " is not supported.");
  123. }
  124. return;
  125. }
  126. throw new SAXNotRecognizedException();
  127. }
  128. public Object getProperty(String name)
  129. throws SAXNotRecognizedException, SAXNotSupportedException {
  130. return null;
  131. }
  132. public void setProperty(String name, Object value)
  133. throws SAXNotRecognizedException, SAXNotSupportedException {
  134. }
  135. public void setEntityResolver(EntityResolver resolver) {
  136. mEntityResolver = resolver;
  137. }
  138. public EntityResolver getEntityResolver() {
  139. return mEntityResolver;
  140. }
  141. public void setDTDHandler(DTDHandler handler) {
  142. mDTDHandler = handler;
  143. }
  144. public DTDHandler getDTDHandler() {
  145. return mDTDHandler;
  146. }
  147. public void setContentHandler(ContentHandler handler) {
  148. mContentHandler = handler;
  149. }
  150. public ContentHandler getContentHandler() {
  151. return mContentHandler;
  152. }
  153. public void setErrorHandler(ErrorHandler handler) {
  154. mErrorHandler = handler;
  155. }
  156. public ErrorHandler getErrorHandler() {
  157. return mErrorHandler;
  158. }
  159. public void parse(InputSource input) throws IOException, SAXException {
  160. if (mContentHandler == null) {
  161. throw new NullPointerException("Missing content handler.");
  162. }
  163. mInStream = input.getByteStream();
  164. if (mInStream == null) {
  165. throw new NullPointerException("No input.");
  166. }
  167. if (!mInStream.markSupported()) {
  168. mInStream = new BufferedInputStream(mInStream);
  169. }
  170. // start XML document
  171. mContentHandler.startDocument();
  172. mContentHandler.startPrefixMapping("xsi",
  173. XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
  174. // parse
  175. parse(mTopRule, mInStream);
  176. // end XML document
  177. mContentHandler.endPrefixMapping("xsi");
  178. mContentHandler.endDocument();
  179. }
  180. public void parse(String systemId) throws IOException, SAXException {
  181. parse(new InputSource(systemId));
  182. }
  183. private void parse(RuleNode rule, InputStream inStream)
  184. throws IOException, SAXException {
  185. if (mLog.isLoggable(Level.FINEST)) {
  186. mLog.finest("Parse node '" + rule.toString() + "'");
  187. } else if (mLog.isLoggable(Level.FINE)) {
  188. mLog.fine("Parse node '" + rule.getQName().getLocalPart() + "'");
  189. }
  190. // check on occursDependOn
  191. RuleNode occursDependOn = rule.getOccursDependOn();
  192. int occurs = rule.getMaxOccurs();
  193. if (occursDependOn != null) {
  194. if (mOccursDependOn[occursDependOn.getOccursDependOnIndex()] == null) {
  195. throwException("Occurs Depending On field has not been evaluated: "
  196. + occursDependOn.getQName(), null);
  197. }
  198. occurs = mOccursDependOn[occursDependOn.getOccursDependOnIndex()];
  199. if (mLog.isLoggable(Level.FINEST)) {
  200. mLog.finest("Based on dependency, find maxOccurs=" + occurs);
  201. }
  202. }
  203. // check on redefine index
  204. int redefineIndex = rule.getRedefineIndex();
  205. if (redefineIndex >= 0) {
  206. int size = rule.getCharacteristics().getSize();
  207. mInStream.mark(size + 1);
  208. byte[] bytes = new byte[size];
  209. mInStream.read(bytes);
  210. mInStream.reset();
  211. mRedefined[redefineIndex] = bytes;
  212. }
  213. // check on redefineNode
  214. RuleNode redefineNode = rule.getRedefinedNode();
  215. InputStream currentStream = inStream;
  216. if (redefineNode != null) {
  217. if (mRedefined[redefineNode.getRedefineIndex()] == null) {
  218. throwException("Redefined field has not been evaluated: "
  219. + redefineNode.getQName(), null);
  220. }
  221. currentStream =
  222. new BufferedInputStream(
  223. new ByteArrayInputStream(
  224. mRedefined[redefineNode.getRedefineIndex()]));
  225. }
  226. while (occurs > 0) {
  227. Attributes attrs = mEmptyAttributes;
  228. if (rule.isTop()) {
  229. attrs = new SchemaLocationAttributes(
  230. rule.getQName().getNamespaceURI(),
  231. mSchemaLocation);
  232. }
  233. mContentHandler.startElement(
  234. rule.getQName().getNamespaceURI(),
  235. rule.getQName().getLocalPart(),
  236. rule.getQName().getLocalPart(),
  237. attrs);
  238. RuleNode[] children = rule.getChildren();
  239. if (children != null && children.length > 0) {
  240. for (int i = 0; i < children.length; i++) {
  241. if (mLog.isLoggable(Level.FINEST)) {
  242. mLog.finest("Process child node[" + i + "] of " + rule);
  243. } else if (mLog.isLoggable(Level.FINE)) {
  244. mLog.fine("Process child node[" + i + "] of "
  245. + rule.getQName().getLocalPart());
  246. }
  247. parse(children[i], currentStream);
  248. }
  249. } else {
  250. String data;
  251. int usage = rule.getCharacteristics().getUsage();
  252. if (mLog.isLoggable(Level.FINEST)) {
  253. mLog.finest("It's a leaf node '" + rule.getQName().getLocalPart()
  254. + "' with usage-category=" + rule.getCharacteristics().descUsageCategory());
  255. }
  256. switch (usage) {
  257. case CobolCharacteristics.USAGE_COMP1:
  258. data = String.valueOf(
  259. CobolDataConverter.decodeToFloat(currentStream));
  260. break;
  261. case CobolCharacteristics.USAGE_COMP2:
  262. data = String.valueOf(
  263. CobolDataConverter.decodeToDouble(currentStream));
  264. break;
  265. case CobolCharacteristics.USAGE_BINARY:
  266. case CobolCharacteristics.USAGE_COMP:
  267. case CobolCharacteristics.USAGE_COMP3:
  268. case CobolCharacteristics.USAGE_COMP4:
  269. case CobolCharacteristics.USAGE_COMP5:
  270. case CobolCharacteristics.USAGE_PACKED:
  271. case CobolCharacteristics.USAGE_INDEX:
  272. data = CobolDataConverter.decodeToBigDecimal(
  273. currentStream,
  274. rule.getPicture(),
  275. rule.getCharacteristics(),
  276. rule.getCharEncoding()).toString();
  277. break;
  278. case CobolCharacteristics.USAGE_DISPLAY:
  279. case CobolCharacteristics.USAGE_DISPLAY1:
  280. switch (rule.getCharacteristics().getPicCategory()) {
  281. case CobolCharacteristics.PIC_EXFLOAT:
  282. case CobolCharacteristics.PIC_NUM:
  283. data = CobolDataConverter.decodeToBigDecimal(
  284. currentStream,
  285. rule.getPicture(),
  286. rule.getCharacteristics(),
  287. rule.getCharEncoding()).toString();
  288. break;
  289. case CobolCharacteristics.PIC_ALPHA:
  290. case CobolCharacteristics.PIC_ALPHANUM:
  291. case CobolCharacteristics.PIC_ALPHANUME:
  292. case CobolCharacteristics.PIC_NUME:
  293. case CobolCharacteristics.PIC_DBCS:
  294. data = CobolDataConverter.decodeToString(
  295. currentStream,
  296. rule.getPicture(),
  297. rule.getCharacteristics(),
  298. rule.getCharEncoding());
  299. break;
  300. default:
  301. Message msg = MessageCatalog.getMessage("CCCB4113");
  302. String err =
  303. msg.formatText(
  304. new Object[]{rule.getQName(),
  305. rule.getCharacteristics().getUsage()});
  306. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  307. throw new SAXException(err);
  308. }
  309. break;
  310. default:
  311. Message msg = MessageCatalog.getMessage("CCCB4112");
  312. String err = msg.formatText(new Object[]{rule.getQName(),
  313. rule.getCharacteristics().getUsage()});
  314. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  315. throw new SAXException(err);
  316. }
  317. // XML characters
  318. if (mLog.isLoggable(Level.FINEST)) {
  319. mLog.finest("Data parsed for node '" + rule.getQName().getLocalPart()
  320. + "'=" + Misc.printable(data));
  321. }
  322. char[] chars = new char[data.length()];
  323. data.getChars(0, data.length(), chars, 0);
  324. mContentHandler.characters(chars, 0, chars.length);
  325. final int dependOnIndex = rule.getOccursDependOnIndex();
  326. if (dependOnIndex >= 0) {
  327. mOccursDependOn[dependOnIndex] = new Integer(data);
  328. }
  329. }
  330. // end element
  331. mContentHandler.endElement(
  332. rule.getQName().getNamespaceURI(),
  333. rule.getQName().getLocalPart(),
  334. rule.getQName().getLocalPart());
  335. occurs--;
  336. }
  337. }
  338. private void throwException(String msg, Exception cause)
  339. throws SAXException {
  340. throw new SAXParseException(msg, new LocatorImpl(), cause);
  341. }
  342. private class LocatorImpl implements Locator {
  343. public String getPublicId() {
  344. return mPublicId;
  345. }
  346. public String getSystemId() {
  347. return mSystemId;
  348. }
  349. public int getLineNumber() {
  350. //COBOL Copybook data is a stream of bytes. Assume the line number
  351. //is always one.
  352. return 1;
  353. }
  354. public int getColumnNumber() {
  355. return (int) mBytesRead;
  356. }
  357. }
  358. }