001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *//*
041: * CodeClipUtilities.java
042: *
043: * Created on July 27, 2006, 10:16 AM
044: *
045: * This is a generic codeclip utilities class.
046: *
047: * @author Joelle Lam <joelle.lam@sun.com>
048: * @version %I%, %G%
049: */
050:
051: package org.netbeans.modules.visualweb.palette.codeclips;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055: import java.io.OutputStreamWriter;
056: import java.util.MissingResourceException;
057: import java.util.ResourceBundle;
058: import java.util.Vector;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.loaders.DataFolder;
064: import org.openide.util.NbBundle;
065:
066: public class CodeClipUtilities {
067:
068: private static final String LOC_MARKER = "~"; // NOI18N
069: private static final String PARAM_MARKER = "@"; // NOI18N
070:
071: /** Creates a new instance of CodeClipUtilities */
072: // public CodeClipUtilities() {
073: // }
074: /*
075: * create a new codeclip in the given category folder.
076: * @param categoryFolder DataFolder of the given category.
077: */
078: public static void createCodeClip(DataFolder categoryFolder) {
079: final FileObject categoryFile = categoryFolder.getPrimaryFile();
080: final String displayNameString = NbBundle.getMessage(
081: CodeClipUtilities.class, "CLIP");
082:
083: CodeClipViewerPanel snippetViewer = new CodeClipViewerPanel(
084: displayNameString, displayNameString, "");
085: snippetViewer.setVisible(true);
086: if (!snippetViewer.isCancelled()) {
087: try {
088: CodeClipUtilities.createCodeClipFile(categoryFile,
089: snippetViewer.getContentText(), snippetViewer
090: .getClipName(), null, snippetViewer
091: .getToolTip());
092: } catch (IOException ex) {
093: ErrorManager.getDefault().notify(ex);
094: } catch (MissingResourceException mre) {
095: ErrorManager.getDefault().notify(mre);
096: }
097: }
098: }
099:
100: public static String parseClipForParams(String title, String str) {
101: String clipStr = str;
102: Vector editedToken = new Vector();
103: // look for marker
104: int occ = clipStr.indexOf(PARAM_MARKER);
105: if (occ != -1) {
106: // we need two or more markers and a way to escape them
107: //String[] paramArray = new String[3];
108: Vector<String> paramArray = new Vector<String>();
109: String chopped = clipStr;
110: for (int i = 0; occ != -1; i++) {
111: int nextOcc = chopped.indexOf(PARAM_MARKER, occ + 1);
112:
113: if (nextOcc < 0) {
114: occ = -1;
115: } else if (((String) chopped
116: .substring(occ + 1, nextOcc)).contains(" ")) {
117: //This match is currently not picking up.
118: //It is an annotation
119: chopped = chopped.substring(nextOcc);
120: occ = nextOcc; //the is should equal chopped.indexOf(PARAM_MARKER)
121: } else {
122:
123: if (!paramArray.contains(chopped.substring(occ + 1,
124: nextOcc))) {
125: paramArray.add(chopped.substring(occ + 1,
126: nextOcc));
127: }
128: chopped = chopped.substring(nextOcc + 1);
129: occ = chopped.indexOf(PARAM_MARKER);
130: }
131: }
132: if (paramArray.size() == 0) {
133: return clipStr;
134: }
135: final CodeClipsParametersDialog paramEditor = new CodeClipsParametersDialog(
136: title, paramArray);
137: paramEditor.setVisible(true);
138: if (paramEditor.isCancelled()) {
139: return ""; // NOI18N
140: }
141:
142: if (!paramEditor.isCancelled()) {
143: editedToken = paramEditor.getNewParam();
144: }
145:
146: // insert token back in code clip the edited part
147: int j = 0;
148: while (j < paramArray.size()) {
149: if (paramArray.elementAt(j) != null) {
150: String paramElement = paramArray.elementAt(j);
151: String editedElement = editedToken.elementAt(j)
152: .toString();
153: clipStr = clipStr.replaceAll(PARAM_MARKER
154: + paramElement + PARAM_MARKER,
155: editedElement);
156: }
157: j++;
158: }
159: }
160:
161: return clipStr;
162: }
163:
164: public static String fillFromBundle(String body, String bundleName) {
165: String clipStr = body;
166: // look for marker
167: int occ = clipStr.indexOf(LOC_MARKER);
168: if (occ != -1) {
169: Vector<String> paramArray = new Vector<String>();
170: String chopped = clipStr;
171: for (int i = 0; occ != -1; i++) {
172: int nextOcc = chopped.indexOf(LOC_MARKER, occ + 1);
173: if (!paramArray.contains(chopped.substring(occ + 1,
174: nextOcc))) {
175: paramArray.add(chopped.substring(occ + 1, nextOcc));
176: }
177: chopped = chopped.substring(nextOcc + 1);
178: occ = chopped.indexOf(LOC_MARKER);
179: }
180: Vector<String> editedToken = new Vector<String>();
181: for (int i = 0; i < paramArray.size(); i++) {
182: try {
183: ResourceBundle ccbundle = NbBundle
184: .getBundle(bundleName);
185: editedToken.add(ccbundle.getString(paramArray
186: .elementAt(i)));
187: } catch (MissingResourceException mse) {
188: editedToken.add(LOC_MARKER
189: + paramArray.elementAt(i) + LOC_MARKER);
190: }
191: }
192: // insert token back in code clip the edited part
193: int j = 0;
194: while (j < paramArray.size()) {
195: if (paramArray.elementAt(j) != null) {
196: String paramElement = paramArray.elementAt(j);
197: String editedElement = editedToken.elementAt(j);
198: clipStr = clipStr.replaceAll(LOC_MARKER
199: + paramElement + LOC_MARKER, editedElement);
200: }
201: j++;
202: }
203: }
204: return clipStr;
205: }
206:
207: /**
208: * Creates a code clip and uses the displayNameString as the name without checking the bundle file.
209: */
210: public static void createCodeClipFile(FileObject folder,
211: String body, String displayNameString, String tooltip)
212: throws IOException {
213: String localizingBundle = "org.netbeans.modules.visualweb.palette.codeclips.Bundle";
214: createCodeClipFile(folder, body, displayNameString,
215: localizingBundle, tooltip);
216: }
217:
218: /**
219: * Creates a code clip
220: *
221: * @param folder the fileObject or category in which you wan the codeClipCreated
222: * @param body the body of the codeclip
223: * @param displayNameString the codeclip name (it will be localized if a valid bundle file is given)
224: * @param localizingBundle the bundle file for which to find the display name string.
225: * @param image the image file associated with this codeclip.
226: */
227: public static void createCodeClipFile(FileObject folder,
228: String body, String displayNameString, String bundleName,
229: String tooltip) throws IOException {
230:
231: String nameExt = "xml";
232: String image_16 = "org/netbeans/modules/visualweb/spi/palette/resources/Codesnippet_C16.png";
233: String image_32 = "org/netbeans/modules/visualweb/spi/palette/resources/Codesnippet_C32.png";
234:
235: String fileName = getFreeFileName(folder, "CLIP", nameExt); //NOI18N
236: // String fileName = getFreeFileName(folder, displayNameString, nameExt );
237: FileObject itemFile = folder.createData(fileName, "xml");
238: // This would be only necessary if I wanted to increment the "CLIP" name, however, if I do that, all edited clips would also take on their name.
239: // String displayName = fileName.replace('_',' ');
240: StringBuffer buff = new StringBuffer(512);
241:
242: buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); // NOI18N
243: buff
244: .append("<!DOCTYPE "
245: + CodeClipHandler.XML_ROOT
246: + " PUBLIC \"-//NetBeans//CodeClip Palette Item 1.0//EN\"\n"); // NOI18N
247: buff
248: .append("\"http://www.netbeans.org/dtds/codeclip-palette-item-1_0.dtd\">\n\n"); // NOI18N
249: buff.append(" <" + CodeClipHandler.XML_ROOT + " "
250: + CodeClipHandler.ATTR_VERSION + "=\""
251: + CodeClipHandler.LATEST_VERSION + "\">\n"); // NOI18N
252: buff.append(" <" + CodeClipHandler.TAG_BODY + ">\n"); // NOI18N
253: buff.append(" <![CDATA[\n"); //NOIl8N
254: buff.append(body + "\n");
255: buff.append(" ]]>\n"); //NOIl8N
256: buff.append(" </" + CodeClipHandler.TAG_BODY + ">\n"); // NOI18N
257: buff.append("<" + CodeClipHandler.TAG_ICON16 + " urlvalue=\""
258: + image_16 + "\"/>\n");
259: buff.append("<" + CodeClipHandler.TAG_ICON32 + " urlvalue=\""
260: + image_32 + "\"/>\n");
261: // This code line causes names to be fumbled.
262: // buff.append("<" + CodeClipHandler.TAG_DESCRIPTION + " display-name-key=\"" + displayName + "\" \n");
263: buff.append("<" + CodeClipHandler.TAG_DESCRIPTION
264: + " display-name-key=\"" + displayNameString + "\" \n");
265: if (bundleName != null) {
266: buff.append(" " + CodeClipHandler.ATTR_BUNDLE
267: + "=\"" + bundleName + "\" \n");
268: }
269: if (tooltip != null) {
270: buff.append(" "
271: + CodeClipHandler.ATTR_TOOLTIP_KEY + "=\""
272: + tooltip + "\" \n");
273: }
274: buff.append("/>\n");
275: buff.append("</" + CodeClipHandler.XML_ROOT + ">");
276:
277: FileLock lock = itemFile.lock();
278:
279: //OutputStreamWriter out = new OutputStreamWriter(new ByteArrayOutputStream(),"UTF-32");
280: OutputStream os = itemFile.getOutputStream(lock);
281:
282: OutputStreamWriter out = new OutputStreamWriter(os, "UTF8");
283: out.write(buff.toString());
284: out.close();
285: //os.write(buff.toString().getBytes());
286: os.close();
287: lock.releaseLock();
288: }
289:
290: public static String getFreeFileName(FileObject folder,
291: String filename, String nameExt) {
292: String myfilename = filename.replaceAll("\\W", "");
293: return FileUtil.findFreeFileName(folder, myfilename, nameExt);
294: }
295: }
|