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.xslt.mapper.methoid;
021:
022: import java.awt.Image;
023: import java.awt.Toolkit;
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.ResourceBundle;
028: import javax.swing.Icon;
029: import javax.swing.ImageIcon;
030: import org.netbeans.modules.soa.mapper.basicmapper.util.MapperUtilities;
031: import org.netbeans.modules.soa.mapper.basicmapper.methoid.BasicField;
032: import org.netbeans.modules.soa.mapper.basicmapper.methoid.BasicMethoid;
033: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.ILiteralUpdater;
034: import org.netbeans.modules.soa.mapper.common.basicmapper.literal.ILiteralUpdaterFactory;
035: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IField;
036: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoid;
037: import org.openide.ErrorManager;
038: import org.openide.filesystems.FileObject;
039: import org.openide.filesystems.Repository;
040:
041: /**
042: * This class is intended to load meta-information from the layer.xml
043: * The only public method returns the IMethoid object which contains all
044: * meta-information. The object of this type is required by Mapper Core
045: * as a transferable data for DnD operation
046: * (dragging an element from the palette to canvas view).
047: *
048: * @author nk160297
049: */
050:
051: public class MethoidLoader {
052:
053: public static IMethoid loadMethoid(FileObject fileObject) {
054: IMethoid methoid = generateMethoid(fileObject);
055: return methoid;
056: }
057:
058: public static IMethoid loadMethoid(String xpathOperator) {
059: FileObject metainfoFo = Repository.getDefault()
060: .getDefaultFileSystem().findResource(
061: Constants.XSLT_PALETTE_METAINFO);
062: for (FileObject subfolder : metainfoFo.getChildren()) {
063: for (FileObject methoidfile : subfolder.getChildren()) {
064: if (xpathOperator.equals(methoidfile.getName())) {
065: return MethoidLoader.loadMethoid(methoidfile);
066: }
067: }
068:
069: }
070: System.out.println("The methoid with the name \""
071: + xpathOperator + "\" can't be found"); // NOI18N
072: return null;
073:
074: }
075:
076: private static IField generateField(FileObject fo, String attrName,
077: String attrType, String attrToolTip, boolean isInput,
078: boolean isOutput, ResourceBundle bundle, boolean isLiteral,
079: ILiteralUpdaterFactory literalUpdaterFactory) {
080: String fieldName = "";
081: String fieldTooltip = "";
082: String fieldType = "";
083: String tooltipKey = "";
084:
085: if ((fieldName = (String) fo.getAttribute(attrName)) != null) {
086: if ((fieldName == null)
087: || (fieldName.length() == 0)
088: ||
089: // java expression, no "this"
090: (attrName.equals("Class") && fieldName
091: .equals("javaExp"))) {
092: return null;
093: }
094:
095: fieldType = (String) fo.getAttribute(attrType);
096: tooltipKey = (String) fo.getAttribute(attrToolTip);
097:
098: if (tooltipKey == null) {
099: ErrorManager.getDefault().log(
100: "Unable to find tooltip name:[" + attrToolTip
101: + "]"); // NOi18N
102: } else {
103: try {
104: fieldTooltip = bundle.getString(tooltipKey);
105: } catch (Exception e) {
106: }
107: }
108:
109: ILiteralUpdater literalUpdater = null;
110: if ((isInput || isLiteral) && literalUpdaterFactory != null) {
111: // literal updater will be non-null if field type supports literals
112: literalUpdater = literalUpdaterFactory
113: .createLiteralUpdater(fieldType);
114: }
115:
116: IField field = new BasicField(fieldName, fieldType,
117: fieldTooltip, null, isInput, isOutput,
118: literalUpdater);
119:
120: return field;
121: }
122:
123: return null;
124: }
125:
126: private static IMethoid generateMethoid(FileObject fo) {
127:
128: boolean isEditableLiteral = false;
129: Object literalObj = fo.getAttribute(Constants.LITERAL_FLAG);
130: if (literalObj != null && literalObj instanceof Boolean) {
131: isEditableLiteral = ((Boolean) literalObj).booleanValue();
132: }
133:
134: String bundleRef = (String) fo
135: .getAttribute(Constants.BUNDLE_CLASS);
136: ResourceBundle bundle = ResourceBundle.getBundle(bundleRef);
137:
138: // creating funtoid namespace (this) field
139: String fieldName = null;
140: String fieldTooltip = null;
141: String fieldType = null;
142: IField this Field = generateField(fo, Constants.INPUT_THIS,
143: Constants.THIS_CLASS, Constants.THIS_TOOLTIP, true,
144: false, bundle, isEditableLiteral, null);
145:
146: // creating input fields
147: List fieldList = new ArrayList();
148: IField field = null;
149: int i = 1;
150: int inputNum = 0;
151:
152: try {
153: inputNum = Integer.parseInt((String) fo
154: .getAttribute("InputNum"));
155: } catch (java.lang.Throwable t) {
156: t.printStackTrace(System.err);
157: }
158:
159: for (; i <= inputNum; i++) {
160: field = generateField(fo, Constants.INPUT_PARAM + i,
161: Constants.INPUT_TYPE + i, Constants.INPUT_TOOLTIP
162: + i, true, false, bundle,
163: isEditableLiteral, null);
164:
165: if (field != null) {
166: fieldList.add(field);
167: }
168: }
169:
170: List input = new ArrayList(fieldList);
171: fieldList.clear();
172:
173: // creating output fields
174: i = 1;
175:
176: int outputNum = 0;
177:
178: try {
179: outputNum = Integer.parseInt((String) fo
180: .getAttribute(Constants.OUTPUT_NUM));
181: } catch (java.lang.Throwable t) {
182: t.printStackTrace(System.err);
183: }
184:
185: for (; i <= outputNum; i++) {
186: field = generateField(fo, Constants.OUTPUT_PARAM + i,
187: Constants.OUTPUT_TYPE + i, Constants.OUTPUT_TOOLTIP
188: + i, false, true, bundle,
189: isEditableLiteral, null); // new BpelLiteralHandler()
190:
191: if (field != null) {
192: fieldList.add(field);
193: }
194: }
195:
196: List output = new ArrayList(fieldList);
197:
198: boolean isAccumulative = false;
199: Object accumObj = fo.getAttribute(Constants.ACCUMULATIVE);
200: if (accumObj != null && accumObj instanceof Boolean) {
201: isAccumulative = ((Boolean) accumObj).booleanValue();
202: }
203: //
204: String tooltip = (String) fo.getAttribute(Constants.TOOLTIP);
205: tooltip = bundle.getString(tooltip);
206: //
207: String name = (String) fo.getAttribute(Constants.LOCAL_NAME);
208: name = bundle.getString(name);
209: //
210: URL iconUrl = (URL) fo.getAttribute(Constants.FILE_ICON);
211: Image img = Toolkit.getDefaultToolkit().getImage(iconUrl);
212: Icon icon = new ImageIcon(img);
213: //
214: return new BasicMethoid(icon, MapperUtilities
215: .cutAmpersand(name), tooltip, fo, thisField, input,
216: output, isAccumulative, isEditableLiteral);
217: }
218: }
|