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: package org.netbeans.modules.visualweb.complib;
043:
044: import java.io.IOException;
045: import java.io.OutputStreamWriter;
046: import java.io.PrintWriter;
047: import java.net.URL;
048:
049: import javax.help.HelpSet;
050:
051: import org.openide.filesystems.FileLock;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileSystem;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.filesystems.Repository;
056:
057: /**
058: * Handle the installation and uninstallation of JavaHelp for a component
059: * library.
060: *
061: * @author Edwin Goei
062: */
063: class JavaHelpStorage {
064:
065: private static final FileObject fsRoot = Repository.getDefault()
066: .getDefaultFileSystem().getRoot();
067:
068: /**
069: * Install complib help into system.
070: *
071: * @param complib
072: * @throws IOException
073: */
074: static void installComplibHelp(ExtensionComplib complib)
075: throws IOException {
076: String helpSetFile = complib.getHelpSetFile();
077: if (helpSetFile == null) {
078: // No HelpSet file was specified so do nothing
079: return;
080: }
081:
082: URL hsUrl = HelpSet.findHelpSet(complib.getClassLoader(),
083: helpSetFile);
084: if (hsUrl == null) {
085: IdeUtil
086: .logWarning("Unable to access HelpSet file for component library: "
087: + helpSetFile);
088: }
089:
090: String refPath = getHelpSetRefPath(complib);
091: FileObject helpSetRefFO = FileUtil.createData(fsRoot, refPath);
092:
093: writeHelpSetRefFile(helpSetRefFO, hsUrl);
094: }
095:
096: /**
097: * Uninstall complib help from system if found else do nothing.
098: *
099: * @param complib
100: * @throws IOException
101: */
102: static void uninstallComplibHelp(ExtensionComplib complib)
103: throws IOException {
104: String helpsetrefPath = getHelpSetRefPath(complib);
105: FileObject helpSetRefFO = fsRoot.getFileObject(helpsetrefPath);
106: if (helpSetRefFO != null) {
107: helpSetRefFO.delete();
108: }
109: }
110:
111: private static String getHelpSetRefPath(ExtensionComplib complib) {
112: String helpsetrefPath = "Services/JavaHelp/"
113: + complib.getDirectoryBaseName() + "-helpset.xml";
114: return helpsetrefPath;
115: }
116:
117: private static void writeHelpSetRefFile(
118: final FileObject helpSetRefFO, final URL hsUrl)
119: throws IOException {
120: helpSetRefFO.getFileSystem().runAtomicAction(
121: new FileSystem.AtomicAction() {
122: public void run() throws IOException {
123: writeHelpSetRef(helpSetRefFO, hsUrl);
124: }
125: });
126: }
127:
128: private static void writeHelpSetRef(FileObject definitionFile,
129: URL hsUrl) throws IOException {
130: FileLock lock = null;
131: PrintWriter out = null;
132: try {
133: lock = definitionFile.lock();
134: out = new PrintWriter(new OutputStreamWriter(definitionFile
135: .getOutputStream(lock), "UTF-8"));
136: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
137: out
138: .println("<!DOCTYPE helpsetref PUBLIC \"-//NetBeans//DTD JavaHelp Help Set Reference 1.0//EN\" \"http://www.netbeans.org/dtds/helpsetref-1_0.dtd\">"); // NOI18N
139: out.println("<helpsetref url=\"" + hsUrl.toExternalForm()
140: + "\"/>"); // NOI18N
141: } finally {
142: if (out != null)
143: out.close();
144: if (lock != null)
145: lock.releaseLock();
146: }
147: }
148: }
|