PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/pymma/openesb-components
Java | 222 lines | 136 code | 16 blank | 70 comment | 19 complexity | e1dca6fb980c4fcfea8c933b764731d1 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. * @(#)CocoEncoderProvider.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.File;
  31. import java.io.IOException;
  32. import java.net.URL;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.HashSet;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Set;
  40. import javax.xml.namespace.QName;
  41. import javax.xml.transform.URIResolver;
  42. import org.apache.xmlbeans.SchemaGlobalElement;
  43. import org.apache.xmlbeans.SchemaTypeLoader;
  44. import org.apache.xmlbeans.SchemaTypeSystem;
  45. import org.apache.xmlbeans.XmlBeans;
  46. import org.apache.xmlbeans.XmlException;
  47. import org.apache.xmlbeans.XmlObject;
  48. import org.apache.xmlbeans.XmlOptions;
  49. import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
  50. import com.sun.encoder.DataNature;
  51. import com.sun.encoder.Encoder;
  52. import com.sun.encoder.EncoderConfigurationException;
  53. import com.sun.encoder.EncoderProperties;
  54. import com.sun.encoder.EncoderProvider;
  55. import com.sun.encoder.EncoderType;
  56. import com.sun.encoder.MetaRef;
  57. /**
  58. * Implementation of encoder provider for COBOL Copybook.
  59. *
  60. * @author Jun Xu
  61. * @since 6.0
  62. * @version $Revision: 1.3 $
  63. */
  64. public class CocoEncoderProvider implements EncoderProvider {
  65. public static final String STYLE_ID = "cocoencoder-1.0";
  66. private EncoderType mEncoderType;
  67. public String getIdentification() {
  68. return STYLE_ID;
  69. }
  70. /**
  71. * Instantiates a COBOL Copybook encoder.
  72. *
  73. * @see com.sun.encoder.EncoderProvider#newEncoder(com.sun.encoder.MetaRef,
  74. * javax.xml.transform.URIResolver)
  75. */
  76. public Encoder newEncoder(MetaRef metaRef, URIResolver uriResolver)
  77. throws EncoderConfigurationException {
  78. return newEncoder(metaRef, uriResolver, new EncoderProperties());
  79. }
  80. /**
  81. * Instantiates a collection of COBOL Copybook encoders.
  82. *
  83. * @see com.sun.encoder.EncoderProvider#newEncoders(
  84. * java.util.Set, javax.xml.transform.URIResolver)
  85. */
  86. public Map newEncoders(Set metaRefs, URIResolver uriResolver)
  87. throws EncoderConfigurationException {
  88. return newEncoders(metaRefs, uriResolver, new EncoderProperties());
  89. }
  90. public DataNature getDataNature() {
  91. return DataNature.BYTE_BASED;
  92. }
  93. /**
  94. * Instantiates a COBOL Copybook encoder with additional properties.
  95. *
  96. * @see com.sun.encoder.EncoderProvider#newEncoder(com.sun.encoder.MetaRef,
  97. * javax.xml.transform.URIResolver, com.sun.encoder.EncoderProperties)
  98. */
  99. public Encoder newEncoder(MetaRef metaRef, URIResolver uriResolver,
  100. EncoderProperties properties)
  101. throws EncoderConfigurationException {
  102. CocoEncoder encoder = new CocoEncoder(mEncoderType, properties);
  103. encoder.setMeta(metaRef);
  104. return encoder;
  105. }
  106. /**
  107. * Instantiates a collection of COBOL Copybook encoders with additional
  108. * encoder properties.
  109. *
  110. * @see com.sun.encoder.EncoderProvider#newEncoders(
  111. * java.util.Set, javax.xml.transform.URIResolver,
  112. * com.sun.encoder.EncoderProperties)
  113. */
  114. public Map newEncoders(Set metaRefs, URIResolver uriResolver,
  115. EncoderProperties properties)
  116. throws EncoderConfigurationException {
  117. if (metaRefs == null || metaRefs.isEmpty()) {
  118. throw new EncoderConfigurationException("No metadata references.");
  119. }
  120. Set<String> pathSet = new HashSet<String>();
  121. Set<URL> urlSet = new HashSet<URL>();
  122. List<XmlObject> xmlObjectList = new ArrayList<XmlObject>();
  123. try {
  124. XmlOptions options = new XmlOptions();
  125. options.put(XmlOptions.COMPILE_DOWNLOAD_URLS, Boolean.TRUE);
  126. for (Iterator iter = metaRefs.iterator(); iter.hasNext();) {
  127. MetaRef metaRef = (MetaRef) iter.next();
  128. if (metaRef.getURL() != null) {
  129. if (!urlSet.contains(metaRef.getURL())) {
  130. xmlObjectList.add(
  131. SchemaDocument.Factory.parse(
  132. metaRef.getURL(), options));
  133. urlSet.add(metaRef.getURL());
  134. }
  135. } else {
  136. if (!pathSet.contains(metaRef.getPath())) {
  137. xmlObjectList.add(
  138. SchemaDocument.Factory.parse(
  139. new File(metaRef.getPath())));
  140. pathSet.add(metaRef.getPath());
  141. }
  142. }
  143. }
  144. XmlObject[] xmlObjects = xmlObjectList.toArray(new XmlObject[0]);
  145. SchemaTypeSystem schemaTS;
  146. if (urlSet.size() > 0) {
  147. schemaTS =
  148. XmlBeans.compileXsd(xmlObjects,
  149. XmlBeans.getContextTypeLoader(), options);
  150. } else {
  151. schemaTS =
  152. XmlBeans.compileXsd(xmlObjects,
  153. XmlBeans.getContextTypeLoader(), null);
  154. }
  155. SchemaTypeLoader typeLoader =
  156. XmlBeans.typeLoaderUnion(
  157. new SchemaTypeLoader[]{XmlBeans.getContextTypeLoader(),
  158. schemaTS});
  159. Map<MetaRef, Encoder> encoderMap = new HashMap<MetaRef, Encoder>();
  160. for (Iterator iter = metaRefs.iterator(); iter.hasNext();) {
  161. MetaRef metaRef = (MetaRef) iter.next();
  162. QName qName = metaRef.getRootElemName();
  163. if (qName == null) {
  164. throw new EncoderConfigurationException(
  165. "Root element name cannot be null. path='"
  166. + metaRef.getPath() + "'");
  167. }
  168. SchemaGlobalElement element = typeLoader.findElement(
  169. metaRef.getRootElemName());
  170. if (element == null) {
  171. throw new EncoderConfigurationException(
  172. "Unable to find global element '"
  173. + metaRef.getRootElemName() + "'");
  174. }
  175. CocoEncoder encoder = new CocoEncoder(mEncoderType, properties);
  176. if (metaRef.getURL() != null) {
  177. encoder.setMeta(metaRef.getURL(), element);
  178. } else {
  179. encoder.setMeta(
  180. new File(metaRef.getPath()).toURL(), element);
  181. }
  182. encoderMap.put(metaRef, encoder);
  183. }
  184. return encoderMap;
  185. } catch (XmlException e) {
  186. throw new EncoderConfigurationException(e);
  187. } catch (IOException e) {
  188. throw new EncoderConfigurationException(e);
  189. }
  190. }
  191. /**
  192. * Sets the encoder type from the encoder framework. Just save it and pass
  193. * it to all encoder instances created from this provider.
  194. */
  195. public void setType(EncoderType encoderType) {
  196. mEncoderType = encoderType;
  197. }
  198. /**
  199. * Gets the aliases of the encoder type.
  200. */
  201. public String[] getAliases() {
  202. //Not using aliases for now. Should aliases be needed, just return
  203. //them in a string array here.
  204. return null;
  205. }
  206. }