PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/component-common/bpelmodel/src/com/sun/bpel/model/extensions/impl/ForEachImpl.java

https://bitbucket.org/openesb/openesb-components
Java | 667 lines | 359 code | 77 blank | 231 comment | 101 complexity | b10c90d7f07d43f52c997eca721a8059 MD5 | raw file
Possible License(s): AGPL-3.0
  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. * @(#)ForEachImpl.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.bpel.model.extensions.impl;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Collections;
  33. import java.util.Iterator;
  34. import javax.xml.namespace.QName;
  35. import com.sun.bpel.model.Assign;
  36. import com.sun.bpel.model.BPELHelper;
  37. import com.sun.bpel.model.Variable;
  38. import com.sun.bpel.model.Copy;
  39. import com.sun.bpel.model.PartnerLink;
  40. import com.sun.bpel.model.extensions.Choose;
  41. import com.sun.bpel.model.extensions.ForEach;
  42. import com.sun.bpel.model.extensions.ForEach.ATTR;
  43. import com.sun.bpel.model.impl.BPELElementImpl;
  44. import com.sun.bpel.model.visitor.BPELVisitor;
  45. import com.sun.bpel.model.wsdlmodel.impl.XMLAttributeImpl;
  46. import com.sun.bpel.xml.common.model.XMLAttribute;
  47. import com.sun.bpel.xml.common.model.XMLDocument;
  48. import com.sun.bpel.xml.common.model.XMLElement;
  49. import com.sun.bpel.xml.common.model.XMLNode;
  50. import com.sun.bpel.xml.common.visitor.Visitor;
  51. /**
  52. * Implements the <forEach> element.
  53. *
  54. * @author Sun Microsystems
  55. */
  56. public class ForEachImpl extends BPELElementImpl implements ForEach {
  57. /** serialVersionUID for this class */
  58. static final long serialVersionUID = 4492531035037826454L;
  59. /** Holds the copy element list. */
  60. private ArrayList copies = new ArrayList();
  61. /** Holds the forEach element list. */
  62. private ArrayList forEachs = new ArrayList();
  63. /** Holds the chooses element list. */
  64. private ArrayList mChooses = new ArrayList();
  65. private PartnerLink mPartnerLink;
  66. private Variable mVariable;
  67. /** QName object for SeeBeyond Private extension line label */
  68. public static final QName FOREACH_QNAME =
  69. com.sun.bpel.xml.NamespaceUtility.getQName(
  70. XMLElement.SBYNBPEL_EXTN_NAMESPACE,
  71. ForEach.TAG,
  72. XMLElement.SBYNBPEL_EXTN_PREFIX);
  73. /** Creates a new instance of ForEachImpl */
  74. public ForEachImpl() {
  75. super();
  76. initForEach();
  77. }
  78. /** Creates a new instance of ForEachImpl.
  79. * @param d Owner document.
  80. */
  81. public ForEachImpl(XMLDocument d) {
  82. super(d);
  83. initForEach();
  84. }
  85. /** Initializes this class.
  86. */
  87. private void initForEach() {
  88. owningNamespace = XMLElement.SBYNBPEL_EXTN_NAMESPACE;
  89. owningNamespacePrefix = XMLElement.SBYNBPEL_EXTN_PREFIX;
  90. //setLocalName(ForEach.TAG);
  91. setQualifiedName(FOREACH_QNAME);
  92. xmlAttrs = new XMLAttribute[] {
  93. new XMLAttributeImpl(ATTR.CONTAINER, String.class, true, null),
  94. new XMLAttributeImpl(ATTR.PART, String.class, true, null),
  95. new XMLAttributeImpl(ATTR.QUERY, String.class, true, null),
  96. new XMLAttributeImpl(ATTR.EXPRESSION, String.class, true, null),
  97. new XMLAttributeImpl(ATTR.BEGIN, String.class, true, null),
  98. new XMLAttributeImpl(ATTR.END, String.class, true, null),
  99. new XMLAttributeImpl(ATTR.STEP, String.class, true, null),
  100. };
  101. childrenTags = new String[] {
  102. ForEach.TAG,
  103. Copy.TAG,
  104. Choose.TAG
  105. };
  106. }
  107. /** Getter for container attribute.
  108. * @return container attribute.
  109. */
  110. public String getContainer() {
  111. return xmlAttrs[CONTAINER].getValue();
  112. }
  113. /** Setter for container attribute.
  114. * @param c container attribute.
  115. */
  116. public void setContainer(String variable) {
  117. String oldVariable = getContainer();
  118. setAttribute(CONTAINER, variable);
  119. //if variable is changed we need to clear out cached variable object
  120. //so that next call to getBPELVariable can find new variable object
  121. if(variable != null && !variable.equals(oldVariable)) {
  122. this.mVariable = null;
  123. }
  124. }
  125. /** Getter for part attribute.
  126. * @return part attribute.
  127. */
  128. public String getPart() {
  129. return xmlAttrs[PART].getValue();
  130. }
  131. /** Setter for part attribute.
  132. * @param p part attribute.
  133. */
  134. public void setPart(String p) {
  135. setAttribute(PART, p);
  136. }
  137. /** Getter for query attribute.
  138. * @return query attribute.
  139. */
  140. public String getQuery() {
  141. return xmlAttrs[QUERY].getValue();
  142. }
  143. /** Setter for query attribute.
  144. * @param q query attribute.
  145. */
  146. public void setQuery(String q) {
  147. setAttribute(QUERY, q);
  148. }
  149. /** Getter for expression attribute.
  150. * @return expression attribute.
  151. */
  152. public String getExpression() {
  153. return xmlAttrs[EXPRESSION].getValue();
  154. }
  155. /** Setter for expression attribute.
  156. * @param e expression attribute.
  157. */
  158. public void setExpression(String e) {
  159. setAttribute(EXPRESSION, e);
  160. }
  161. /** Getter for begin attribute.
  162. * @return begin attribute.
  163. */
  164. public String getBegin() {
  165. return xmlAttrs[BEGIN].getValue();
  166. }
  167. /** Setter for begin attribute.
  168. * @param b begin attribute.
  169. */
  170. public void setBegin(String b) {
  171. setAttribute(BEGIN, b);
  172. }
  173. /** Getter for end attribute.
  174. * @return end attribute.
  175. */
  176. public String getEnd() {
  177. return xmlAttrs[END].getValue();
  178. }
  179. /** Setter for end attribute.
  180. * @param e end attribute.
  181. */
  182. public void setEnd(String e) {
  183. setAttribute(END, e);
  184. }
  185. /** Getter for step attribute.
  186. * @return step attribute.
  187. */
  188. public String getStep() {
  189. return xmlAttrs[STEP].getValue();
  190. }
  191. /** Setter for step attribute.
  192. * @param s step attribute.
  193. */
  194. public void setStep(String s) {
  195. setAttribute(STEP, s);
  196. }
  197. /** @see XMLNode#addChild(XMLNode)
  198. */
  199. public void addChild(XMLNode c) {
  200. if (c instanceof Copy) {
  201. addCopy((Copy) c);
  202. } else if (c instanceof ForEach) {
  203. addForEach((ForEach) c);
  204. } else if (c instanceof Choose) {
  205. addChoose((Choose) c);
  206. } else {
  207. super.addChild(c);
  208. }
  209. }
  210. /** @see XMLNode#removeChild
  211. */
  212. public void removeChild(XMLNode c) {
  213. if (c instanceof Copy) {
  214. removeCopy((Copy) c);
  215. } else if (c instanceof ForEach) {
  216. removeForEach((ForEach) c);
  217. } else if (c instanceof Choose) {
  218. removeChoose((Choose) c);
  219. } else {
  220. super.removeChild(c);
  221. }
  222. }
  223. /** @see XMLNode#getCopy
  224. */
  225. public Copy getCopy(int i) {
  226. return (Copy) copies.get(i);
  227. }
  228. /**
  229. * Describe <code>getForEach</code> method here.
  230. *
  231. * @param i an <code>int</code> value
  232. * @return a <code>Copy</code> value
  233. */
  234. public ForEach getForEach(int i) {
  235. return (ForEach) forEachs.get(i);
  236. }
  237. /**
  238. * Describe <code>getChoose</code> method here.
  239. *
  240. * @param i an <code>int</code> value
  241. * @return a <code>Copy</code> value
  242. */
  243. public Choose getChoose(int i) {
  244. return (Choose) mChooses.get(i);
  245. }
  246. /** @see XMLNode#setCopy
  247. */
  248. public synchronized void setCopy(int i, Copy c) {
  249. if (copies.size() == i) {
  250. addCopy(c);
  251. } else {
  252. replaceChild(0, (Copy) copies.get(i), c);
  253. copies.set(i, c);
  254. }
  255. }
  256. /**
  257. * Describe <code>setForEach</code> method here.
  258. *
  259. * @param i an <code>int</code> value
  260. * @param f a <code>ForEach</code> value
  261. */
  262. public synchronized void setForEach(int i, ForEach f) {
  263. if (forEachs.size() == i) {
  264. addForEach(f);
  265. } else {
  266. replaceChild(0, (ForEach) forEachs.get(i), f);
  267. forEachs.set(i, f);
  268. }
  269. }
  270. /**
  271. * Describe <code>setChoose</code> method here.
  272. *
  273. * @param i an <code>int</code> value
  274. * @param c a <code>Choose</code> value
  275. */
  276. public synchronized void setChoose(int i, Choose c) {
  277. if (mChooses.size() == i) {
  278. addChoose(c);
  279. } else {
  280. replaceChild(3, (Choose) mChooses.get(i), c);
  281. mChooses.set(i, c);
  282. }
  283. }
  284. /** @see XMLNode#addCopy(Copy)
  285. */
  286. public synchronized void addCopy(Copy c) {
  287. super.addChild(0, c);
  288. copies.add(c);
  289. }
  290. /**
  291. * Describe <code>addForEach</code> method here.
  292. *
  293. * @param f a <code>ForEach</code> value
  294. */
  295. public synchronized void addForEach(ForEach f) {
  296. super.addChild(0, f);
  297. forEachs.add(f);
  298. }
  299. /**
  300. * Describe <code>addChoose</code> method here.
  301. *
  302. * @param c a <code>Choose</code> value
  303. */
  304. public synchronized void addChoose(Choose c) {
  305. super.addChild(3, c);
  306. mChooses.add(c);
  307. }
  308. /** @see Assign#addCopy(int, Copy)
  309. */
  310. public synchronized void addCopy(int index, Copy c) {
  311. if ((index < 0) || (getChildren() != null && index > getChildren().size())) {
  312. throw new ArrayIndexOutOfBoundsException("Expected: 0 <= index <= " + getChildren().size());
  313. } else if ((index == 0 && getChildren() == null) ||
  314. (getChildren() != null && index == getChildren().size())) {
  315. super.addChild(3, c);
  316. addCopy(c);
  317. } else {
  318. super.addChild(3, (XMLNode) getChildren().get(index), c);
  319. copies.clear();
  320. for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
  321. XMLNode kid = (XMLNode) iter.next();
  322. if (kid instanceof Copy) {
  323. copies.add(kid);
  324. }
  325. }
  326. }
  327. }
  328. /**
  329. * Describe <code>addForEach</code> method here.
  330. *
  331. * @param index an <code>int</code> value
  332. * @param f a <code>ForEach</code> value
  333. */
  334. public synchronized void addForEach(int index, ForEach f) {
  335. if ((index < 0) || (getChildren() != null && index > getChildren().size())) {
  336. throw new ArrayIndexOutOfBoundsException("Expected: 0 <= index <= " + getChildren().size());
  337. } else if ((index == 0 && getChildren() == null) ||
  338. (getChildren() != null && index == getChildren().size())) {
  339. super.addChild(3, f);
  340. addForEach(f);
  341. } else {
  342. super.addChild(3, (XMLNode) getChildren().get(index), f);
  343. forEachs.clear();
  344. for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
  345. XMLNode kid = (XMLNode) iter.next();
  346. if (kid instanceof ForEach) {
  347. forEachs.add(kid);
  348. }
  349. }
  350. }
  351. }
  352. /**
  353. * Describe <code>addChoose</code> method here.
  354. *
  355. * @param index an <code>int</code> value
  356. * @param c a <code>Choose</code> value
  357. */
  358. public synchronized void addChoose(int index, Choose c) {
  359. if ((index < 0) || (getChildren() != null && index > getChildren().size())) {
  360. throw new ArrayIndexOutOfBoundsException("Expected: 0 <= index <= " + getChildren().size());
  361. } else if ((index == 0 && getChildren() == null) ||
  362. (getChildren() != null && index == getChildren().size())) {
  363. super.addChild(3, c);
  364. addChoose(c);
  365. } else {
  366. super.addChild(3, (XMLNode) getChildren().get(index), c);
  367. mChooses.clear();
  368. for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
  369. XMLNode kid = (XMLNode) iter.next();
  370. if (kid instanceof Choose) {
  371. mChooses.add(kid);
  372. }
  373. }
  374. }
  375. }
  376. /** @see Assign#clearCopies
  377. */
  378. public synchronized void clearCopies() {
  379. while (copies.size() > 0) {
  380. removeCopy(0); // stays at 0 because array elements keep shifting to the left
  381. }
  382. }
  383. /**
  384. * Describe <code>clearForEachs</code> method here.
  385. *
  386. */
  387. public synchronized void clearForEachs() {
  388. while (forEachs.size() > 0) {
  389. removeForEach(0); // stays at 0 because array elements keep shifting to the left
  390. }
  391. }
  392. /**
  393. * Describe <code>clearChooses</code> method here.
  394. *
  395. */
  396. public synchronized void clearChooses() {
  397. while (mChooses.size() > 0) {
  398. removeChoose(0); // stays at 0 because array elements keep shifting to the left
  399. }
  400. }
  401. /** @see Assign#removeCopy(int)
  402. */
  403. public synchronized void removeCopy(int i) {
  404. removeCopy(getCopy(i));
  405. }
  406. /**
  407. * Describe <code>removeForEach</code> method here.
  408. *
  409. * @param i an <code>int</code> value
  410. */
  411. public synchronized void removeForEach(int i) {
  412. removeForEach(getForEach(i));
  413. }
  414. /**
  415. * Describe <code>removeChoose</code> method here.
  416. *
  417. * @param i an <code>int</code> value
  418. */
  419. public synchronized void removeChoose(int i) {
  420. removeChoose(getChoose(i));
  421. }
  422. /** @see Assign#removeCopy(Copy)
  423. */
  424. public synchronized boolean removeCopy(Copy c) {
  425. super.removeChild(c);
  426. return copies.remove(c);
  427. }
  428. /**
  429. * Describe <code>removeForEach</code> method here.
  430. *
  431. * @param f a <code>ForEach</code> value
  432. * @return a <code>boolean</code> value
  433. */
  434. public synchronized boolean removeForEach(ForEach f) {
  435. super.removeChild(f);
  436. return forEachs.remove(f);
  437. }
  438. /**
  439. * Describe <code>removeChoose</code> method here.
  440. *
  441. * @param f a <code>Choose</code> value
  442. * @return a <code>boolean</code> value
  443. */
  444. public synchronized boolean removeChoose(Choose f) {
  445. super.removeChild(f);
  446. return mChooses.remove(f);
  447. }
  448. /** @see Assign#getCopySize
  449. */
  450. public int getCopySize() {
  451. return copies.size();
  452. }
  453. /**
  454. * Describe <code>getForEachSize</code> method here.
  455. *
  456. * @return an <code>int</code> value
  457. */
  458. public int getForEachSize() {
  459. return forEachs.size();
  460. }
  461. /**
  462. * Describe <code>getChooseSize</code> method here.
  463. *
  464. * @return an <code>int</code> value
  465. */
  466. public int getChooseSize() {
  467. return mChooses.size();
  468. }
  469. /** @see Assign#indexOfCopy
  470. */
  471. public int indexOfCopy(XMLNode copy) {
  472. return copies.indexOf(copy);
  473. }
  474. /**
  475. * Describe <code>indexOfForEach</code> method here.
  476. *
  477. * @param forEach a <code>XMLNode</code> value
  478. * @return an <code>int</code> value
  479. */
  480. public int indexOfForEach(XMLNode forEach) {
  481. return forEachs.indexOf(forEach);
  482. }
  483. /**
  484. * Describe <code>indexOfChoose</code> method here.
  485. *
  486. * @param choose a <code>XMLNode</code> value
  487. * @return an <code>int</code> value
  488. */
  489. public int indexOfChoose(XMLNode choose) {
  490. return mChooses.indexOf(choose);
  491. }
  492. /** @see Assign#getCopies
  493. */
  494. public synchronized Collection getCopies() {
  495. return Collections.unmodifiableCollection((ArrayList) copies.clone());
  496. }
  497. /**
  498. * Describe <code>getForEachs</code> method here.
  499. *
  500. * @return a <code>Collection</code> value
  501. */
  502. public synchronized Collection getForEachs() {
  503. return Collections.unmodifiableCollection((ArrayList) forEachs.clone());
  504. }
  505. /**
  506. * Describe <code>getChooses</code> method here.
  507. *
  508. * @return a <code>Collection</code> value
  509. */
  510. public synchronized Collection getChooses() {
  511. return Collections.unmodifiableCollection((ArrayList) mChooses.clone());
  512. }
  513. public void setBPELVariable(Variable variable) {
  514. //update the variable attribute value
  515. if(variable != null) {
  516. setContainer(variable.getName());
  517. } else {
  518. setContainer(null);
  519. }
  520. this.mVariable = variable;
  521. }
  522. public Variable getBPELVariable() {
  523. if(this.mVariable != null) {
  524. return this.mVariable;
  525. }
  526. if (BPELHelper.isValueAbsent(getContainer())) {
  527. return null;
  528. }
  529. this.mVariable = BPELHelper.getMatchingVariable(getContainer(), this);
  530. return this.mVariable;
  531. }
  532. /**
  533. * Check if two ForEaches have the same rules
  534. * @param other - another <code>ForEach</code> instance
  535. * @return boolean - true if same
  536. */
  537. public boolean hasSameRules(ForEach other) {
  538. if (other == null) {
  539. return false;
  540. }
  541. if (!strEqual(this.getContainer(), other.getContainer())) {
  542. return false;
  543. }
  544. if (!strEqual(this.getPart(), other.getPart())) {
  545. return false;
  546. }
  547. if (!strEqual(this.getQuery(), other.getQuery())) {
  548. return false;
  549. }
  550. if (!strEqual(this.getExpression(), other.getExpression())) {
  551. return false;
  552. }
  553. return true;
  554. }
  555. private boolean strEqual(String str1, String str2) {
  556. if (str1 == null) {
  557. if (str2 != null) {
  558. return false;
  559. }
  560. } else {
  561. if (str2 == null) {
  562. return false;
  563. } else if (!str1.equals(str2)) {
  564. return false;
  565. }
  566. }
  567. return true;
  568. }
  569. /** Accepts a visitor to perform some work on the element.
  570. * @param w The working visitor
  571. * @return <tt>true</tt> if traversal is to continue.
  572. */
  573. public boolean accept(Visitor w) {
  574. BPELVisitor v = morphVisitor(w);
  575. if (traverseParentFirst(v)) {
  576. if (!v.visit(this)) {
  577. return false;
  578. }
  579. }
  580. if (!super.accept(v)) {
  581. return false;
  582. }
  583. if (traverseParentLast(v)) {
  584. if (!v.visit(this)) {
  585. return false;
  586. }
  587. }
  588. return true;
  589. }
  590. }