PageRenderTime 24ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pymma/openesb-components
Java | 767 lines | 605 code | 50 blank | 112 comment | 121 complexity | 7f1498e0e88b050f71123532dbeac5d0 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. * @(#)RuleNode.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 java.io.File;
  31. import java.io.IOException;
  32. import java.io.Reader;
  33. import java.io.StringWriter;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import javax.xml.namespace.QName;
  37. import org.apache.xmlbeans.SchemaAnnotation;
  38. import org.apache.xmlbeans.SchemaGlobalElement;
  39. import org.apache.xmlbeans.SchemaLocalElement;
  40. import org.apache.xmlbeans.SchemaParticle;
  41. import org.apache.xmlbeans.SchemaType;
  42. import org.apache.xmlbeans.SchemaTypeSystem;
  43. import org.apache.xmlbeans.XmlBeans;
  44. import org.apache.xmlbeans.XmlCursor;
  45. import org.apache.xmlbeans.XmlException;
  46. import org.apache.xmlbeans.XmlObject;
  47. import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
  48. import com.sun.encoder.EncoderConfigurationException;
  49. import com.sun.encoder.coco.appinfo.CocoEncoding;
  50. import com.sun.encoder.coco.appinfo.CocoEncoding.Usage;
  51. import com.sun.encoder.coco.model.CocoDescriptionEntry;
  52. import com.sun.encoder.coco.model.CocoPicture;
  53. import com.sun.encoder.coco.model.CocoDescriptionEntry.UsageType;
  54. import com.sun.encoder.coco.model.CocoPicture.Category;
  55. import com.sun.encoder.coco.runtime.messages.ErrorManager;
  56. import com.sun.encoder.coco.runtime.messages.Message;
  57. import com.sun.encoder.coco.runtime.messages.MessageCatalog;
  58. import com.sun.encoder.codegen.Emit;
  59. /**
  60. * Node that represents a COBOL Copybook entry. This node class is deemed for
  61. * runtime use (used by COBOL Copybook data parser and marshaler), so it is
  62. * compact and all getters should return pre-calculated values as much as
  63. * possible.
  64. *
  65. * @author Jun Xu
  66. * @since 6.0
  67. */
  68. public class RuleNode {
  69. private static final ErrorManager cErrorMgr =
  70. ErrorManager.getManager("OpenESB.encoder.COBOLCopybook."
  71. + RuleNode.class.getName());
  72. private boolean mSuper = false;
  73. private boolean mTop = false;
  74. private RuleContext mContext;
  75. private RuleNode mParentNode;
  76. /** Child index used to get this node from its parent node. */
  77. private int mIndex;
  78. private QName mQName;
  79. private int mMinOccurs;
  80. private int mMaxOccurs;
  81. private RuleNode mOccursDependOn;
  82. private String mCharEncoding;
  83. /**
  84. * mChildList is only used when initially populating the rule tree. Once
  85. * this is done, only mChildren should be used.
  86. */
  87. private List<RuleNode> mChildList = new ArrayList<RuleNode>();
  88. private RuleNode[] mChildren;
  89. private CobolCharacteristics mCharacteristics;
  90. private String mPicture;
  91. /**
  92. * if the redefine index is greater or equal to zero, then this node has
  93. * been redefined. Redefine index is used to retrieve data from the
  94. * redefined data pool.
  95. */
  96. private int mRedefineIndex = -1;
  97. /**
  98. * if the 'occurs depend' on index is greater or equal to zero, then this
  99. * node has been depended on for occurrence. Occurs Depending On index is
  100. * used to retrieve data from the Occurs Depending On data pool.
  101. */
  102. private int mOccursDependOnIndex = -1;
  103. /**
  104. * If the redefined node is not null, then this node is redefining another
  105. * node
  106. */
  107. private RuleNode mRedefinedNode;
  108. public RuleContext getContext() {
  109. return mContext;
  110. }
  111. public RuleNode getParentNode() {
  112. return mParentNode;
  113. }
  114. public int getIndex() {
  115. return mIndex;
  116. }
  117. public boolean isSuper() {
  118. return mSuper;
  119. }
  120. public boolean isTop() {
  121. return mTop;
  122. }
  123. public QName getQName() {
  124. return mQName;
  125. }
  126. public int getMinOccurs() {
  127. return mMinOccurs;
  128. }
  129. public int getMaxOccurs() {
  130. return mMaxOccurs;
  131. }
  132. public int getOccursDependOnIndex() {
  133. return mOccursDependOnIndex;
  134. }
  135. public RuleNode getOccursDependOn() {
  136. return mOccursDependOn;
  137. }
  138. public String getCharEncoding() {
  139. if (mCharEncoding != null) {
  140. return mCharEncoding;
  141. }
  142. if (mCharacteristics.getPicCategory()
  143. == CobolCharacteristics.PIC_DBCS) {
  144. return mContext.mDisplay1CharEncoding;
  145. }
  146. return mContext.mDisplayCharEncoding;
  147. }
  148. public int getRedefineIndex() {
  149. return mRedefineIndex;
  150. }
  151. public RuleNode getRedefinedNode() {
  152. return mRedefinedNode;
  153. }
  154. public RuleNode[] getChildren() {
  155. return mChildren;
  156. }
  157. public CobolCharacteristics getCharacteristics() {
  158. return mCharacteristics;
  159. }
  160. public String getPicture() {
  161. return mPicture;
  162. }
  163. @Override
  164. public String toString() {
  165. StringBuffer buf = new StringBuffer("RuleNode@")
  166. .append(Integer.toHexString(hashCode()));
  167. buf.append(" name='").append(mQName).append("'");
  168. if (mMinOccurs != -1) {
  169. buf.append(" minOccurs=").append(mMinOccurs);
  170. }
  171. if (mMaxOccurs != -1) {
  172. buf.append(" maxOccurs=").append(mMaxOccurs);
  173. }
  174. if (mCharEncoding != null) {
  175. buf.append(" charEncoding=").append(mCharEncoding);
  176. }
  177. if (mOccursDependOn != null) {
  178. buf.append(" occursDependOn=").append(mOccursDependOn.getQName());
  179. }
  180. if (mOccursDependOnIndex >= 0) {
  181. buf.append(" occursDependOnIndex=").append(mOccursDependOnIndex);
  182. }
  183. if (mRedefinedNode != null) {
  184. buf.append(" redefine=").append(mRedefinedNode.getQName());
  185. }
  186. if (mRedefineIndex >= 0) {
  187. buf.append(" redefineIndex=").append(mRedefineIndex);
  188. }
  189. if (mPicture != null) {
  190. buf.append(" picture=").append(mPicture);
  191. }
  192. if (mCharacteristics != null) {
  193. buf.append(" characteristics=").append(mCharacteristics);
  194. }
  195. if (mChildren != null && mChildren.length > 0) {
  196. buf.append(" children={");
  197. for (int i = 0; i < mChildren.length; i++) {
  198. buf.append(mChildren[i].getQName());
  199. if (i < mChildren.length -1) {
  200. buf.append(", ");
  201. }
  202. }
  203. buf.append("}]");
  204. }
  205. return buf.toString();
  206. }
  207. public void dump(Emit emit) {
  208. emit.part("(name=");
  209. emitValue(emit, mQName);
  210. emit.down(" ");
  211. emit.indent();
  212. emit.part("minOccurs=");
  213. emitValue(emit, mMinOccurs);
  214. emit.emit();
  215. emit.part("maxOccurs=");
  216. emitValue(emit, mMaxOccurs);
  217. emit.emit();
  218. emit.part("charEncoding=");
  219. emitValue(emit, mCharEncoding);
  220. emit.emit();
  221. emit.part("occursDependOn=");
  222. emitValue(emit,
  223. mOccursDependOn == null ? "<null>"
  224. : mOccursDependOn.getQName());
  225. emit.emit();
  226. emit.part("occursDependOnIndex=");
  227. emitValue(emit, mOccursDependOnIndex);
  228. emit.emit();
  229. emit.part("redefine=");
  230. emitValue(emit,
  231. mRedefinedNode == null ? "<null>" : mRedefinedNode.getQName());
  232. emit.emit();
  233. emit.part("redefineIndex=");
  234. emitValue(emit, mRedefineIndex);
  235. emit.emit();
  236. emit.part("picture=");
  237. emitValue(emit, mPicture);
  238. emit.emit();
  239. emit.part("characteristics=");
  240. emitValue(emit, mCharacteristics);
  241. emit.emit();
  242. emit.undent();
  243. for (int i = 0; mChildren != null && i < mChildren.length; i++) {
  244. mChildren[i].dump(emit);
  245. }
  246. emit.undent();
  247. emit.emit(")");
  248. }
  249. private void emitValue(Emit emit, Object value) {
  250. emit.part("\"");
  251. emit.part(value == null ? "<null>" : value.toString());
  252. emit.part("\"");
  253. }
  254. /**
  255. * Wraps a top node into a super node. A super node does not
  256. * represent any rule but just acts as a container of the top
  257. * node for facilitating programing logic in some situations.
  258. *
  259. * @param topNode the top node
  260. * @return the super node that contains the top node as its child node
  261. */
  262. public static RuleNode wrapAsSuperNode(RuleNode topNode) {
  263. RuleNode superNode = new RuleNode();
  264. superNode.mSuper = true;
  265. superNode.mQName = new QName("super");
  266. superNode.mChildren = new RuleNode[] {topNode};
  267. superNode.mMaxOccurs = 1;
  268. superNode.mMinOccurs = 1;
  269. topNode.mParentNode = superNode;
  270. topNode.mIndex = 0;
  271. return superNode;
  272. }
  273. /**
  274. * Reads a rule tree from an XML type system (compiled from XSDs) and
  275. * a top element.
  276. *
  277. * @param ts the XML type system
  278. * @param topElemName the qualified name of a top element
  279. * @return the top rule
  280. * @throws EncoderConfigurationException thrown if any configuration error
  281. * occurs (e.g., invalidity in the XSD or its annotation)
  282. */
  283. public static RuleNode readRules(SchemaTypeSystem ts, QName topElemName)
  284. throws EncoderConfigurationException {
  285. SchemaGlobalElement topElem = ts.findElement(topElemName);
  286. if (topElem == null) {
  287. //Global element ''{0}'' not found in the type system
  288. Message msg = MessageCatalog.getMessage("CCCR4033");
  289. String err = msg.formatText(new Object[] {topElemName});
  290. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  291. throw new EncoderConfigurationException(err);
  292. }
  293. RuleNode topRule = new RuleNode();
  294. topRule.mTop = true;
  295. RuleContext context = new RuleContext();
  296. context.mTopRule = topRule;
  297. topRule.mContext = context;
  298. readRule(topRule, topElem);
  299. context.mRedefinedNodes =
  300. context.mRedefinedNodeList.toArray(new RuleNode[0]);
  301. context.mRedefinedNodeList.clear();
  302. context.mOccursDependOnNodes =
  303. context.mOccursDependOnList.toArray(new RuleNode[0]);
  304. context.mOccursDependOnList.clear();
  305. return topRule;
  306. }
  307. /**
  308. * Reads current RuleNode.
  309. * @param currNode
  310. * @param elem
  311. * @throws EncoderConfigurationException
  312. */
  313. private static void readRule(RuleNode currNode,
  314. SchemaLocalElement elem) throws EncoderConfigurationException {
  315. RuleContext context = currNode.getContext();
  316. CocoEncoding encoding = null;
  317. SchemaAnnotation anno = elem.getAnnotation();
  318. if (anno != null) {
  319. XmlObject[] xmlObjs = anno.getApplicationInformation();
  320. for (int i = 0; i < xmlObjs.length; i++) {
  321. if (xmlObjs[i] instanceof CocoEncoding) {
  322. encoding = (CocoEncoding) xmlObjs[i];
  323. break;
  324. }
  325. XmlCursor cursor = xmlObjs[i].newCursor();
  326. String source = cursor.getAttributeText(new QName("source"));
  327. cursor.dispose();
  328. if ("urn:com.sun:encoder".equals(source)) {
  329. Reader reader = xmlObjs[i].newReader();
  330. try {
  331. encoding = CocoEncoding.Factory.parse(reader);
  332. reader.close();
  333. } catch (XmlException e) {
  334. //Missing encoding information
  335. Message msg = MessageCatalog.getMessage("CCCR4035");
  336. String err =
  337. msg.formatText(new Object[] {elem.getName()});
  338. cErrorMgr.log(ErrorManager.Severity.ERROR, null,
  339. err + e.getLocalizedMessage());
  340. throw new EncoderConfigurationException(err, e);
  341. } catch (IOException e) {
  342. //Missing encoding information
  343. Message msg = MessageCatalog.getMessage("CCCR4035");
  344. String err =
  345. msg.formatText(new Object[] {elem.getName()});
  346. cErrorMgr.log(ErrorManager.Severity.ERROR, null,
  347. err + e.getLocalizedMessage());
  348. throw new EncoderConfigurationException(err, e);
  349. }
  350. break;
  351. }
  352. }
  353. }
  354. if (encoding == null) {
  355. //Missing encoding information
  356. Message msg = MessageCatalog.getMessage("CCCR4034");
  357. String err = msg.formatText(new Object[] {elem.getName()});
  358. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  359. throw new EncoderConfigurationException(err);
  360. }
  361. currNode.mQName = elem.getName();
  362. if (elem instanceof SchemaGlobalElement) {
  363. currNode.mMinOccurs = 1;
  364. currNode.mMaxOccurs = 1;
  365. } else {
  366. currNode.mMinOccurs = elem.getMinOccurs().intValue();
  367. currNode.mMaxOccurs =
  368. elem.getMaxOccurs() == null ? -1
  369. : elem.getMaxOccurs().intValue();
  370. }
  371. if (currNode.mTop) {
  372. if (encoding.isSetDisplayCharEncoding()) {
  373. currNode.mContext.mDisplayCharEncoding =
  374. encoding.getDisplayCharEncoding();
  375. }
  376. if (encoding.isSetDisplay1CharEncoding()) {
  377. currNode.mContext.mDisplay1CharEncoding =
  378. encoding.getDisplay1CharEncoding();
  379. }
  380. if (encoding.isSetPreDecodeCharCoding()) {
  381. currNode.mContext.mPreDecodeCharCoding =
  382. encoding.getPreDecodeCharCoding();
  383. }
  384. if (encoding.isSetPostEncodeCharCoding()) {
  385. currNode.mContext.mPostEncodeCharCoding =
  386. encoding.getPostEncodeCharCoding();
  387. }
  388. }
  389. CobolCharacteristics characteristics = new CobolCharacteristics();
  390. currNode.mCharacteristics = characteristics;
  391. characteristics.setBlankWhenZero(encoding.isSetBlankWhenZero());
  392. characteristics.setJustified(encoding.isSetJustified());
  393. if (encoding.isSetRedefine()) {
  394. if (currNode.mTop) {
  395. Message msg = MessageCatalog.getMessage("CCCR4036");
  396. String err = msg.formatText(new Object[] {elem.getName()});
  397. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  398. throw new EncoderConfigurationException(err);
  399. }
  400. RuleNode redefinedNode =
  401. findSibling(currNode, encoding.getRedefine());
  402. if (redefinedNode == null) {
  403. Message msg = MessageCatalog.getMessage("CCCR4037");
  404. String err =
  405. msg.formatText(new Object[] {elem, encoding.getRedefine()});
  406. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  407. throw new EncoderConfigurationException(err);
  408. }
  409. if (redefinedNode.mRedefineIndex == -1) {
  410. redefinedNode.mRedefineIndex =
  411. context.mRedefinedNodeList.size();
  412. context.mRedefinedNodeList.add(redefinedNode);
  413. }
  414. currNode.mRedefinedNode = redefinedNode;
  415. }
  416. if (encoding.isSetOccursDependOn()) {
  417. RuleNode dependedNode =
  418. findNode(context.mTopRule,
  419. encoding.getOccursDependOn().getPath());
  420. if (dependedNode == null) {
  421. Message msg = MessageCatalog.getMessage("CCCR4038");
  422. String err =
  423. msg.formatText(new Object[]{
  424. elem, encoding.getOccursDependOn().getPath()});
  425. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  426. throw new EncoderConfigurationException(err);
  427. }
  428. if (dependedNode.mOccursDependOnIndex == -1) {
  429. dependedNode.mOccursDependOnIndex =
  430. context.mOccursDependOnList.size();
  431. context.mOccursDependOnList.add(dependedNode);
  432. }
  433. currNode.mOccursDependOn = dependedNode;
  434. }
  435. CocoPicture picture;
  436. if (encoding.isSetPicture()) {
  437. try {
  438. picture = new CocoPicture(encoding.getPicture());
  439. } catch (IllegalArgumentException e) {
  440. Message msg = MessageCatalog.getMessage("CCCR4039");
  441. String err =
  442. msg.formatText(new Object[] {elem, encoding.getPicture()});
  443. cErrorMgr.log(ErrorManager.Severity.ERROR, null,
  444. err + e.getLocalizedMessage());
  445. throw new EncoderConfigurationException(err, e);
  446. }
  447. characteristics.setDecimalPosition(picture.getDecimalPosition());
  448. characteristics.setDecimalScalingPositions(
  449. picture.getDecimalScalingPositions());
  450. characteristics.setSigned(picture.hasSign());
  451. characteristics.setPicCategory(
  452. cvtPictureCategory(picture.getCategory()));
  453. currNode.mPicture = picture.getPicture();
  454. } else {
  455. picture = null;
  456. }
  457. if (encoding.isSetUsage()) {
  458. characteristics.setUsage(cvtUsage(encoding.getUsage()));
  459. }
  460. characteristics.setSignLeading(characteristics.isSigned()
  461. && encoding.isSetSign() && encoding.getSign().isSetLeading());
  462. characteristics.setSignSeparate(characteristics.isSigned()
  463. && encoding.isSetSign() && encoding.getSign().isSetSeparate());
  464. SchemaType xmlType = elem.getType();
  465. if (xmlType == null || xmlType.isURType()) {
  466. Message msg = MessageCatalog.getMessage("CCCR4040");
  467. String err = msg.formatText(new Object[] {elem.getName()});
  468. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  469. throw new EncoderConfigurationException(err);
  470. }
  471. if (xmlType.isSimpleType()) {
  472. characteristics.setSize(
  473. CocoDescriptionEntry.computeElementarySize(
  474. picture,
  475. cvtUsage(characteristics.getUsage()),
  476. characteristics.isSignSeparate()));
  477. } else {
  478. SchemaParticle model = xmlType.getContentModel();
  479. if (model == null
  480. || model.getParticleType() == SchemaParticle.WILDCARD
  481. || (model.getParticleType() != SchemaParticle.ELEMENT
  482. && model.countOfParticleChild() == 0)) {
  483. Message msg = MessageCatalog.getMessage("CCCR4041");
  484. String err = msg.formatText(new Object[] {elem.getName()});
  485. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  486. throw new EncoderConfigurationException(err);
  487. }
  488. SchemaParticle[] parts;
  489. if (model.getParticleType() == SchemaParticle.ELEMENT) {
  490. parts = new SchemaParticle[]{model};
  491. } else {
  492. parts = xmlType.getContentModel().getParticleChildren();
  493. }
  494. int size = 0;
  495. for (int i = 0; i < parts.length; i++) {
  496. if (!(parts[i] instanceof SchemaLocalElement)) {
  497. Message msg = MessageCatalog.getMessage("CCCR4042");
  498. String err =
  499. msg.formatText(new Object[] {elem, new Integer(i)});
  500. cErrorMgr.log(ErrorManager.Severity.ERROR, null, err);
  501. throw new EncoderConfigurationException(err);
  502. }
  503. RuleNode childNode = new RuleNode();
  504. currNode.mChildList.add(childNode);
  505. childNode.mParentNode = currNode;
  506. childNode.mIndex = i;
  507. childNode.mContext = context;
  508. readRule(childNode, (SchemaLocalElement) parts[i]);
  509. int occurs;
  510. if (childNode.getOccursDependOn() != null) {
  511. //Same logic in CocoDescriptionEntry's getSize() method
  512. occurs = 1;
  513. } else {
  514. occurs = childNode.getMaxOccurs();
  515. }
  516. size += (occurs * childNode.getCharacteristics().getSize());
  517. }
  518. characteristics.setSize(size);
  519. currNode.mChildren = currNode.mChildList.toArray(new RuleNode[0]);
  520. }
  521. }
  522. private static RuleNode findSibling(RuleNode current, String localName) {
  523. if (current.mParentNode.mChildList == null) {
  524. return null;
  525. }
  526. for (RuleNode node : current.mParentNode.mChildList) {
  527. if (node.getQName().getLocalPart().equals(localName)) {
  528. return node;
  529. }
  530. }
  531. return null;
  532. }
  533. private static RuleNode findNode(RuleNode top, String path) {
  534. if (path == null || path.length() == 0) {
  535. return top;
  536. }
  537. String[] steps = path.split("/");
  538. RuleNode current = top;
  539. RuleNode found = null;
  540. for (int i = 0; i < steps.length; i++) {
  541. found = null;
  542. if (current.mChildList == null) {
  543. break;
  544. }
  545. for (RuleNode node : current.mChildList) {
  546. if (steps[i].equals(node.getQName().getLocalPart())) {
  547. found = node;
  548. break;
  549. }
  550. }
  551. if (found != null) {
  552. current = found;
  553. } else {
  554. break;
  555. }
  556. }
  557. return found;
  558. }
  559. /**
  560. * Converts picture category enumeration value from
  561. * <code>CocoPicture</code>'s definition to
  562. * <code>CobolCharacteristics<code>' definition.
  563. *
  564. * @param cocoPictureCategory picture category enumeration value as defined
  565. * in <code>CocoPicture</code>
  566. * @return picture enumeration category value as defined in
  567. * <code>CobolCharacteristics</code>
  568. */
  569. private static int cvtPictureCategory(Category cocoPictureCategory) {
  570. switch(cocoPictureCategory) {
  571. case ALPHABETIC:
  572. return CobolCharacteristics.PIC_ALPHA;
  573. case ALPHANUMERIC:
  574. return CobolCharacteristics.PIC_ALPHANUM;
  575. case ALPHANUMERIC_EDITED:
  576. return CobolCharacteristics.PIC_ALPHANUME;
  577. case DBCS:
  578. return CobolCharacteristics.PIC_DBCS;
  579. case EX_FLOAT:
  580. return CobolCharacteristics.PIC_EXFLOAT;
  581. case NUMERIC:
  582. return CobolCharacteristics.PIC_NUM;
  583. case NUMERIC_EDITED:
  584. return CobolCharacteristics.PIC_NUME;
  585. default:
  586. return CobolCharacteristics.PIC_DEGENERATE;
  587. }
  588. }
  589. /**
  590. * Converts usage from XSD annotation form to CobolCharacteristics'
  591. * usage form.
  592. *
  593. * @param usage usage as defined in COBOL Copybook XSD
  594. * @return usage enumeration value as defined in
  595. * <code>CobolCharacteristics</code>
  596. */
  597. private static int cvtUsage(Usage usage) {
  598. if (usage.isSetBinary()) {
  599. return CobolCharacteristics.USAGE_BINARY;
  600. }
  601. if (usage.isSetComp() || usage.isSetComputational()) {
  602. return CobolCharacteristics.USAGE_COMP;
  603. }
  604. if (usage.isSetComp1() || usage.isSetComputational1()) {
  605. return CobolCharacteristics.USAGE_COMP1;
  606. }
  607. if (usage.isSetComp2() || usage.isSetComputational2()) {
  608. return CobolCharacteristics.USAGE_COMP2;
  609. }
  610. if (usage.isSetComp3() || usage.isSetComputational3()) {
  611. return CobolCharacteristics.USAGE_COMP3;
  612. }
  613. if (usage.isSetComp4() || usage.isSetComputational4()) {
  614. return CobolCharacteristics.USAGE_COMP4;
  615. }
  616. if (usage.isSetComp5() || usage.isSetComputational5()) {
  617. return CobolCharacteristics.USAGE_COMP5;
  618. }
  619. if (usage.isSetDisplay()) {
  620. return CobolCharacteristics.USAGE_DISPLAY;
  621. }
  622. if (usage.isSetDisplay1()) {
  623. return CobolCharacteristics.USAGE_DISPLAY1;
  624. }
  625. if (usage.isSetIndex()) {
  626. return CobolCharacteristics.USAGE_INDEX;
  627. }
  628. if (usage.isSetPackedDecimal()) {
  629. return CobolCharacteristics.USAGE_PACKED;
  630. }
  631. return CobolCharacteristics.USAGE_DEGENERATE;
  632. }
  633. /**
  634. * Converts usage from CobolCharacteristics usage form form to
  635. * CocoDescriptionEntry.UsageType form.
  636. *
  637. * @param usage usage enumeration value as defined in
  638. * <code>CobolCharacteristics</code>
  639. * @return usage usage enumeration value as defined in
  640. * <code>CocoDescriptionEntry.UsageType</code>
  641. */
  642. private static int cvtUsage(int usage) {
  643. switch (usage) {
  644. case CobolCharacteristics.USAGE_BINARY:
  645. return UsageType.BINARY;
  646. case CobolCharacteristics.USAGE_COMP:
  647. return UsageType.COMP;
  648. case CobolCharacteristics.USAGE_COMP1:
  649. return UsageType.COMP1;
  650. case CobolCharacteristics.USAGE_COMP2:
  651. return UsageType.COMP2;
  652. case CobolCharacteristics.USAGE_COMP3:
  653. return UsageType.COMP3;
  654. case CobolCharacteristics.USAGE_COMP4:
  655. return UsageType.COMP4;
  656. case CobolCharacteristics.USAGE_COMP5:
  657. return UsageType.COMP5;
  658. case CobolCharacteristics.USAGE_DISPLAY:
  659. return UsageType.DISPLAY;
  660. case CobolCharacteristics.USAGE_DISPLAY1:
  661. return UsageType.DISPLAY1;
  662. case CobolCharacteristics.USAGE_INDEX:
  663. return UsageType.INDEX;
  664. case CobolCharacteristics.USAGE_PACKED:
  665. return UsageType.PACDEC;
  666. }
  667. return UsageType.DISPLAY;
  668. }
  669. public static class RuleContext {
  670. private RuleNode mTopRule;
  671. private List<RuleNode> mRedefinedNodeList = new ArrayList<RuleNode>();
  672. private List<RuleNode> mOccursDependOnList = new ArrayList<RuleNode>();
  673. private RuleNode[] mRedefinedNodes;
  674. private RuleNode[] mOccursDependOnNodes;
  675. private String mDisplayCharEncoding;
  676. private String mDisplay1CharEncoding;
  677. private String mPreDecodeCharCoding;
  678. private String mPostEncodeCharCoding;
  679. public RuleNode getTopRule() {
  680. return mTopRule;
  681. }
  682. public RuleNode[] getRedefinedNodes() {
  683. return mRedefinedNodes;
  684. }
  685. public RuleNode[] getOccursDependOnNodes() {
  686. return mOccursDependOnNodes;
  687. }
  688. public String getPreDecodeCharCoding() {
  689. return mPreDecodeCharCoding;
  690. }
  691. public String getPostEncodeCharCoding() {
  692. return mPostEncodeCharCoding;
  693. }
  694. }
  695. public static void main(String[] argv) {
  696. try {
  697. String xsdLocation = "test/data/redefcob2a.xsd";
  698. SchemaDocument schemaDoc =
  699. SchemaDocument.Factory.parse(new File(xsdLocation));
  700. SchemaTypeSystem ts =
  701. XmlBeans.compileXsd(
  702. new XmlObject[]{schemaDoc},
  703. XmlBeans.getContextTypeLoader(),
  704. null);
  705. RuleNode topRule = readRules(ts, new QName("TESTCOB"));
  706. StringWriter writer = new StringWriter();
  707. topRule.dump(new Emit(writer, 0, 4));
  708. System.out.println(writer.toString());
  709. } catch (XmlException e) {
  710. e.printStackTrace();
  711. } catch (IOException e) {
  712. e.printStackTrace();
  713. } catch (EncoderConfigurationException e) {
  714. e.printStackTrace();
  715. }
  716. }
  717. }