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: /*
042: * CodeClipEnvironmentProvider.java
043: *
044: * Created on July 27, 2006, 10:16 AM
045: *
046: * The is the environment provider for all codeclips. In the System FileSystem
047: * this file is specified to be used whenever loading codeclips as defined by
048: * the codeclip dtd.
049: *
050: * @author Joelle Lam <joelle.lam@sun.com>
051: * @version %I%, %G%
052: * @see layer.xml
053: */
054:
055: package org.netbeans.modules.visualweb.palette.codeclips;
056:
057: import java.io.IOException;
058: import java.io.InputStream;
059: import java.io.InputStreamReader;
060: import java.lang.ref.Reference;
061: import java.lang.ref.WeakReference;
062: import java.util.ArrayList;
063: import org.openide.ErrorManager;
064: import org.openide.filesystems.FileObject;
065: import org.openide.loaders.DataNode;
066: import org.openide.loaders.DataObject;
067: import org.openide.loaders.Environment;
068: import org.openide.loaders.XMLDataObject;
069: import org.openide.nodes.Children;
070: import org.openide.nodes.Node;
071: import org.openide.util.Lookup;
072: import org.openide.util.lookup.AbstractLookup;
073: import org.openide.util.lookup.InstanceContent;
074: import org.openide.xml.EntityCatalog;
075: import org.openide.xml.XMLUtil;
076: import org.xml.sax.InputSource;
077: import org.xml.sax.SAXException;
078: import org.xml.sax.XMLReader;
079:
080: /**
081: *
082:
083: */
084: public class CodeClipEnvironmentProvider implements
085: Environment.Provider {
086:
087: private static CodeClipEnvironmentProvider createProvider() {
088: return new CodeClipEnvironmentProvider();
089: }
090:
091: private CodeClipEnvironmentProvider() {
092: }
093:
094: // ---------------- Environment.Provider ----------------------------
095:
096: public Lookup getEnvironment(DataObject obj) {
097:
098: CodeClipNodeFactory nodeFactory = new CodeClipNodeFactory(
099: (XMLDataObject) obj);
100: return nodeFactory.getLookup();
101: }
102:
103: private static class CodeClipNodeFactory implements
104: InstanceContent.Convertor {
105:
106: private XMLDataObject xmlDataObject = null;
107:
108: private Lookup lookup = null;
109:
110: Reference<CodeClipItemNode> refNode = new WeakReference<CodeClipItemNode>(
111: null);
112:
113: CodeClipNodeFactory(XMLDataObject obj) {
114:
115: xmlDataObject = obj;
116:
117: InstanceContent content = new InstanceContent();
118: content.add(Node.class, this );
119:
120: lookup = new AbstractLookup(content);
121: }
122:
123: Lookup getLookup() {
124: return lookup;
125: }
126:
127: // ---------------- InstanceContent.Convertor ----------------------------
128:
129: public Class type(Object obj) {
130: return (Class) obj;
131: }
132:
133: public String id(Object obj) {
134: return obj.toString();
135: }
136:
137: public String displayName(Object obj) {
138: return ((Class) obj).getName();
139: }
140:
141: public Object convert(Object obj) {
142: Object o = null;
143: if (obj == Node.class) {
144: try {
145: o = getInstance();
146: } catch (Exception ex) {
147: ErrorManager.getDefault().notify(ex);
148: }
149: }
150:
151: return o;
152: }
153:
154: // ---------------- helper methods ----------------------------
155:
156: public synchronized CodeClipItemNode getInstance() {
157:
158: CodeClipItemNode node = refNode.get();
159: if (node != null)
160: return node;
161:
162: FileObject file = xmlDataObject.getPrimaryFile();
163: if (file.getSize() == 0L) // item file is empty
164: return null;
165:
166: CodeClipHandler handler = new CodeClipHandler();
167: try {
168: XMLReader reader = XMLUtil.createXMLReader(true);
169: reader.setContentHandler(handler);
170: reader.setEntityResolver(EntityCatalog.getDefault());
171: String urlString = xmlDataObject.getPrimaryFile()
172: .getURL().toExternalForm();
173: InputStream inputStream = xmlDataObject
174: .getPrimaryFile().getInputStream();
175: InputStreamReader in = new InputStreamReader(
176: inputStream, "UTF8");
177: InputSource is = new InputSource(in);
178: //Force UTF8 Reading.
179: //InputSource is = new InputSource(xmlDataObject.getPrimaryFile().getInputStream());
180: is.setSystemId(urlString);
181: reader.parse(is);
182: } catch (SAXException saxe) {
183: ErrorManager.getDefault().notify(
184: ErrorManager.INFORMATIONAL, saxe);
185: } catch (IOException ioe) {
186: ErrorManager.getDefault().notify(
187: ErrorManager.INFORMATIONAL, ioe);
188: }
189:
190: node = createPaletteItemNode(handler);
191: refNode = new WeakReference<CodeClipItemNode>(node);
192:
193: return node;
194: }
195:
196: private CodeClipItemNode createPaletteItemNode(
197: CodeClipHandler handler) {
198:
199: String name = xmlDataObject.getName();
200: InstanceContent ic = new InstanceContent();
201:
202: // if (handler.getBody() != null && handler.getBundleName() != null) {
203: if (handler.getBody() != null) {
204: ArrayList<Object> codeclipArray = new ArrayList<Object>(
205: 2);
206: codeclipArray.add(0, handler.getBundleName());
207: codeclipArray.add(1, handler.getBody());
208: codeclipArray.add(2, handler.getDisplayNameKey());
209: ic.add(codeclipArray, ActiveEditorDropCodeClipProvider
210: .getInstance());
211: }
212:
213: CodeClipItemNode node = new CodeClipItemNode(new DataNode(
214: xmlDataObject, Children.LEAF), name, handler
215: .getBundleName(), handler.getDisplayNameKey(),
216: handler.getTooltipKey(), handler.getIcon16URL(),
217: handler.getIcon32URL(), handler.getBody(), ic);
218:
219: return node;
220: }
221: }
222:
223: }
|