001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.mapper.predicates.editor;
021:
022: import java.util.ArrayList;
023: import java.util.LinkedList;
024: import java.util.List;
025: import org.netbeans.modules.bpel.mapper.predicates.AbstractPredicate;
026: import org.netbeans.modules.bpel.mapper.tree.models.VariableDeclarationWrapper;
027: import org.netbeans.modules.bpel.mapper.tree.spi.RestartableIterator;
028: import org.netbeans.modules.bpel.model.api.AbstractVariableDeclaration;
029: import org.netbeans.modules.bpel.model.api.VariableDeclaration;
030: import org.netbeans.modules.bpel.model.api.support.XPathBpelVariable;
031: import org.netbeans.modules.xml.schema.model.SchemaComponent;
032: import org.netbeans.modules.xml.wsdl.model.Part;
033: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext;
034: import org.netbeans.modules.xml.xpath.ext.spi.SimpleSchemaContext;
035: import org.netbeans.modules.xml.xpath.ext.spi.VariableSchemaContext;
036:
037: /**
038: * The auxiliary class to convert tree path or path iterator to other forms.
039: *
040: * @author nk160297
041: */
042: public class PathConverter {
043:
044: private static enum ParsingStage {
045: SCHEMA, PART, VARIABLE;
046: };
047:
048: /**
049: * Builds an XPathSchemaContext by a RestartableIterator.
050: * It is implied that the RestartableIterator provides a collection of
051: * tree items' data objects in order from leafs to the tree root.
052: *
053: * @param pathItr
054: * @return
055: */
056: public static XPathSchemaContext constructContext(
057: RestartableIterator<Object> pathItr) {
058: //
059: pathItr.restart();
060: //
061: LinkedList<SchemaComponent> sCompList = new LinkedList<SchemaComponent>();
062: Part part = null;
063: AbstractVariableDeclaration var = null;
064: //
065: // Process the path
066: ParsingStage stage = null;
067: while (pathItr.hasNext()) {
068: Object obj = pathItr.next();
069: if (obj instanceof SchemaComponent) {
070: if (!(stage == null || stage == ParsingStage.SCHEMA)) {
071: return null;
072: }
073: stage = ParsingStage.SCHEMA;
074: sCompList.addFirst((SchemaComponent) obj);
075: } else if (obj instanceof AbstractPredicate) {
076: if (!(stage == null || stage == ParsingStage.SCHEMA)) {
077: return null;
078: }
079: stage = ParsingStage.SCHEMA;
080: sCompList.addFirst(((AbstractPredicate) obj)
081: .getSComponent());
082: } else if (obj instanceof Part) {
083: if (!(stage == ParsingStage.SCHEMA || stage == null)) {
084: return null;
085: }
086: stage = ParsingStage.PART;
087: part = (Part) obj;
088: } else if (obj instanceof AbstractVariableDeclaration) {
089: if (!(stage == ParsingStage.SCHEMA
090: || stage == ParsingStage.PART || stage == null)) {
091: return null;
092: }
093: stage = ParsingStage.VARIABLE;
094: var = (AbstractVariableDeclaration) obj;
095: //
096: // Everything found!
097: break;
098: } else {
099: if (stage == null) {
100: return null;
101: }
102: break;
103: }
104: }
105: //
106: VariableDeclaration varDecl = null;
107: if (var != null) {
108: if (var instanceof VariableDeclaration) {
109: varDecl = (VariableDeclaration) var;
110: } else if (var instanceof VariableDeclarationWrapper) {
111: varDecl = ((VariableDeclarationWrapper) var)
112: .getDelegate();
113: }
114: }
115: //
116: XPathBpelVariable xPathVariable = null;
117: //
118: if (varDecl != null) {
119: xPathVariable = new XPathBpelVariable(varDecl, part);
120: }
121: //
122: VariableSchemaContext varContext = null;
123: if (xPathVariable != null) {
124: varContext = new VariableSchemaContext(xPathVariable);
125: }
126: //
127: XPathSchemaContext xPathContext = SimpleSchemaContext
128: .constructSimpleSchemaContext(varContext, sCompList);
129: //
130: return xPathContext;
131: }
132:
133: /**
134: * Constructs a new list, which contains the schema elements, predicates,
135: * part and variable from the specified iterator pathItr.
136: * The first object taken from iterator will be at the beginning of the list.
137: * If the iterator has incompatible content then the null is returned.
138: *
139: * @param pathItr
140: * @return
141: */
142: public static List<Object> constructPredicateLocationtList(
143: RestartableIterator<Object> pathItr) {
144: //
145: pathItr.restart();
146: //
147: ArrayList<Object> treeItemList = new ArrayList<Object>();
148: //
149: // Process the path
150: ParsingStage stage = null;
151: while (pathItr.hasNext()) {
152: Object obj = pathItr.next();
153: if (obj instanceof SchemaComponent) {
154: if (!(stage == null || stage == ParsingStage.SCHEMA)) {
155: return null;
156: }
157: stage = ParsingStage.SCHEMA;
158: treeItemList.add(obj);
159: } else if (stage != null
160: && obj instanceof AbstractPredicate) {
161: if (!(stage == null || stage == ParsingStage.SCHEMA)) {
162: return null;
163: }
164: stage = ParsingStage.SCHEMA;
165: treeItemList.add(obj);
166: } else if (obj instanceof Part) {
167: if (stage != ParsingStage.SCHEMA) {
168: return null;
169: }
170: stage = ParsingStage.PART;
171: treeItemList.add(obj);
172: } else if (obj instanceof AbstractVariableDeclaration) {
173: if (!(stage == ParsingStage.SCHEMA || stage == ParsingStage.PART)) {
174: return null;
175: }
176: //
177: AbstractVariableDeclaration var = (AbstractVariableDeclaration) obj;
178: //
179: VariableDeclaration varDecl = null;
180: if (var instanceof VariableDeclaration) {
181: varDecl = (VariableDeclaration) var;
182: } else if (var instanceof VariableDeclarationWrapper) {
183: varDecl = ((VariableDeclarationWrapper) var)
184: .getDelegate();
185: }
186: //
187: if (varDecl == null) {
188: return null;
189: }
190: //
191: stage = ParsingStage.VARIABLE;
192: treeItemList.add(varDecl);
193: //
194: // Everything found!
195: break;
196: } else {
197: if (stage == null) {
198: return null;
199: }
200: break;
201: }
202: }
203: //
204: return treeItemList;
205: }
206:
207: public static String toString(RestartableIterator<Object> pathItr) {
208: LinkedList<Object> list = new LinkedList<Object>();
209: pathItr.restart();
210: while (pathItr.hasNext()) {
211: list.addFirst(pathItr.next());
212: }
213: //
214: StringBuilder sb = new StringBuilder();
215: boolean isFirst = true;
216: for (Object obj : list) {
217: if (isFirst) {
218: isFirst = false;
219: } else {
220: sb.append("/"); // NOI18N
221: }
222: sb.append(obj.toString());
223: }
224: //
225: return sb.toString();
226: }
227:
228: }
|