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.debugger.ui.util;
021:
022: import java.io.IOException;
023: import java.util.Collections;
024: import java.util.Iterator;
025: import java.util.List;
026: import javax.swing.text.StyledDocument;
027: import javax.xml.XMLConstants;
028: import javax.xml.namespace.NamespaceContext;
029: import javax.xml.namespace.QName;
030: import javax.xml.xpath.XPath;
031: import javax.xml.xpath.XPathConstants;
032: import javax.xml.xpath.XPathExpressionException;
033: import javax.xml.xpath.XPathFactory;
034: import org.netbeans.api.debugger.DebuggerEngine;
035: import org.netbeans.api.debugger.DebuggerManager;
036: import org.netbeans.modules.bpel.debugger.api.EditorContextBridge;
037: import org.netbeans.modules.bpel.debugger.api.SourcePath;
038: import org.netbeans.modules.bpel.debugger.api.psm.ProcessStaticModel;
039: import org.netbeans.modules.bpel.model.api.BpelEntity;
040: import org.netbeans.modules.bpel.model.api.BpelModel;
041: import org.netbeans.modules.bpel.model.api.support.UniqueId;
042: import org.netbeans.modules.bpel.model.spi.FindHelper;
043: import org.netbeans.modules.xml.xam.Component;
044: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
045: import org.openide.text.NbDocument;
046: import org.openide.util.Lookup;
047: import org.w3c.dom.Document;
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050:
051: /**
052: *
053: * @author Alexander Zgursky
054: */
055: public final class ModelUtil {
056: private static FindHelper findHelper = Lookup.getDefault().lookup(
057: FindHelper.class);
058:
059: private ModelUtil() {
060: // Does nothing
061: }
062:
063: public static FindHelper getFindHelper() {
064: return findHelper;
065: }
066:
067: public static String getUrl(final QName processQName) {
068: final DebuggerEngine engine = DebuggerManager
069: .getDebuggerManager().getCurrentEngine();
070:
071: if (engine == null) {
072: return null;
073: }
074:
075: final SourcePath sourcePath = engine.lookupFirst(null,
076: SourcePath.class);
077:
078: if (sourcePath == null) {
079: return null;
080: }
081:
082: return sourcePath.getSourcePath(processQName);
083: }
084:
085: public static BpelModel getBpelModel(final QName processQName) {
086: final String url = getUrl(processQName);
087:
088: if (url == null) {
089: return null;
090: }
091:
092: return EditorUtil.getBpelModel(url);
093: }
094:
095: public static String getXpath(final UniqueId bpelEntityId) {
096:
097: final BpelModel model = bpelEntityId.getModel();
098:
099: try {
100: model.sync();
101: } catch (IOException ex) {
102: return null;
103: }
104:
105: class MyRunnable implements Runnable {
106: private String result = null;
107:
108: public String getResult() {
109: return this .result;
110: }
111:
112: public void run() {
113: if (!model.getState().equals(BpelModel.State.VALID)) {
114: return;
115: }
116:
117: final BpelEntity bpelEntity = model
118: .getEntity(bpelEntityId);
119:
120: if (bpelEntity != null) {
121: result = EditorContextBridge
122: .normalizeXpath(findHelper
123: .getXPath(bpelEntity));
124: }
125: }
126: }
127:
128: final MyRunnable r = new MyRunnable();
129:
130: model.invoke(r);
131:
132: return r.getResult();
133: }
134:
135: public static UniqueId getBpelEntityId(final BpelModel model,
136: final String xpath) {
137:
138: try {
139: model.sync();
140: } catch (IOException ex) {
141: return null;
142: }
143:
144: class MyRunnable implements Runnable {
145: private UniqueId result = null;
146:
147: public UniqueId getResult() {
148: return this .result;
149: }
150:
151: public void run() {
152: if (!model.getState().equals(BpelModel.State.VALID)) {
153: return;
154: }
155:
156: // BpelEntity[] entities = findHelper.findModelElements(model, xpath);
157: // if (entities.length == 1) {
158: // result = entities[0].getUID();
159: // }
160: BpelEntity entity = findBpelEntity(model, xpath);
161: if (entity != null) {
162: result = entity.getUID();
163: }
164:
165: }
166: }
167:
168: final MyRunnable r = new MyRunnable();
169:
170: model.invoke(r);
171:
172: return r.getResult();
173: }
174:
175: public static BpelEntity findBpelEntity(final BpelModel model,
176: final String xpathExpression) {
177:
178: if (model.getProcess() == null) {
179: return null;
180: }
181: Document doc = model.getProcess().getPeer().getOwnerDocument();
182: XPath xpath = XPathFactory.newInstance().newXPath();
183: NamespaceContext myNsContext = new NamespaceContext() {
184: public String getNamespaceURI(String prefix) {
185: if (ProcessStaticModel.BPEL_NAMESPACE_PREFIX
186: .equals(prefix)) {
187: return ProcessStaticModel.BPEL_NAMESPACE_URI;
188: } else {
189: return XMLConstants.NULL_NS_URI;
190: }
191: }
192:
193: public String getPrefix(String namespaceURI) {
194: if (ProcessStaticModel.BPEL_NAMESPACE_URI
195: .equals(namespaceURI)) {
196: return ProcessStaticModel.BPEL_NAMESPACE_PREFIX;
197: } else {
198: return null;
199: }
200: }
201:
202: public Iterator getPrefixes(String namespaceURI) {
203: String prefix = getPrefix(namespaceURI);
204: if (prefix != null) {
205: return Collections.singletonList(prefix).iterator();
206: } else {
207: return Collections.emptyList().iterator();
208: }
209: }
210: };
211:
212: xpath.setNamespaceContext(myNsContext);
213: Node node;
214: try {
215: node = (Node) xpath.evaluate(xpathExpression, doc,
216: XPathConstants.NODE);
217: } catch (XPathExpressionException e) {
218: return null;
219: }
220:
221: if (node instanceof Element) {
222: AbstractDocumentModel<BpelEntity> xamModel = (AbstractDocumentModel<BpelEntity>) model;
223: List<Element> pathToRoot = xamModel.getAccess()
224: .getPathFromRoot(xamModel.getDocument(),
225: (Element) node);
226: Component comp = xamModel.findComponent(pathToRoot);
227: if (comp instanceof BpelEntity) {
228: return (BpelEntity) comp;
229: } else {
230: return null;
231: }
232: } else {
233: return null;
234: }
235: }
236:
237: public static UniqueId getBpelEntityId(final BpelModel model,
238: final int offset) {
239:
240: try {
241: model.sync();
242: } catch (IOException ex) {
243: return null;
244: }
245:
246: class MyRunnable implements Runnable {
247: private UniqueId result = null;
248:
249: public UniqueId getResult() {
250: return this .result;
251: }
252:
253: public void run() {
254: if (!model.getState().equals(BpelModel.State.VALID)) {
255: return;
256: }
257: BpelEntity bpelEntity = model.findElement(offset);
258: if (bpelEntity != null) {
259: result = bpelEntity.getUID();
260: }
261:
262: }
263: }
264:
265: final MyRunnable r = new MyRunnable();
266:
267: model.invoke(r);
268:
269: return r.getResult();
270: }
271:
272: public static int getLineNumber(final UniqueId bpelEntityId) {
273:
274: if (bpelEntityId == null) {
275: return -1;
276: }
277:
278: final BpelModel model = bpelEntityId.getModel();
279:
280: try {
281: model.sync();
282: } catch (IOException ex) {
283: return -1;
284: }
285:
286: class MyRunnable implements Runnable {
287: private int result = -1;
288:
289: public int getResult() {
290: return this .result;
291: }
292:
293: public void run() {
294: if (!model.getState().equals(BpelModel.State.VALID)) {
295: return;
296: }
297: BpelEntity bpelEntity = model.getEntity(bpelEntityId);
298: if (bpelEntity != null) {
299: result = bpelEntity.findPosition();
300: }
301: }
302: }
303:
304: final MyRunnable r = new MyRunnable();
305: model.invoke(r);
306:
307: int offset = r.getResult();
308: if (offset < 0) {
309: return -1;
310: }
311:
312: final StyledDocument document = model.getModelSource()
313: .getLookup().lookup(StyledDocument.class);
314:
315: return NbDocument.findLineNumber(document, offset) + 1;
316: }
317:
318: public static int getLineNumber(final BpelModel model,
319: final String xpath) {
320: return getLineNumber(getBpelEntityId(model, xpath));
321: }
322: }
|