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.project.jsf.libraries;
043:
044: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectConstants;
045: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.net.URL;
050: import java.util.List;
051: import java.util.ArrayList;
052: import org.netbeans.api.java.classpath.ClassPath;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.libraries.Library;
055: import org.netbeans.api.project.libraries.LibraryManager;
056: import org.openide.modules.InstalledFileLocator;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.filesystems.FileStateInvalidException;
059:
060: /**
061: *
062: * @author Po-Ting Wu
063: */
064: public class JsfProjectLibrary {
065:
066: // JSF 1.1 support libraries for both Compile and Deploy
067: public static final String[] ALLTIME_LIBS_JSF11 = {
068: "jsf12-support", "webui", };
069:
070: // JSF 1.1 support libraries for Compile only
071: public static final String[] DESIGNTIME_LIBS_JSF11 = {};
072:
073: // JSF 1.1 support libraries for Deploy only
074: public static final String[] RUNTIME_LIBS_JSF11 = { "exceptionhandler-runtime", };
075:
076: // JSF 1.2 support libraries for both Compile and Deploy
077: public static final String[] ALLTIME_LIBS_JSF12 = {
078: "jsf12-support", "woodstock-components", };
079:
080: // JSF 1.2 support libraries for Compile only
081: public static final String[] DESIGNTIME_LIBS_JSF12 = {};
082:
083: // JSF 1.2 support libraries for Deploy only
084: public static final String[] RUNTIME_LIBS_JSF12 = { "exceptionhandler-runtime", };
085:
086: public static final String DEFAULT_JSF11_THEME = "theme-default";
087: public static final String DEFAULT_JSF12_THEME = "woodstock-theme-default";
088:
089: public static void addLibrary(Project project) throws IOException {
090: // Add the JSF support libraries to the project
091: LibraryManager libMgr = LibraryManager.getDefault();
092: String[] alltimeList;
093: String[] designtimeList;
094: String[] runtimeList;
095: Library[] alltimeLibs;
096: Library[] designtimeLibs;
097: Library[] runtimeLibs;
098: String defaultTheme;
099:
100: if (JsfProjectUtils.isJavaEE5Project(project)) {
101: defaultTheme = DEFAULT_JSF12_THEME;
102: alltimeList = ALLTIME_LIBS_JSF12;
103: designtimeList = DESIGNTIME_LIBS_JSF12;
104: runtimeList = RUNTIME_LIBS_JSF12;
105: } else {
106: defaultTheme = DEFAULT_JSF11_THEME;
107: alltimeList = ALLTIME_LIBS_JSF11;
108: designtimeList = DESIGNTIME_LIBS_JSF11;
109: runtimeList = RUNTIME_LIBS_JSF11;
110: }
111:
112: JsfProjectUtils.createProjectProperty(project,
113: JsfProjectConstants.PROP_JSF_PROJECT_LIBRARIES_DIR,
114: JsfProjectConstants.PATH_LIBRARIES);
115: JsfProjectUtils.createProjectProperty(project,
116: JsfProjectConstants.PROP_CURRENT_THEME, defaultTheme);
117:
118: alltimeLibs = new Library[alltimeList.length + 1];
119: for (int i = 0; i < alltimeList.length; i++) {
120: alltimeLibs[i] = libMgr.getLibrary(alltimeList[i]);
121: }
122: alltimeLibs[alltimeList.length] = libMgr
123: .getLibrary(defaultTheme);
124:
125: designtimeLibs = new Library[designtimeList.length];
126: for (int i = 0; i < designtimeList.length; i++) {
127: designtimeLibs[i] = libMgr.getLibrary(designtimeList[i]);
128: }
129:
130: runtimeLibs = new Library[runtimeList.length];
131: for (int i = 0; i < runtimeList.length; i++) {
132: runtimeLibs[i] = libMgr.getLibrary(runtimeList[i]);
133: }
134:
135: JsfProjectUtils.addLibraryReferences(project, alltimeLibs);
136: JsfProjectUtils.addLibraryReferences(project, designtimeLibs,
137: ClassPath.COMPILE);
138: JsfProjectUtils.addLibraryReferences(project, runtimeLibs,
139: ClassPath.EXECUTE);
140:
141: updateLocalizedRoots(project);
142: }
143:
144: public static void updateLocalizedRoots(Project project)
145: throws IOException {
146: // Add the localized JSF support jar files to the project
147: String[] alltimeList;
148: String[] designtimeList;
149: String[] runtimeList;
150: URL[] locAlltimeList;
151: URL[] locDesigntimeList;
152: URL[] locRuntimeList;
153:
154: if (JsfProjectUtils.isJavaEE5Project(project)) {
155: alltimeList = ALLTIME_LIBS_JSF12;
156: designtimeList = DESIGNTIME_LIBS_JSF12;
157: runtimeList = RUNTIME_LIBS_JSF12;
158: } else {
159: alltimeList = ALLTIME_LIBS_JSF11;
160: designtimeList = DESIGNTIME_LIBS_JSF11;
161: runtimeList = RUNTIME_LIBS_JSF11;
162: }
163:
164: locAlltimeList = getLocaleRoots(project, alltimeList);
165: locDesigntimeList = getLocaleRoots(project, designtimeList);
166: locRuntimeList = getLocaleRoots(project, runtimeList);
167:
168: JsfProjectUtils.addLocalizedRoots(project, locAlltimeList,
169: ClassPath.COMPILE);
170: JsfProjectUtils.addLocalizedRoots(project, locDesigntimeList,
171: ClassPath.COMPILE);
172: JsfProjectUtils.addLocalizedRoots(project, locRuntimeList,
173: ClassPath.EXECUTE);
174:
175: String defaultTheme = JsfProjectUtils.getProjectProperty(
176: project, JsfProjectConstants.PROP_CURRENT_THEME);
177: JsfProjectUtils.addLocalizedTheme(project, defaultTheme);
178: }
179:
180: public static URL getLocalizedThemeRoot(Project project,
181: String themeName) {
182: try {
183: URL[] list = getLocaleRoots(project,
184: new String[] { themeName });
185: if (list.length == 0) {
186: return null;
187: }
188: return list[0];
189: } catch (IOException e) {
190: return null;
191: }
192: }
193:
194: private static URL[] getLocaleRoots(Project project,
195: String[] libNames) throws IOException {
196: ArrayList<URL> list = new ArrayList<URL>();
197:
198: for (String libName : libNames) {
199: Library lib = LibraryManager.getDefault().getLibrary(
200: libName);
201: if (lib == null) {
202: continue;
203: }
204:
205: List<URL> ulist = lib.getContent("classpath"); // NOI18N
206: for (URL url : ulist) {
207: if (!"jar".equals(url.getProtocol())) {
208: continue;
209: }
210: url = FileUtil.getArchiveFile(url);
211: String name = url.getPath();
212: int index = name.lastIndexOf("/");
213: // exclude first slash:
214: name = name.substring(1, index) + "/locale"
215: + name.substring(index); // NOI18N
216: File f = InstalledFileLocator.getDefault().locate(name,
217: null, true);
218: if (f != null) {
219: list.add(FileUtil.getArchiveRoot(
220: FileUtil.toFileObject(f)).getURL());
221: }
222: }
223: }
224:
225: return list.toArray(new URL[list.size()]);
226: }
227:
228: public static boolean isDesigntimeLib(String name) {
229: if (name == null) {
230: return false;
231: }
232:
233: return name.startsWith("${libs.")
234: && name.endsWith("-designtime.classpath}");
235: }
236: }
|