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.api.portlet.dd;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import org.openide.ErrorManager;
047: import javax.xml.parsers.ParserConfigurationException;
048: import org.xml.sax.SAXException;
049:
050: /**
051: *
052: * @author David Botterill
053: */
054: public class PortletDDHelper {
055:
056: private File ddFile;
057: private PortletApp portletApp;
058:
059: public PortletDDHelper(File inDDFile) {
060: ddFile = inDDFile;
061: /**
062: * Create the portlet application
063: */
064:
065: try {
066: portletApp = PortletApp.read(ddFile);
067: } catch (IOException ioe) {
068: ErrorManager.getDefault().notify(ioe);
069: } catch (ParserConfigurationException pce) {
070: ErrorManager.getDefault().notify(pce);
071: } catch (SAXException se) {
072: ErrorManager.getDefault().notify(se);
073: }
074: }
075:
076: /**
077: * This method will set the portlet name
078: * @param oldName the current name of the portlet to change.
079: * @param newName the new name to give to the portlet.
080: */
081: public void setPortletName(String oldName, String newName) {
082: try {
083: if (null != portletApp) {
084: /**
085: * Now get the Portlets in the portlet application.
086: * NOTE- we only expect there to be one portlet per project so
087: * we will only deal with the first portlet.
088: */
089: PortletType[] portletType = portletApp.getPortlet();
090: for (int ii = 0; null != portletType
091: && ii < portletType.length; ii++) {
092: String currentPortletName = portletType[ii]
093: .getPortletName();
094: if (currentPortletName.equals(oldName)) {
095: portletType[ii].setPortletName(newName);
096: break;
097: }
098: }
099: portletApp.write(ddFile);
100: }
101: } catch (IOException ioe) {
102: ErrorManager.getDefault().notify(ioe);
103: }
104:
105: }
106:
107: /**
108: * This method will set the initial page in the portlet.xml file.
109: * @param inMode The MODE to set for the initial page.
110: * @param inRelativePathPageName This is page to be set as the initial page for the mode.
111: * The page must include the relative path. This will usually be a "/" but could
112: * also include a subfolder. For example, if the absolute path of the page is
113: * /home/david/Creator/Projects/Portlet1/web/edit/EditPage.jsp, this parameter should
114: * be "/edit/EditPage.jsp".
115: */
116: public void setInitialPage(PortletModeType inMode,
117: String inRelativePathPageName) {
118: /**
119: * First unset the given page so we don't have leftover
120: */
121: this .unsetInitialPage(inRelativePathPageName);
122:
123: try {
124:
125: if (null != portletApp) {
126: /**
127: * Now get the Portlets in the portlet application.
128: * NOTE- we only expect there to be one portlet per project so
129: * we will only deal with the first portlet.
130: */
131: PortletType[] portletType = portletApp.getPortlet();
132: if (portletType != null && portletType.length > 0) {
133: InitParamType[] initParams = portletType[0]
134: .getInitParam();
135:
136: /**
137: * Now look for the init-param currently set with the initial page.
138: */
139: boolean foundMode = false;
140: for (int ii = 0; null != initParams
141: && ii < initParams.length; ii++) {
142:
143: if (inMode.equals(PortletModeType.VIEW)) {
144: if (initParams[ii].getName().equals(
145: JsfPortletDDProperties.INIT_VIEW)) {
146: foundMode = true;
147: initParams[ii]
148: .setValue(inRelativePathPageName);
149: }
150: } else if (inMode.equals(PortletModeType.EDIT)) {
151: if (initParams[ii].getName().equals(
152: JsfPortletDDProperties.INIT_EDIT)) {
153: foundMode = true;
154: initParams[ii]
155: .setValue(inRelativePathPageName);
156: }
157: } else if (inMode.equals(PortletModeType.HELP)) {
158: if (initParams[ii].getName().equals(
159: JsfPortletDDProperties.INIT_HELP)) {
160: foundMode = true;
161: initParams[ii]
162: .setValue(inRelativePathPageName);
163: }
164: }
165:
166: }
167:
168: if (!foundMode) {
169: /**
170: * The INIT for the page didn't exist so we need to add it.
171: */
172: InitParamType initParam = new InitParamType();
173: if (inMode.equals(PortletModeType.VIEW)) {
174: initParam
175: .setName(JsfPortletDDProperties.INIT_VIEW);
176: } else if (inMode.equals(PortletModeType.EDIT)) {
177: initParam
178: .setName(JsfPortletDDProperties.INIT_EDIT);
179: } else if (inMode.equals(PortletModeType.HELP)) {
180: initParam
181: .setName(JsfPortletDDProperties.INIT_HELP);
182: }
183: initParam.setValue(inRelativePathPageName);
184: portletType[0].addInitParam(initParam);
185:
186: }
187:
188: /**
189: * If the the supported mode is not listed, list it.
190: */
191: SupportsType[] supportsTypes = portletType[0]
192: .getSupports();
193:
194: boolean foundSupports = false;
195: boolean foundPortletMode = false;
196: if (null != supportsTypes
197: && supportsTypes.length > 0) {
198: foundSupports = true;
199: }
200:
201: for (int ii = 0; null != supportsTypes
202: && ii < supportsTypes.length; ii++) {
203: String[] modes = supportsTypes[ii]
204: .getPortletMode();
205: for (int jj = 0; null != modes
206: && jj < modes.length; jj++) {
207: if (modes[jj]
208: .equalsIgnoreCase(PortletModeType.VIEW
209: .toString())
210: && inMode
211: .equals(PortletModeType.VIEW)) {
212: foundPortletMode = true;
213: } else if (modes[jj]
214: .equalsIgnoreCase(PortletModeType.EDIT
215: .toString())
216: && inMode
217: .equals(PortletModeType.EDIT)) {
218: foundPortletMode = true;
219: } else if (modes[jj]
220: .equalsIgnoreCase(PortletModeType.HELP
221: .toString())
222: && inMode
223: .equals(PortletModeType.HELP)) {
224: foundPortletMode = true;
225: }
226: }
227: }
228:
229: if (!foundPortletMode) {
230: SupportsType supportsType = null;
231: /**
232: * If we found a SupportsType use it, otherwise create one.
233: */
234: if (!foundSupports) {
235: supportsType = new SupportsType();
236: } else {
237: supportsType = supportsTypes[0];
238: }
239:
240: supportsType.addPortletMode(inMode.toString());
241: }
242:
243: }
244: portletApp.write(ddFile);
245: }
246: } catch (IOException ioe) {
247: ErrorManager.getDefault().notify(ioe);
248: }
249:
250: }
251:
252: /**
253: * This method will unset the initial page in the portlet.xml file.
254: * @param inPageName This is page to be unset as any and all initial page.
255: * The page must include the relative path. This will usually be a "/" but could
256: * also include a subfolder. For example, if the absolute path of the page is
257: * /home/david/Creator/Projects/Portlet1/web/edit/EditPage.jsp, this parameter should
258: * be "/edit/EditPage.jsp".
259: */
260: public void unsetInitialPage(String inPageName) {
261: try {
262:
263: if (null != portletApp) {
264: /**
265: * Now get the Portlets in the portlet application.
266: * NOTE- we only expect there to be one portlet per project so
267: * we will only deal with the first portlet.
268: */
269: PortletType[] portletType = portletApp.getPortlet();
270: if (portletType != null && portletType.length > 0) {
271: InitParamType[] initParams = portletType[0]
272: .getInitParam();
273:
274: boolean foundMode = false;
275: String modeToRemove = null;
276: for (int ii = 0; null != initParams
277: && ii < initParams.length; ii++) {
278: if (initParams[ii].getValue()
279: .equals(inPageName)) {
280: portletType[0]
281: .removeInitParam(initParams[ii]);
282: modeToRemove = initParams[ii].getName();
283: }
284: }
285: /**
286: * Now remove the supported mode
287: */
288: SupportsType[] supportsTypes = portletType[0]
289: .getSupports();
290: for (int ii = 0; null != supportsTypes
291: && null != modeToRemove
292: && ii < supportsTypes.length; ii++) {
293:
294: if (modeToRemove
295: .equals(JsfPortletDDProperties.INIT_VIEW)) {
296: supportsTypes[ii]
297: .removePortletMode(PortletModeType.VIEW
298: .toString());
299: } else if (modeToRemove
300: .equals(JsfPortletDDProperties.INIT_EDIT)) {
301: supportsTypes[ii]
302: .removePortletMode(PortletModeType.EDIT
303: .toString());
304: } else if (modeToRemove
305: .equals(JsfPortletDDProperties.INIT_HELP)) {
306: supportsTypes[ii]
307: .removePortletMode(PortletModeType.HELP
308: .toString());
309: }
310: }
311:
312: }
313: }
314: portletApp.write(ddFile);
315:
316: } catch (IOException ioe) {
317: ErrorManager.getDefault().notify(ioe);
318: }
319:
320: }
321:
322: /**
323: * This method will determine if the given page is the initial page for the given mode.
324: * @param inMode This is the mode to check against.
325: * @param inPageName This is page to be check as the initial page for the given mode.
326: * The page must include the relative path. This will usually be a "/" but could
327: * also include a subfolder. For example, if the absolute path of the page is
328: * /home/david/Creator/Projects/Portlet1/web/edit/EditPage.jsp, this parameter should
329: * be "/edit/EditPage.jsp".
330: */
331: public boolean isInitialPage(PortletModeType inMode,
332: String inPageName) {
333: boolean isPage = false;
334: if (null != portletApp) {
335: /**
336: * Now get the Portlets in the portlet application.
337: * NOTE- we only expect there to be one portlet per project so
338: * we will only deal with the first portlet.
339: */
340: PortletType[] portletType = portletApp.getPortlet();
341: if (portletType != null && portletType.length > 0) {
342: InitParamType[] initParams = portletType[0]
343: .getInitParam();
344: for (int ii = 0; null != initParams
345: && ii < initParams.length; ii++) {
346: if (inMode.equals(PortletModeType.VIEW)) {
347: if (initParams[ii].getName().equals(
348: JsfPortletDDProperties.INIT_VIEW)) {
349: String pageValue = initParams[ii]
350: .getValue();
351: if (null != pageValue) {
352:
353: if (pageValue.equals(inPageName)) {
354: isPage = true;
355: }
356: }
357: }
358: } else if (inMode.equals(PortletModeType.EDIT)) {
359: if (initParams[ii].getName().equals(
360: JsfPortletDDProperties.INIT_EDIT)) {
361: String pageValue = initParams[ii]
362: .getValue();
363: if (null != pageValue) {
364:
365: if (pageValue.equals(inPageName)) {
366: isPage = true;
367: }
368: }
369: }
370: } else if (inMode.equals(PortletModeType.HELP)) {
371: if (initParams[ii].getName().equals(
372: JsfPortletDDProperties.INIT_HELP)) {
373: String pageValue = initParams[ii]
374: .getValue();
375: if (null != pageValue) {
376:
377: if (pageValue.equals(inPageName)) {
378: isPage = true;
379: }
380: }
381: }
382: }
383: }
384: }
385:
386: }
387: return isPage;
388: }
389:
390: /**
391: * This method will check whether a given page is an initial page for any mode.
392: * @param inFileObject The FileObject of the file to set check as the initial page.
393: * @return the PortletModeType the page is the initial page for. If the page is not an initial page,
394: * null is returned.
395: */
396: public PortletModeType isInitialPage(String inPageName) {
397: PortletModeType returnMode = null;
398: if (null != portletApp) {
399: /**
400: * Now get the Portlets in the portlet application.
401: * NOTE- we only expect there to be one portlet per project so
402: * we will only deal with the first portlet.
403: */
404: PortletType[] portletType = portletApp.getPortlet();
405: if (portletType != null && portletType.length > 0) {
406: InitParamType[] initParams = portletType[0]
407: .getInitParam();
408: for (int ii = 0; null != initParams
409: && ii < initParams.length; ii++) {
410: String pageValue = initParams[ii].getValue();
411: if (null != pageValue
412: && pageValue.equals(inPageName)) {
413: if (initParams[ii].getName().equals(
414: JsfPortletDDProperties.INIT_VIEW)) {
415: returnMode = PortletModeType.VIEW;
416: } else if (initParams[ii].getName().equals(
417: JsfPortletDDProperties.INIT_EDIT)) {
418: returnMode = PortletModeType.EDIT;
419: } else if (initParams[ii].getName().equals(
420: JsfPortletDDProperties.INIT_HELP)) {
421: returnMode = PortletModeType.HELP;
422: }
423: }
424: }
425: }
426: }
427: return returnMode;
428: }
429:
430: /**
431: * This method will determine get the initial page for the given mode.
432: * @param inMode This is the mode to check against.
433: */
434:
435: public String getInitialPage(PortletModeType inMode) {
436: String returnPage = null;
437:
438: if (null != portletApp) {
439: /**
440: * Now get the Portlets in the portlet application.
441: * NOTE- we only expect there to be one portlet per project so
442: * we will only deal with the first portlet.
443: */
444: PortletType[] portletType = portletApp.getPortlet();
445: if (portletType != null && portletType.length > 0) {
446: InitParamType[] initParams = portletType[0]
447: .getInitParam();
448: for (int ii = 0; null != initParams
449: && ii < initParams.length; ii++) {
450: if (inMode.equals(PortletModeType.VIEW)) {
451: if (initParams[ii].getName().equals(
452: JsfPortletDDProperties.INIT_VIEW)) {
453: returnPage = initParams[ii].getValue();
454: }
455: } else if (inMode.equals(PortletModeType.EDIT)) {
456: if (initParams[ii].getName().equals(
457: JsfPortletDDProperties.INIT_EDIT)) {
458: returnPage = initParams[ii].getValue();
459: }
460: } else if (inMode.equals(PortletModeType.HELP)) {
461: if (initParams[ii].getName().equals(
462: JsfPortletDDProperties.INIT_HELP)) {
463: returnPage = initParams[ii].getValue();
464: }
465: }
466: }
467: }
468:
469: }
470: return returnPage;
471: }
472: }
|