0001: /*
0002: * The contents of this file are subject to the terms of the Common Development
0003: * and Distribution License (the License). You may not use this file except in
0004: * compliance with the License.
0005: *
0006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
0007: * or http://www.netbeans.org/cddl.txt.
0008: *
0009: * When distributing Covered Code, include this CDDL Header Notice in each file
0010: * and include the License file at http://www.netbeans.org/cddl.txt.
0011: * If applicable, add the following below the CDDL Header, with the fields
0012: * enclosed by brackets [] replaced by your own identifying information:
0013: * "Portions Copyrighted [year] [name of copyright owner]"
0014: *
0015: * The Original Software is NetBeans. The Initial Developer of the Original
0016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0017: * Microsystems, Inc. All Rights Reserved.
0018: */
0019:
0020: package org.netbeans.modules.bpel.debugger.ui.util;
0021:
0022: import java.util.Arrays;
0023: import java.util.LinkedList;
0024: import java.util.List;
0025: import java.util.StringTokenizer;
0026: import javax.xml.namespace.QName;
0027: import org.netbeans.api.debugger.DebuggerEngine;
0028: import org.netbeans.api.debugger.DebuggerManager;
0029: import org.netbeans.modules.bpel.debugger.api.BpelDebugger;
0030: import org.netbeans.modules.bpel.debugger.api.Position;
0031: import org.netbeans.modules.bpel.debugger.api.ProcessInstance;
0032: import org.netbeans.modules.bpel.debugger.api.SourcePath;
0033: import org.netbeans.modules.bpel.debugger.api.variables.NamedValueHost;
0034: import org.netbeans.modules.bpel.debugger.api.variables.SimpleValue;
0035: import org.netbeans.modules.bpel.debugger.api.variables.SimpleVariable;
0036: import org.netbeans.modules.bpel.debugger.api.variables.Value;
0037: import org.netbeans.modules.bpel.debugger.api.variables.WsdlMessageValue;
0038: import org.netbeans.modules.bpel.debugger.api.variables.WsdlMessageVariable;
0039: import org.netbeans.modules.bpel.debugger.api.variables.XmlElementValue;
0040: import org.netbeans.modules.bpel.debugger.api.variables.XmlElementVariable;
0041: import org.netbeans.modules.bpel.model.api.BpelEntity;
0042: import org.netbeans.modules.bpel.model.api.BpelModel;
0043: import org.netbeans.modules.bpel.model.api.Scope;
0044: import org.netbeans.modules.bpel.model.api.Variable;
0045: import org.netbeans.modules.bpel.model.api.VariableContainer;
0046: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
0047: import org.netbeans.modules.xml.schema.model.GlobalType;
0048: import org.netbeans.modules.xml.wsdl.model.Message;
0049: import org.netbeans.modules.xml.wsdl.model.Part;
0050: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
0051: import org.netbeans.spi.viewmodel.TreeModel;
0052: import org.openide.util.NbBundle;
0053: import org.w3c.dom.Element;
0054: import org.w3c.dom.NamedNodeMap;
0055: import org.w3c.dom.Node;
0056: import org.w3c.dom.NodeList;
0057:
0058: /**
0059: * A helper class which is used to provide various information about BPEL
0060: * variables.
0061: *
0062: * @author Kirill Sorokin
0063: */
0064: public class VariablesUtil {
0065: private BpelDebugger myDebugger;
0066:
0067: public VariablesUtil(final BpelDebugger debugger) {
0068: myDebugger = debugger;
0069: }
0070:
0071: // Display name ////////////////////////////////////////////////////////////
0072: public String getDisplayName(final Object object) {
0073: if (object instanceof NamedValueHost) {
0074: return ((NamedValueHost) object).getName();
0075: }
0076:
0077: if (object instanceof Node) {
0078: return ((Node) object).getNodeName();
0079: }
0080:
0081: return NbBundle.getMessage(VariablesUtil.class,
0082: "VU_CannotResolveDN", object); // NOI18N
0083: }
0084:
0085: // Icon base ///////////////////////////////////////////////////////////////
0086: public String getIconBase(final Object object) {
0087:
0088: if (object instanceof NamedValueHost) {
0089: if (object instanceof WsdlMessageVariable) {
0090: return WSDL_MESSAGE_VARIABLE_ICON;
0091: }
0092:
0093: if (object instanceof WsdlMessageValue.Part) {
0094: return WSDL_MESSAGE_PART_ICON;
0095: }
0096:
0097: if (object instanceof XmlElementVariable) {
0098: return XML_ELEMENT_VARIABLE_ICON;
0099: }
0100:
0101: if (object instanceof SimpleVariable) {
0102: return SIMPLE_VARIABLE_ICON;
0103: }
0104:
0105: return DEFAULT_VARIABLE_ICON;
0106: }
0107:
0108: if (object instanceof Node) {
0109: switch (((Node) object).getNodeType()) {
0110: case Node.ELEMENT_NODE:
0111: return ELEMENT_NODE_ICON;
0112:
0113: case Node.ATTRIBUTE_NODE:
0114: return ATTRIBUTE_NODE_ICON;
0115:
0116: case Node.TEXT_NODE:
0117: return TEXT_NODE_ICON;
0118:
0119: case Node.CDATA_SECTION_NODE:
0120: return CDATA_NODE_ICON;
0121:
0122: default:
0123: return DEFAULT_NODE_ICON;
0124: }
0125: }
0126:
0127: return DEFAULT_VARIABLE_ICON;
0128: }
0129:
0130: // Children ////////////////////////////////////////////////////////////////
0131: public Object[] getChildren(final Object object) {
0132: if (object.equals(TreeModel.ROOT)) {
0133: final ProcessInstance processInstance = myDebugger
0134: .getCurrentProcessInstance();
0135:
0136: if (processInstance != null) {
0137: return processInstance.getVariables();
0138: } else {
0139: return new Object[0];
0140: }
0141: }
0142:
0143: if (object instanceof NamedValueHost) {
0144: final Value value = ((NamedValueHost) object).getValue();
0145:
0146: if (value != null) {
0147: return getChildren(value);
0148: } else {
0149: return new Object[0];
0150: }
0151: }
0152:
0153: if (object instanceof Node) {
0154: return getChildren((Node) object);
0155: }
0156:
0157: return new Object[0];
0158: }
0159:
0160: private Object[] getChildren(final Node node) {
0161: final LinkedList<Object> result = new LinkedList<Object>();
0162:
0163: if (node.getNodeType() == Node.ELEMENT_NODE) {
0164: final Element element = (Element) node;
0165: final NamedNodeMap attrs = element.getAttributes();
0166:
0167: final int length = attrs.getLength();
0168: for (int i = 0; i < length; i++) {
0169: result.add(attrs.item(i));
0170: }
0171: }
0172:
0173: final NodeList nodes = node.getChildNodes();
0174: final int nodeCount = nodes.getLength();
0175: if (nodeCount == 1
0176: && nodes.item(0).getNodeType() == Node.TEXT_NODE) {
0177: // Don't add any children - merge the child text node with
0178: // its parent
0179: } else {
0180: for (int i = 0; i < nodeCount; i++) {
0181: final Node child = nodes.item(i);
0182: if (child.getNodeType() == Node.COMMENT_NODE) {
0183: continue;
0184: }
0185:
0186: if (child.getNodeType() == Node.TEXT_NODE) {
0187: if ((child.getNodeValue() == null)
0188: || child.getNodeValue().trim().equals("")) {
0189: continue;
0190: }
0191: }
0192:
0193: result.add(child);
0194: }
0195: }
0196:
0197: return result.toArray(new Object[result.size()]);
0198: }
0199:
0200: private Object[] getChildren(final Value value) {
0201: if (value instanceof SimpleValue) {
0202: return new Object[0];
0203: }
0204:
0205: if (value instanceof WsdlMessageValue) {
0206: return ((WsdlMessageValue) value).getParts();
0207: }
0208:
0209: if (value instanceof XmlElementValue) {
0210: return getChildren(((XmlElementValue) value).getElement());
0211: }
0212:
0213: return new Object[0];
0214: }
0215:
0216: // Type ////////////////////////////////////////////////////////////////////
0217: public String getType(final Object object) {
0218: String type = null;
0219:
0220: if (object instanceof NamedValueHost) {
0221: if (object instanceof WsdlMessageVariable) {
0222: type = getType((WsdlMessageVariable) object);
0223: }
0224:
0225: if (object instanceof WsdlMessageValue.Part) {
0226: type = getType((WsdlMessageValue.Part) object);
0227: }
0228:
0229: if (object instanceof XmlElementVariable) {
0230: type = getType((XmlElementVariable) object);
0231: }
0232:
0233: if (object instanceof SimpleVariable) {
0234: type = getType((SimpleVariable) object);
0235: }
0236:
0237: if (type == null) {
0238: return "";
0239: } else {
0240: return " " + type + " ";
0241: }
0242: }
0243:
0244: if (object instanceof Node) {
0245: type = getType((Node) object);
0246:
0247: if (type == null) {
0248: return "";
0249: } else {
0250: return " " + type + " ";
0251: }
0252: }
0253:
0254: return NbBundle.getMessage(VariablesUtil.class,
0255: "VU_CannotResolveType", object); // NOI18N
0256: }
0257:
0258: public String getTypeTooltip(final Object object) {
0259: if (object instanceof NamedValueHost) {
0260: if (object instanceof WsdlMessageVariable) {
0261: return " "
0262: + getTypeTooltip((WsdlMessageVariable) object)
0263: + " ";
0264: }
0265:
0266: if (object instanceof WsdlMessageValue.Part) {
0267: return " "
0268: + getTypeTooltip((WsdlMessageValue.Part) object)
0269: + " ";
0270: }
0271:
0272: if (object instanceof XmlElementVariable) {
0273: return " "
0274: + getTypeTooltip((XmlElementVariable) object)
0275: + " ";
0276: }
0277:
0278: if (object instanceof SimpleVariable) {
0279: return " " + getTypeTooltip((SimpleVariable) object)
0280: + " ";
0281: }
0282: }
0283:
0284: if (object instanceof Node) {
0285: return " " + getTypeTooltip((Node) object) + " ";
0286: }
0287:
0288: return NbBundle.getMessage(VariablesUtil.class,
0289: "VU_CannotResolveType", object); // NOI18N
0290: }
0291:
0292: private String getType(final WsdlMessageVariable object) {
0293: final QName qName = getTypeQName(object);
0294:
0295: if (qName != null) {
0296: return formatShort(qName);
0297: }
0298:
0299: return null;
0300: }
0301:
0302: private String getType(final WsdlMessageValue.Part object) {
0303: final QName qName = getTypeQName(object);
0304:
0305: if (qName != null) {
0306: return formatShort(qName);
0307: }
0308:
0309: return null;
0310: }
0311:
0312: private String getType(final XmlElementVariable object) {
0313: final QName qName = getTypeQName(object);
0314:
0315: if (qName != null) {
0316: return formatShort(qName);
0317: }
0318:
0319: return null;
0320: }
0321:
0322: private String getType(final SimpleVariable object) {
0323: final QName qName = getTypeQName(object);
0324:
0325: if (qName != null) {
0326: return formatShort(qName);
0327: }
0328:
0329: return null;
0330: }
0331:
0332: private String getType(final Node object) {
0333: switch (object.getNodeType()) {
0334: case Node.ELEMENT_NODE:
0335: return NbBundle.getMessage(VariablesUtil.class,
0336: "VU_TypeElement"); // NOI18N
0337:
0338: case Node.ATTRIBUTE_NODE:
0339: return NbBundle.getMessage(VariablesUtil.class,
0340: "VU_TypeAttribute"); // NOI18N
0341:
0342: case Node.TEXT_NODE:
0343: return NbBundle.getMessage(VariablesUtil.class,
0344: "VU_TypeText"); // NOI18N
0345:
0346: case Node.CDATA_SECTION_NODE:
0347: return NbBundle.getMessage(VariablesUtil.class,
0348: "VU_TypeCDATA"); // NOI18N
0349:
0350: default:
0351: return null;
0352: }
0353: }
0354:
0355: private String getTypeTooltip(final WsdlMessageVariable object) {
0356: final QName qName = getTypeQName(object);
0357:
0358: if (qName != null) {
0359: return formatLong(qName);
0360: }
0361:
0362: return null;
0363: }
0364:
0365: private String getTypeTooltip(final WsdlMessageValue.Part object) {
0366: final QName qName = getTypeQName(object);
0367:
0368: if (qName != null) {
0369: return formatLong(qName);
0370: }
0371:
0372: return null;
0373: }
0374:
0375: private String getTypeTooltip(final XmlElementVariable object) {
0376: final QName qName = getTypeQName(object);
0377:
0378: if (qName != null) {
0379: return formatLong(qName);
0380: }
0381:
0382: return null;
0383: }
0384:
0385: private String getTypeTooltip(final SimpleVariable object) {
0386: final QName qName = getTypeQName(object);
0387:
0388: if (qName != null) {
0389: return formatLong(qName);
0390: }
0391:
0392: return null;
0393: }
0394:
0395: private String getTypeTooltip(final Node object) {
0396: return getType(object);
0397: }
0398:
0399: private QName getTypeQName(final WsdlMessageVariable object) {
0400: final String name = object.getName();
0401:
0402: final Variable variable = getBpelVariable(name);
0403: if (variable != null) {
0404: return variable.getMessageType().getQName();
0405: }
0406:
0407: return null;
0408: }
0409:
0410: private QName getTypeQName(final WsdlMessageValue.Part object) {
0411: final String partName = object.getName();
0412: final String messageName = object.getMessage().getValueHost()
0413: .getName();
0414:
0415: final Variable variable = getBpelVariable(messageName);
0416: if (variable != null) {
0417: final Part part = getWsdlMessagePart(variable, partName);
0418:
0419: if (part != null) {
0420: final QName qName;
0421:
0422: if (part.getType() != null) {
0423: qName = part.getType().getQName();
0424: } else {
0425: final NamedComponentReference<? extends GlobalType> type = part
0426: .getElement().get().getType();
0427:
0428: if (type != null) {
0429: qName = type.getQName();
0430: } else {
0431: qName = part.getElement().getQName();
0432: }
0433: }
0434:
0435: return qName;
0436: }
0437: }
0438:
0439: return null;
0440: }
0441:
0442: private QName getTypeQName(final XmlElementVariable object) {
0443: final String name = object.getName();
0444:
0445: final Variable variable = getBpelVariable(name);
0446: if (variable != null) {
0447: if (variable.getElement() != null) {
0448: return variable.getElement().getQName();
0449: } else {
0450: return variable.getType().getQName();
0451: }
0452: }
0453:
0454: return null;
0455: }
0456:
0457: private QName getTypeQName(final SimpleVariable object) {
0458: final String name = object.getName();
0459:
0460: final Variable variable = getBpelVariable(name);
0461: if (variable != null) {
0462: return variable.getType().getQName();
0463: }
0464:
0465: return null;
0466: }
0467:
0468: // Value ///////////////////////////////////////////////////////////////////
0469: public String getValue(final Object object) {
0470: if (object instanceof NamedValueHost) {
0471: if (object instanceof WsdlMessageVariable) {
0472: return getValue((WsdlMessageVariable) object);
0473: }
0474:
0475: if (object instanceof WsdlMessageValue.Part) {
0476: return getValue((WsdlMessageValue.Part) object);
0477: }
0478:
0479: if (object instanceof XmlElementVariable) {
0480: return getValue((XmlElementVariable) object);
0481: }
0482:
0483: if (object instanceof SimpleVariable) {
0484: return getValue((SimpleVariable) object);
0485: }
0486: }
0487:
0488: if (object instanceof Node) {
0489: return getValue((Node) object);
0490: }
0491:
0492: return NbBundle.getMessage(VariablesUtil.class,
0493: "VU_CannotResolveValue", object); // NOI18N
0494: }
0495:
0496: public String getValueTooltip(final Object object) {
0497: if (object instanceof NamedValueHost) {
0498: if (object instanceof WsdlMessageVariable) {
0499: return " "
0500: + getValueTooltip((WsdlMessageVariable) object)
0501: + " ";
0502: }
0503:
0504: if (object instanceof WsdlMessageValue.Part) {
0505: return " "
0506: + getValueTooltip((WsdlMessageValue.Part) object)
0507: + " ";
0508: }
0509:
0510: if (object instanceof XmlElementVariable) {
0511: return " "
0512: + getValueTooltip((XmlElementVariable) object)
0513: + " ";
0514: }
0515:
0516: if (object instanceof SimpleVariable) {
0517: return " " + getValueTooltip((SimpleVariable) object)
0518: + " ";
0519: }
0520: }
0521:
0522: if (object instanceof Node) {
0523: return " " + getValueTooltip((Node) object) + " ";
0524: }
0525:
0526: return NbBundle.getMessage(VariablesUtil.class,
0527: "VU_CannotResolveValue", object); // NOI18N
0528: }
0529:
0530: private String getValue(final WsdlMessageVariable object) {
0531: final WsdlMessageValue value = (WsdlMessageValue) object
0532: .getValue();
0533:
0534: if (value != null) {
0535: return NbBundle.getMessage(VariablesUtil.class,
0536: "VU_ValueWSDL", value.getParts().length); // NOI18N
0537: } else {
0538: return NbBundle.getMessage(VariablesUtil.class,
0539: "VU_ValueNotInitialized", getDisplayName(object)); // NOI18N
0540: }
0541: }
0542:
0543: private String getValue(final WsdlMessageValue.Part object) {
0544: if (object.getValue() == null) {
0545: return NbBundle.getMessage(VariablesUtil.class,
0546: "VU_ValueNotInitialized", getDisplayName(object)); // NOI18N
0547: }
0548:
0549: if (object.getValue() instanceof XmlElementValue) {
0550: return getValue(((XmlElementValue) object.getValue())
0551: .getElement());
0552: } else {
0553: return ((SimpleValue) object.getValue()).getValueAsString();
0554: }
0555: }
0556:
0557: private String getValue(final XmlElementVariable object) {
0558: if (object.getValue() != null) {
0559: return getValue(((XmlElementValue) object.getValue())
0560: .getElement());
0561: } else {
0562: return NbBundle.getMessage(VariablesUtil.class,
0563: "VU_ValueNotInitialized", getDisplayName(object)); // NOI18N
0564: }
0565: }
0566:
0567: private String getValue(final SimpleVariable object) {
0568: if (object.getValue() != null) {
0569: return ((SimpleValue) object.getValue()).getValueAsString();
0570: } else {
0571: return NbBundle.getMessage(VariablesUtil.class,
0572: "VU_ValueNotInitialized", getDisplayName(object)); // NOI18N
0573: }
0574: }
0575:
0576: private String getValue(final Node object) {
0577: if (object.getNodeType() == Node.ELEMENT_NODE) {
0578: if (XmlUtil.isTextOnlyNode(object)) {
0579: return object.getChildNodes().item(0).getNodeValue();
0580: }
0581:
0582: return NbBundle.getMessage(VariablesUtil.class,
0583: "VU_ValueXMLData"); // NOI18N
0584: }
0585:
0586: return object.getTextContent();
0587: }
0588:
0589: private String getValueTooltip(final WsdlMessageVariable object) {
0590: return getValue(object);
0591: }
0592:
0593: private String getValueTooltip(final WsdlMessageValue.Part object) {
0594: return getValue(object);
0595: }
0596:
0597: private String getValueTooltip(final XmlElementVariable object) {
0598: return getValue(object);
0599: }
0600:
0601: private String getValueTooltip(final SimpleVariable object) {
0602: return getValue(object);
0603: }
0604:
0605: private String getValueTooltip(final Node object) {
0606: return getValue(object);
0607: }
0608:
0609: // Read-only / read-write //////////////////////////////////////////////////
0610: public boolean isValueReadOnly(final Object object) {
0611: if (object instanceof NamedValueHost) {
0612: if (object instanceof WsdlMessageVariable) {
0613: return isValueReadOnly((WsdlMessageVariable) object);
0614: }
0615:
0616: if (object instanceof WsdlMessageValue.Part) {
0617: return isValueReadOnly((WsdlMessageValue.Part) object);
0618: }
0619:
0620: if (object instanceof XmlElementVariable) {
0621: return isValueReadOnly((XmlElementVariable) object);
0622: }
0623:
0624: if (object instanceof SimpleVariable) {
0625: return isValueReadOnly((SimpleVariable) object);
0626: }
0627: }
0628:
0629: if (object instanceof Node) {
0630: return isValueReadOnly((Node) object);
0631: }
0632:
0633: return true;
0634: }
0635:
0636: private boolean isValueReadOnly(final WsdlMessageVariable object) {
0637: return true;
0638: }
0639:
0640: private boolean isValueReadOnly(final WsdlMessageValue.Part object) {
0641: if (object.getValue() instanceof XmlElementValue) {
0642: return true;
0643: } else {
0644: return false;
0645: }
0646: }
0647:
0648: private boolean isValueReadOnly(final XmlElementVariable object) {
0649: return true;
0650: }
0651:
0652: private boolean isValueReadOnly(final SimpleVariable object) {
0653: return object.getValue() == null;
0654: }
0655:
0656: private boolean isValueReadOnly(final Node object) {
0657: if (object.getNodeType() == Node.ATTRIBUTE_NODE) {
0658: if (!(object.getNodeName().equals("xmlns") || // NOI18N
0659: object.getNodeName().equals("targetNamespace") || // NOI18N
0660: object.getNodeName().startsWith("xmlns:"))) { // NOI18N
0661: return false;
0662: }
0663: }
0664:
0665: return !XmlUtil.isTextOnlyNode(object);
0666: }
0667:
0668: // Value setters ///////////////////////////////////////////////////////////
0669: public void setValue(final Object object, final String value) {
0670: if (object instanceof NamedValueHost) {
0671: if (object instanceof WsdlMessageVariable) {
0672: // does nothing
0673: }
0674:
0675: if (object instanceof WsdlMessageValue.Part) {
0676: setValue((WsdlMessageValue.Part) object, value);
0677: }
0678:
0679: if (object instanceof XmlElementVariable) {
0680: // does nothing
0681: }
0682:
0683: if (object instanceof SimpleVariable) {
0684: setValue((SimpleVariable) object, value);
0685: }
0686: }
0687:
0688: if (object instanceof Node) {
0689: setValue((Node) object, value);
0690: }
0691: }
0692:
0693: private void setValue(final WsdlMessageValue.Part object,
0694: final String value) {
0695: if (!(object.getValue() instanceof SimpleValue)) {
0696: return;
0697: }
0698:
0699: final NamedValueHost message = object.getMessage()
0700: .getValueHost();
0701:
0702: if (message instanceof WsdlMessageVariable) {
0703: ((WsdlMessageVariable) message).setPartSimpleValue(object,
0704: value);
0705: }
0706: }
0707:
0708: private void setValue(final SimpleVariable object,
0709: final String value) {
0710: object.setValue(value);
0711: }
0712:
0713: private void setValue(final Node object, final String value) {
0714: if (XmlUtil.isTextOnlyNode(object)) {
0715: setValue(object.getChildNodes().item(0), value);
0716: return;
0717: }
0718:
0719: final XmlElementValue xmlValue = XmlElementValue.Helper
0720: .find(object);
0721: final NamedValueHost valueHost = xmlValue.getValueHost();
0722:
0723: if (valueHost instanceof XmlElementVariable) {
0724: ((XmlElementVariable) valueHost)
0725: .setNodeValue(object, value);
0726: return;
0727: }
0728:
0729: if (valueHost instanceof WsdlMessageValue.Part) {
0730: final WsdlMessageValue.Part part = (WsdlMessageValue.Part) valueHost;
0731: final NamedValueHost messageValueHost = part.getMessage()
0732: .getValueHost();
0733:
0734: if (messageValueHost instanceof WsdlMessageVariable) {
0735: ((WsdlMessageVariable) messageValueHost)
0736: .setPartNodeValue(part, object, value);
0737: }
0738: }
0739:
0740: object.setNodeValue(value);
0741: }
0742:
0743: // Custom editor ///////////////////////////////////////////////////////////
0744: public boolean supportsCustomEditor(final Object object) {
0745: if (object instanceof NamedValueHost) {
0746: if (object instanceof WsdlMessageVariable) {
0747: return false;
0748: }
0749:
0750: if (object instanceof WsdlMessageValue.Part) {
0751: return ((NamedValueHost) object).getValue() != null;
0752: }
0753:
0754: if (object instanceof XmlElementVariable) {
0755: return ((NamedValueHost) object).getValue() != null;
0756: }
0757:
0758: if (object instanceof SimpleVariable) {
0759: return ((NamedValueHost) object).getValue() != null;
0760: }
0761: }
0762:
0763: if (object instanceof Node) {
0764: return true;
0765: }
0766:
0767: return false;
0768: }
0769:
0770: public String getCustomEditorValue(final Object object) {
0771: if (object instanceof NamedValueHost) {
0772: if (object instanceof WsdlMessageVariable) {
0773: return null; // shouldn't happen
0774: }
0775:
0776: if (object instanceof WsdlMessageValue.Part) {
0777: final Value value = ((NamedValueHost) object)
0778: .getValue();
0779:
0780: if (value instanceof XmlElementValue) {
0781: return XmlUtil.toString(((XmlElementValue) value)
0782: .getElement());
0783: } else {
0784: return getValue((WsdlMessageValue.Part) object);
0785: }
0786: }
0787:
0788: if (object instanceof XmlElementVariable) {
0789: final XmlElementValue value = (XmlElementValue) ((XmlElementVariable) object)
0790: .getValue();
0791:
0792: return XmlUtil.toString(value.getElement());
0793: }
0794:
0795: if (object instanceof SimpleVariable) {
0796: return getValue((SimpleVariable) object);
0797: }
0798: }
0799:
0800: if (object instanceof Node) {
0801: final Node node = (Node) object;
0802:
0803: if ((node instanceof Element)
0804: && !XmlUtil.isTextOnlyNode(node)) {
0805: return XmlUtil.toString(node);
0806: }
0807:
0808: return ((Node) object).getTextContent();
0809: }
0810:
0811: return NbBundle.getMessage(VariablesUtil.class,
0812: "VU_CannotResolveValue", object); // NOI18N
0813: }
0814:
0815: public String getCustomEditorMimeType(final Object object) {
0816: if (object instanceof NamedValueHost) {
0817: if (object instanceof WsdlMessageVariable) {
0818: return null; // shouldn't happen
0819: }
0820:
0821: if (object instanceof WsdlMessageValue.Part) {
0822: final Value value = ((NamedValueHost) object)
0823: .getValue();
0824:
0825: if (value instanceof XmlElementValue) {
0826: return "text/xml"; // NOI18N
0827: } else {
0828: return "text/plain"; // NOI18N
0829: }
0830: }
0831:
0832: if (object instanceof XmlElementVariable) {
0833: return "text/xml"; // NOI18N
0834: }
0835:
0836: if (object instanceof SimpleVariable) {
0837: return "text/plain"; // NOI18N
0838: }
0839: }
0840:
0841: if (object instanceof Node) {
0842: final Node node = (Node) object;
0843:
0844: if ((node instanceof Element)
0845: && !XmlUtil.isTextOnlyNode(node)) {
0846: return "text/xml";
0847: }
0848:
0849: return "text/plain"; // NOI18N
0850: }
0851:
0852: return "text/plain"; // NOI18N
0853: }
0854:
0855: // Miscellaneous ///////////////////////////////////////////////////////////
0856: public BpelModel getBpelModel() {
0857: final DebuggerEngine engine = DebuggerManager
0858: .getDebuggerManager().getCurrentEngine();
0859:
0860: if (engine == null) {
0861: return null;
0862: }
0863:
0864: final SourcePath sourcePath = engine.lookupFirst(null,
0865: SourcePath.class);
0866: final ProcessInstance instance = myDebugger
0867: .getCurrentProcessInstance();
0868:
0869: if ((sourcePath == null) || (instance == null)) {
0870: return null;
0871: }
0872:
0873: return EditorUtil.getBpelModel(sourcePath
0874: .getSourcePath(instance.getProcess().getQName()));
0875: }
0876:
0877: /**
0878: * Returns an instance of {@link Variable} which corresponds to the given
0879: * name. This method searches through the BPEL OM, according to the current
0880: * activity XPath and checks variables from the process itself and all
0881: * active scopes.
0882: *
0883: * @param name Name of the variable.
0884: * @return Instance of {@link Variable} which corresponds to the given
0885: * name.
0886: */
0887: public Variable getBpelVariable(final String name) {
0888: final List<Variable> variables = new LinkedList<Variable>();
0889:
0890: final BpelModel model = getBpelModel();
0891:
0892: if (model == null) {
0893: return null;
0894: }
0895:
0896: VariableContainer varsContainer = model.getProcess()
0897: .getVariableContainer();
0898:
0899: // Add the variables from the process
0900: if ((varsContainer != null)
0901: && (varsContainer.sizeOfVariable() > 0)) {
0902: variables.addAll(Arrays
0903: .asList(varsContainer.getVariables()));
0904: }
0905:
0906: final ProcessInstance currentInstance = myDebugger
0907: .getCurrentProcessInstance();
0908: if (currentInstance == null) {
0909: return null;
0910: }
0911:
0912: final Position currentPosition = currentInstance
0913: .getCurrentPosition();
0914: if (currentPosition == null) {
0915: return null;
0916: }
0917:
0918: final String xpath = currentPosition.getXpath();
0919:
0920: int scopeIndex = xpath.indexOf("scope"); // NOI18N
0921: while (scopeIndex != -1) {
0922: final int index = xpath.indexOf("/", scopeIndex); // NOI18N
0923:
0924: final String scopeXpath = index == -1 ? xpath : xpath
0925: .substring(0, index);
0926:
0927: final Scope scope = getScopeEntity(scopeXpath);
0928: if (scope != null) {
0929: varsContainer = scope.getVariableContainer();
0930:
0931: if ((varsContainer != null)
0932: && (varsContainer.sizeOfVariable() > 0)) {
0933:
0934: variables.addAll(Arrays.asList(varsContainer
0935: .getVariables()));
0936: }
0937: }
0938:
0939: scopeIndex = index == -1 ? index : xpath.indexOf("scope",
0940: index); // NOI18N
0941: }
0942:
0943: for (Variable variable : variables) {
0944: if (variable.getName().equals(name)) {
0945: return variable;
0946: }
0947: }
0948:
0949: return null;
0950: }
0951:
0952: public Scope getScopeEntity(final String xpath) {
0953:
0954: final BpelModel model = getBpelModel();
0955:
0956: if (model == null) {
0957: return null;
0958: }
0959:
0960: BpelEntity currentEntity = model.getProcess();
0961:
0962: final StringTokenizer tokenizer = new StringTokenizer(xpath,
0963: "/"); // NOI18N
0964: while (tokenizer.hasMoreTokens()) {
0965: String name = tokenizer.nextToken();
0966: int offset = 1;
0967:
0968: if (name.equals("")) { // NOI18N
0969: continue;
0970: }
0971:
0972: final int colonIndex = name.indexOf(":"); // NOI18N
0973: if (colonIndex > -1) {
0974: name = name.substring(colonIndex + 1);
0975: }
0976:
0977: final int openingBracketIndex = name.indexOf("["); // NOI18N
0978: if (openingBracketIndex > -1) {
0979: final int closingBracketIndex = name.lastIndexOf("]"); // NOI18N
0980:
0981: offset = Integer.parseInt(name.substring(
0982: openingBracketIndex + 1, closingBracketIndex));
0983: name = name.substring(0, openingBracketIndex);
0984: }
0985:
0986: if (name.equals("process")) { // NOI18N
0987: continue;
0988: }
0989:
0990: for (BpelEntity entity : currentEntity.getChildren()) {
0991: if (entity.getPeer().getNodeName().equals(name)) {
0992: offset--;
0993: }
0994:
0995: if (offset == 0) {
0996: currentEntity = entity;
0997: break;
0998: }
0999: }
1000: }
1001:
1002: return (Scope) (currentEntity instanceof Scope ? currentEntity
1003: : null);
1004:
1005: }
1006:
1007: public Part getWsdlMessagePart(final Variable variable,
1008: final String name) {
1009: final WSDLReference<Message> reference = variable
1010: .getMessageType();
1011:
1012: if (reference == null) {
1013: return null;
1014: }
1015:
1016: final Message message = reference.get();
1017: for (Part part : message.getParts()) {
1018: if (part.getName().equals(name)) {
1019: return part;
1020: }
1021: }
1022:
1023: return null;
1024: }
1025:
1026: public String formatShort(final QName qName) {
1027: return qName.getPrefix() + ":" + qName.getLocalPart(); // NOI18N
1028: }
1029:
1030: public String formatLong(final QName qName) {
1031: return "{" + qName.getNamespaceURI() + "} " + // NOI18N
1032: qName.getLocalPart();
1033: }
1034:
1035: ////////////////////////////////////////////////////////////////////////////
1036: // Constants
1037: private static final String ICONS_ROOT = "org/netbeans/modules/bpel/debugger/ui/resources/"
1038: + // NOI18N
1039: "image/variables/"; // NOI18N
1040:
1041: private static final String DEFAULT_VARIABLE_ICON = "org/netbeans/modules/debugger/resources/"
1042: + // NOI18N
1043: "localsView/LocalVariable"; // NOI18N
1044:
1045: private static final String WSDL_MESSAGE_VARIABLE_ICON = ICONS_ROOT
1046: + "VARIABLE_MESSAGE"; // NOI18N
1047:
1048: private static final String WSDL_MESSAGE_PART_ICON = ICONS_ROOT
1049: + "MESSAGE_PART"; // NOI18N
1050:
1051: private static final String XML_ELEMENT_VARIABLE_ICON = ICONS_ROOT
1052: + "VARIABLE_XML_ELEMENT"; // NOI18N
1053:
1054: private static final String SIMPLE_VARIABLE_ICON = ICONS_ROOT
1055: + "VARIABLE_SIMPLE"; // NOI18N
1056:
1057: private static final String DEFAULT_NODE_ICON = ICONS_ROOT
1058: + "DEFAULT_NODE"; // NOI18N
1059:
1060: private static final String ELEMENT_NODE_ICON = ICONS_ROOT
1061: + "ELEMENT_NODE"; // NOI18N
1062:
1063: private static final String ATTRIBUTE_NODE_ICON = ICONS_ROOT
1064: + "ATTRIBUTE_NODE"; // NOI18N
1065:
1066: private static final String TEXT_NODE_ICON = ICONS_ROOT
1067: + "TEXT_NODE"; // NOI18N
1068:
1069: private static final String CDATA_NODE_ICON = ICONS_ROOT
1070: + "CDATA_NODE"; // NOI18N
1071: }
|