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: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common
009: * Development and Distribution License("CDDL") (collectively, the
010: * "License"). You may not use this file except in compliance with the
011: * License. You can obtain a copy of the License at
012: * http://www.netbeans.org/cddl-gplv2.html
013: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: * specific language governing permissions and limitations under the
015: * License. When distributing the software, include this License Header
016: * Notice in each file and include the License file at
017: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: * particular file as subject to the "Classpath" exception as provided
019: * by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the
021: * License Header, with the fields enclosed by brackets [] replaced by
022: * your own identifying information:
023: * "Portions Copyrighted 2006 Ricoh Corporation"
024: *
025: * Contributor(s):
026: *
027: * The Original Software is NetBeans. The Initial Developer of the Original
028: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
029: * Microsystems, Inc. All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: */
042:
043: package ricoh.util.dom;
044:
045: import java.io.File;
046: import javax.xml.parsers.DocumentBuilder;
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import javax.xml.parsers.ParserConfigurationException;
049: import org.w3c.dom.*;
050: import org.xml.sax.SAXException;
051:
052: /**
053: *
054: * @author esanchez
055: */
056: public class DalpDOMHandler extends DOMHandler {
057: public static final String DEFAULT_DALP_VERSION_STR = "0.10";
058: public static final String DEFAULT_BASEPATH = "current";
059: public static final String DEFAULT_LOCATION = "jar";
060: public static final String DSDK_VERSION_STR = "1.0";
061: public static final String APP_VISIBLE_STR = "true";
062: public static final String INSTALL_MODE = "manual";
063: public static final String INSTALL_DEST = "hdd";
064:
065: public static final int DIGITS_IN_UID = 8;
066:
067: private String appName = null, appVersion = null, email = null,
068: description = null, detailDescription = null, fax = null,
069: iconPath = null, mainClass = null, phone = null,
070: targetJar = null, uid = null, vendor = null,
071: dalpSpecVer = null;
072:
073: private String seperator;
074:
075: /** Creates a new instance of DalpDOMHandler */
076: public DalpDOMHandler() {
077: super ();
078: setCompression(false);
079: }
080:
081: public void setDalpSpecVersion(String ver) {
082: dalpSpecVer = ver;
083: }
084:
085: public String getDalpSpecVersion() {
086: return dalpSpecVer;
087: }
088:
089: public void setAppID(String uid) {
090: this .uid = uid;
091: }
092:
093: public String getAppID() {
094: return this .uid;
095: }
096:
097: public void setXletClass(String mainClass) {
098: this .mainClass = mainClass;
099: }
100:
101: public String getXletClass() {
102: return mainClass;
103: }
104:
105: public void setEmail(String email) {
106: this .email = email;
107: }
108:
109: public String getEmail() {
110: return email;
111: }
112:
113: public void setFax(String fax) {
114: this .fax = fax;
115: }
116:
117: public String getFax() {
118: return fax;
119: }
120:
121: public void setTelephone(String number) {
122: this .phone = number;
123: }
124:
125: public String getTelephone() {
126: return phone;
127: }
128:
129: public String getAppName() {
130: return this .appName;
131: }
132:
133: public void setAppName(String newName) {
134: this .appName = newName;
135: }
136:
137: public void setAppVersion(String newVer) {
138: this .appVersion = newVer;
139: }
140:
141: public String getAppVersion() {
142: return this .appVersion;
143: }
144:
145: public void setIconPath(String iconPath) {
146: this .iconPath = iconPath;
147: if (iconPath.indexOf("/") == -1)
148: seperator = "\\";
149: else
150: seperator = "/";
151: }
152:
153: public void setTargetJar(String target) {
154: targetJar = target;
155: }
156:
157: public String getTargetJar() {
158: return this .targetJar;
159: }
160:
161: public boolean failRequiredInfo() {
162: return !(isValidUid(this .uid));
163: }
164:
165: public static boolean isValidUid(String num) {
166: if (num == null)
167: return false;
168: try {
169: if (Integer.parseInt(num) < 0)
170: return false;
171: } catch (NumberFormatException nfe) {
172: return false;
173: }
174:
175: return true;
176: }
177:
178: public Document loadDocument(File xmlFile) {
179: DocumentBuilderFactory factory = DocumentBuilderFactory
180: .newInstance();
181: DocumentBuilder builder;
182: try {
183: builder = factory.newDocumentBuilder();
184: Document document = builder.parse(xmlFile);
185:
186: return document;
187: } catch (Exception pce) {
188: pce.printStackTrace();
189: return null;
190: }
191: }
192:
193: public Document makeDocument() {
194: if (failRequiredInfo())
195: return null;
196: else {
197: //root (dalp) tag
198: Element root = document.createElement("dalp");
199: root.setAttribute("dsdk", "");
200: root.setAttribute("version", DEFAULT_DALP_VERSION_STR);
201: document.appendChild(root);
202:
203: //information root element
204: Element informationTag = document
205: .createElement("information");
206: root.appendChild(informationTag);
207:
208: //information->product-id
209: Element productId = document.createElement("product-id");
210: productId.setTextContent(uid);
211: informationTag.appendChild(productId);
212:
213: //information->title
214: Element title = document.createElement("title");
215: informationTag.appendChild(title);
216: title.setTextContent(appName);
217:
218: //information->vendor
219: Element vendorTag = document.createElement("vendor");
220: informationTag.appendChild(vendorTag);
221: vendorTag.setTextContent(vendor);
222:
223: //information->icon
224: if ((iconPath != null)
225: && (iconPath.trim().equals("") == false)) {
226: Element iconTag = document.createElement("icon");
227: informationTag.appendChild(iconTag);
228: iconTag.setAttribute("href", "./" + this .targetJar
229: + ".jar");
230: iconTag.setAttribute("basepath", DEFAULT_BASEPATH);
231: iconTag.setAttribute("location", DEFAULT_LOCATION);
232: iconTag.setTextContent(iconPath.substring(iconPath
233: .lastIndexOf(this .seperator) + 1));
234: }
235:
236: //information->description
237: Element descriptionTagA = document
238: .createElement("description");
239: Element descriptionTagB = document
240: .createElement("description");
241: informationTag.appendChild(descriptionTagA);
242: informationTag.appendChild(descriptionTagB);
243: descriptionTagA.setTextContent(this .description);
244: descriptionTagB.setAttribute("type", "detail");
245: descriptionTagB.setTextContent(this .detailDescription);
246:
247: //information->telephone, fax, e-mail
248: Element telephoneTag = document.createElement("telephone");
249: Element faxTag = document.createElement("fax");
250: Element emailTag = document.createElement("e-mail");
251: telephoneTag.setTextContent(this .phone);
252: faxTag.setTextContent(this .fax);
253: emailTag.setTextContent(this .email);
254: informationTag.appendChild(telephoneTag);
255: informationTag.appendChild(faxTag);
256: informationTag.appendChild(emailTag);
257:
258: //these are set by default to fixed values, no change supported as of yet
259: //information->application-version, information->offline-allowed, dalp->security element (root child),
260: Element appVersionTag = document
261: .createElement("application-ver");
262: appVersionTag.setTextContent(appVersion);
263: informationTag.appendChild(appVersionTag);
264:
265: Element allowOfflineTag = document
266: .createElement("offline-allowed");
267: informationTag.appendChild(allowOfflineTag);
268:
269: Element securityTag = document.createElement("security");
270: root.appendChild(securityTag);
271: Element allPermissions = document
272: .createElement("all-permissions");
273: securityTag.appendChild(allPermissions);
274:
275: //dalp->resources
276: Element resourcesTag = document.createElement("resources");
277: root.appendChild(resourcesTag);
278:
279: //resources->dsdk
280: Element dsdkTag = document.createElement("dsdk");
281: dsdkTag.setAttribute("version", DSDK_VERSION_STR);
282: resourcesTag.appendChild(dsdkTag);
283:
284: //resources->jar
285: Element jarTag = document.createElement("jar");
286: jarTag.setAttribute("href", "./" + targetJar + ".jar");
287: jarTag.setAttribute("version", this .appVersion);
288: jarTag.setAttribute("basepath", DEFAULT_BASEPATH);
289: resourcesTag.appendChild(jarTag);
290:
291: //resources->encode-file
292: Element encodeFileTag = document
293: .createElement("encode-file");
294: if (targetJar.contains(".jar"))
295: encodeFileTag.setTextContent(targetJar.substring(0,
296: targetJar.indexOf(".jar")).toLowerCase());
297: else
298: encodeFileTag.setTextContent(targetJar.toLowerCase());
299:
300: resourcesTag.appendChild(encodeFileTag);
301:
302: //dalp->application-desc
303: Element appDescTag = document
304: .createElement("application-desc");
305: appDescTag.setAttribute("main-class", this .mainClass);
306: appDescTag.setAttribute("visible", APP_VISIBLE_STR);
307: root.appendChild(appDescTag);
308:
309: //dalp->install
310: Element installTag = document.createElement("install");
311: installTag.setAttribute("mode", INSTALL_MODE);
312: installTag.setAttribute("destination", INSTALL_DEST);
313: root.appendChild(installTag);
314:
315: return document;
316: }
317: }
318:
319: /**
320: * Getter for property vendor.
321: * @return Value of property vendor.
322: */
323: public String getVendor() {
324: return this .vendor;
325: }
326:
327: /**
328: * Setter for property vendor.
329: * @param vendor New value of property vendor.
330: */
331: public void setVendor(String vendor) {
332: this .vendor = vendor;
333: }
334:
335: /**
336: * Getter for property iconPath.
337: * @return Value of property iconPath.
338: */
339: public java.lang.String getIconPath() {
340: return this .iconPath;
341: }
342:
343: /**
344: * Getter for property description.
345: * @return Value of property description.
346: */
347: public String getDescription() {
348: return this .description;
349: }
350:
351: /**
352: * Setter for property description.
353: * @param description New value of property description.
354: */
355: public void setDescription(String description) {
356: this .description = description;
357: }
358:
359: /**
360: * Getter for property detailDescription.
361: * @return Value of property detailDescription.
362: */
363: public String getDetailDescription() {
364: return this .detailDescription;
365: }
366:
367: /**
368: * Setter for property detailDescription.
369: * @param detailDescription New value of property detailDescription.
370: */
371: public void setDetailDescription(String detailDescription) {
372: this.detailDescription = detailDescription;
373: }
374:
375: }
|