PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pymma/openesb-components
Java | 422 lines | 351 code | 35 blank | 36 comment | 66 complexity | cb7f3905a08dc08f2ab7d7f34e44a787 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. * @(#)CocoEncoder.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;
  30. import java.io.BufferedOutputStream;
  31. import java.io.ByteArrayInputStream;
  32. import java.io.ByteArrayOutputStream;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.OutputStream;
  37. import java.io.Reader;
  38. import java.io.StringReader;
  39. import java.io.StringWriter;
  40. import java.io.UnsupportedEncodingException;
  41. import java.io.Writer;
  42. import javax.xml.namespace.QName;
  43. import javax.xml.transform.Source;
  44. import javax.xml.transform.Transformer;
  45. import javax.xml.transform.TransformerConfigurationException;
  46. import javax.xml.transform.TransformerException;
  47. import javax.xml.transform.TransformerFactory;
  48. import javax.xml.transform.TransformerFactoryConfigurationError;
  49. import javax.xml.transform.sax.SAXResult;
  50. import javax.xml.transform.sax.SAXSource;
  51. import org.apache.xmlbeans.SchemaGlobalElement;
  52. import org.apache.xmlbeans.SchemaTypeSystem;
  53. import org.apache.xmlbeans.XmlBeans;
  54. import org.apache.xmlbeans.XmlException;
  55. import org.apache.xmlbeans.XmlObject;
  56. import org.apache.xmlbeans.XmlOptions;
  57. import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
  58. import org.xml.sax.InputSource;
  59. import com.sun.encoder.Encoder;
  60. import com.sun.encoder.EncoderConfigurationException;
  61. import com.sun.encoder.EncoderException;
  62. import com.sun.encoder.EncoderProperties;
  63. import com.sun.encoder.EncoderType;
  64. import com.sun.encoder.MetaRef;
  65. import com.sun.encoder.coco.runtime.MarshalHandler;
  66. import com.sun.encoder.coco.runtime.RuleNode;
  67. import com.sun.encoder.coco.runtime.UnmarshalAdaptor;
  68. import com.sun.encoder.runtime.ReaderInputStream;
  69. import com.sun.encoder.runtime.WriterOutputStream;
  70. import com.sun.encoder.runtime.provider.Misc;
  71. import java.net.URL;
  72. import java.util.logging.Level;
  73. import java.util.logging.Logger;
  74. /**
  75. * Encoder implementation for COBOL Copybook.
  76. *
  77. * @author Jun Xu
  78. * @since 6.0
  79. */
  80. class CocoEncoder implements Encoder {
  81. private final EncoderType mType; //Should never be null
  82. private final EncoderProperties mProperties; //Should never be null
  83. //but might be empty
  84. private Transformer mTransformer;
  85. private RuleNode mTopRule;
  86. private URL mSchemaLocation;
  87. /** Logger object.*/
  88. private Logger mLog = Logger.getLogger(getClass().getName());
  89. CocoEncoder(EncoderType type, EncoderProperties properties) {
  90. mType = type;
  91. if (properties.immutable()) {
  92. mProperties = properties;
  93. } else {
  94. mProperties = properties.cloneImmutable();
  95. }
  96. }
  97. public Source decodeFromString(String input) throws EncoderException {
  98. return decodeFromString(input, null);
  99. }
  100. public String encodeToString(Source input) throws EncoderException {
  101. return encodeToString(input, null);
  102. }
  103. public Source decodeFromBytes(byte[] input) throws EncoderException {
  104. return decodeFromBytes(input, null);
  105. }
  106. public byte[] encodeToBytes(Source xmlSource) throws EncoderException {
  107. return encodeToBytes(xmlSource, null);
  108. }
  109. public Source decodeFromBytes(byte[] input, EncoderProperties properties)
  110. throws EncoderException {
  111. if (mLog.isLoggable(Level.FINE)) {
  112. StringBuffer buf = new StringBuffer("Decode from input byte array");
  113. buf.append(", total bytes=").append(input.length);
  114. if (mLog.isLoggable(Level.FINER)) {
  115. buf.append(", encoderProperties=[");
  116. buf.append(properties == null ? "null" : properties);
  117. buf.append("]");
  118. }
  119. if (mLog.isLoggable(Level.FINEST)) {
  120. buf.append(", input bytes=[").append(Misc.printable(input));
  121. buf.append("]");
  122. }
  123. buf.append(".");
  124. if (mLog.isLoggable(Level.FINEST)) {
  125. mLog.finest(buf.toString());
  126. } else if (mLog.isLoggable(Level.FINER)) {
  127. mLog.finer(buf.toString());
  128. } else if (mLog.isLoggable(Level.FINE)) {
  129. mLog.fine(buf.toString());
  130. }
  131. }
  132. return decodeFromStream(new ByteArrayInputStream(input), properties);
  133. }
  134. public Source decodeFromReader(Reader input) throws EncoderException {
  135. return decodeFromReader(input, null);
  136. }
  137. public Source decodeFromReader(Reader reader, EncoderProperties properties)
  138. throws EncoderException {
  139. if (mLog.isLoggable(Level.FINE)) {
  140. StringBuffer buf = new StringBuffer("Decode from reader=");
  141. buf.append(reader.getClass().getName());
  142. if (mLog.isLoggable(Level.FINER)) {
  143. buf.append(", encoderProperties=[");
  144. buf.append(properties == null ? "null" : properties);
  145. buf.append("]");
  146. }
  147. buf.append(".");
  148. if (mLog.isLoggable(Level.FINER)) {
  149. mLog.finer(buf.toString());
  150. } else if (mLog.isLoggable(Level.FINE)) {
  151. mLog.fine(buf.toString());
  152. }
  153. }
  154. String charCoding = null;
  155. if (properties != null) {
  156. charCoding = properties.getPreDecodeCharCoding();
  157. }
  158. if (charCoding == null) {
  159. charCoding = mProperties.getPreDecodeCharCoding();
  160. }
  161. if (charCoding == null) {
  162. charCoding = mTopRule.getContext().getPreDecodeCharCoding();
  163. }
  164. if (charCoding == null) {
  165. throw new EncoderException(
  166. "The data nature of the COBOL Copybook encoder type is byte "
  167. + "based but the pre-decoding character coding is not set "
  168. + "for this encoder. Don't know how to convert character "
  169. + "based data into byte based data.");
  170. } else {
  171. if (mLog.isLoggable(Level.FINE)) {
  172. mLog.fine("Use charCoding=" + charCoding);
  173. }
  174. }
  175. ReaderInputStream wrapper;
  176. try {
  177. wrapper = new ReaderInputStream(reader, charCoding);
  178. return decodeFromStream(wrapper, properties);
  179. } catch (UnsupportedEncodingException e) {
  180. throw new EncoderException(e);
  181. }
  182. }
  183. public Source decodeFromStream(InputStream input) throws EncoderException {
  184. return decodeFromStream(input, null);
  185. }
  186. public Source decodeFromStream(InputStream input,
  187. EncoderProperties properties) throws EncoderException {
  188. InputSource inputSource = new InputSource(input);
  189. inputSource.setPublicId("byte sequence data");
  190. UnmarshalAdaptor adaptor =
  191. new UnmarshalAdaptor(mSchemaLocation, mTopRule);
  192. return new SAXSource(adaptor, inputSource);
  193. }
  194. public Source decodeFromString(String input, EncoderProperties properties)
  195. throws EncoderException {
  196. return decodeFromReader(new StringReader(input), properties);
  197. }
  198. public byte[] encodeToBytes(Source input, EncoderProperties properties)
  199. throws EncoderException {
  200. if (mLog.isLoggable(Level.FINE)) {
  201. StringBuffer buf = new StringBuffer("Encode to bytes from source=");
  202. buf.append(input.getClass().getName());
  203. if (mLog.isLoggable(Level.FINER)) {
  204. buf.append(", encoderProperties=[");
  205. buf.append(properties == null ? "null" : properties);
  206. buf.append("]");
  207. }
  208. buf.append(".");
  209. if (mLog.isLoggable(Level.FINER)) {
  210. mLog.finer(buf.toString());
  211. } else if (mLog.isLoggable(Level.FINE)) {
  212. mLog.fine(buf.toString());
  213. }
  214. }
  215. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  216. encodeToStream(input,
  217. new BufferedOutputStream(outStream), properties);
  218. byte[] output = outStream.toByteArray();
  219. if (mLog.isLoggable(Level.FINE)) {
  220. StringBuffer buf = new StringBuffer();
  221. buf.append("Before postcoding, output byte size=");
  222. buf.append(output.length);
  223. if (mLog.isLoggable(Level.FINEST)) {
  224. buf.append(", output bytes=[");
  225. buf.append(Misc.printable(output)).append("]");
  226. }
  227. buf.append(".");
  228. if (mLog.isLoggable(Level.FINEST)) {
  229. mLog.finest(buf.toString());
  230. } else if (mLog.isLoggable(Level.FINE)) {
  231. mLog.fine(buf.toString());
  232. }
  233. }
  234. return output;
  235. }
  236. public void encodeToStream(Source input, OutputStream output)
  237. throws EncoderException {
  238. encodeToStream(input, output, null);
  239. }
  240. public void encodeToStream(Source input, OutputStream output,
  241. EncoderProperties properties) throws EncoderException {
  242. SAXResult result = new SAXResult(
  243. new MarshalHandler(mTopRule, output));
  244. try {
  245. mTransformer.transform(input, result);
  246. output.flush();
  247. } catch (TransformerException e) {
  248. throw new EncoderException(e);
  249. } catch (IOException e) {
  250. throw new EncoderException(e);
  251. }
  252. }
  253. public String encodeToString(Source input, EncoderProperties properties)
  254. throws EncoderException {
  255. Writer writer = new StringWriter();
  256. encodeToWriter(input, writer, properties);
  257. return writer.toString();
  258. }
  259. public void encodeToWriter(Source input, Writer output)
  260. throws EncoderException {
  261. encodeToWriter(input, output, null);
  262. }
  263. public void encodeToWriter(Source input, Writer output,
  264. EncoderProperties properties) throws EncoderException {
  265. if (mLog.isLoggable(Level.FINE)) {
  266. StringBuffer buf = new StringBuffer("Encode to writer from source=");
  267. buf.append(input.getClass().getName());
  268. if (mLog.isLoggable(Level.FINER)) {
  269. buf.append(", encoderProperties=[");
  270. buf.append(properties == null ? "null" : properties);
  271. buf.append("]");
  272. }
  273. buf.append(".");
  274. if (mLog.isLoggable(Level.FINER)) {
  275. mLog.finer(buf.toString());
  276. } else if (mLog.isLoggable(Level.FINE)) {
  277. mLog.fine(buf.toString());
  278. }
  279. }
  280. String charCoding = null;
  281. if (properties != null) {
  282. charCoding = properties.getPostEncodeCharCoding();
  283. }
  284. if (charCoding == null) {
  285. charCoding = mProperties.getPostEncodeCharCoding();
  286. }
  287. if (charCoding == null) {
  288. charCoding = mTopRule.getContext().getPostEncodeCharCoding();
  289. }
  290. if (charCoding == null) {
  291. throw new EncoderException(
  292. "The data nature of the COBOL Copybook encoder type is byte "
  293. + "based but the post-encoding character coding is not set "
  294. + "for this encoder. Don't know how to convert byte based "
  295. + "data into character based data.");
  296. } else {
  297. if (mLog.isLoggable(Level.FINE)) {
  298. mLog.fine("Use charCoding=" + charCoding);
  299. }
  300. }
  301. WriterOutputStream wrapper;
  302. try {
  303. wrapper = new WriterOutputStream(output, charCoding);
  304. encodeToStream(input, wrapper, properties);
  305. wrapper.flushToWriter(true);
  306. } catch (IOException e) {
  307. throw new EncoderException(e);
  308. }
  309. }
  310. public boolean dispose() {
  311. mTopRule = null;
  312. return true;
  313. }
  314. public EncoderProperties getProperties() {
  315. return mProperties;
  316. }
  317. public EncoderType getType() {
  318. return mType;
  319. }
  320. void setMeta(MetaRef xsd) throws EncoderConfigurationException {
  321. try {
  322. XmlObject schemaXmlObj;
  323. QName qName;
  324. XmlOptions options;
  325. SchemaTypeSystem schemaTS;
  326. if (xsd.getURL() != null) {
  327. options = new XmlOptions();
  328. options.put(XmlOptions.COMPILE_DOWNLOAD_URLS, Boolean.TRUE);
  329. schemaXmlObj = SchemaDocument.Factory.parse(xsd.getURL());
  330. schemaTS =
  331. XmlBeans.compileXsd(new XmlObject[]{schemaXmlObj},
  332. SchemaDocument.type.getTypeSystem(), options);
  333. } else {
  334. schemaXmlObj =
  335. SchemaDocument.Factory.parse(
  336. new File(xsd.getPath()));
  337. schemaXmlObj.validate();
  338. schemaTS =
  339. XmlBeans.compileXsd(new XmlObject[]{schemaXmlObj},
  340. SchemaDocument.type.getTypeSystem(), null);
  341. }
  342. qName = xsd.getRootElemName();
  343. if (qName == null) {
  344. throw new EncoderConfigurationException(
  345. "Root element name cannot be null. path='"
  346. + xsd.getPath() + "'");
  347. }
  348. mTopRule = RuleNode.readRules(schemaTS, qName);
  349. if (mTopRule == null) {
  350. throw new EncoderConfigurationException(
  351. "Unable to read metadata for '" + qName + "'");
  352. }
  353. mTransformer = TransformerFactory.newInstance().newTransformer();
  354. if (xsd.getURL() != null) {
  355. mSchemaLocation = xsd.getURL();
  356. } else {
  357. mSchemaLocation = new File(xsd.getPath()).toURL();
  358. }
  359. } catch (XmlException e) {
  360. throw new EncoderConfigurationException(e);
  361. } catch (IOException e) {
  362. throw new EncoderConfigurationException(e);
  363. } catch (TransformerConfigurationException e) {
  364. throw new EncoderConfigurationException(e);
  365. } catch (TransformerFactoryConfigurationError e) {
  366. throw new EncoderConfigurationException(e);
  367. }
  368. }
  369. void setMeta(URL schemaLocation, SchemaGlobalElement rootElemName)
  370. throws EncoderConfigurationException {
  371. try {
  372. mTopRule = RuleNode.readRules(rootElemName.getTypeSystem(),
  373. rootElemName.getName());
  374. if (mTopRule == null) {
  375. throw new EncoderConfigurationException(
  376. "Unable to read metadata for '"
  377. + rootElemName.getName() + "'");
  378. }
  379. mTransformer = TransformerFactory.newInstance().newTransformer();
  380. mSchemaLocation = schemaLocation;
  381. } catch (TransformerConfigurationException e) {
  382. throw new EncoderConfigurationException(e);
  383. } catch (TransformerFactoryConfigurationError e) {
  384. throw new EncoderConfigurationException(e);
  385. }
  386. }
  387. }