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-2006 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: package org.netbeans.modules.xsl.settings;
042:
043: import java.util.*;
044: import java.io.*;
045:
046: import org.openide.filesystems.FileObject;
047:
048: import org.netbeans.modules.xsl.utils.TransformUtil;
049:
050: /**
051: * Transformation history of one XML or XSLT document. Used as FileObject attribute and also as TransformPanel model.
052: *
053: * @author Libor Kramolis
054: * @version 0.1
055: */
056: public final class TransformHistory implements Serializable {
057: /** Serial Version UID */
058: private static final long serialVersionUID = -6268945703343989727L;
059:
060: /** Last selected XMLs with associated Outputs. Can be null. */
061: private ListMap xmlOutputMap; // Map<String,String>
062:
063: /** Last selected XSLs with associated Outputs. Can be null. */
064: private ListMap xslOutputMap; // Map<String,String>
065:
066: /** Automatically overwrite output. */
067: private boolean overwriteOutput;
068:
069: /** What to do with output file: DO_NOTHING | APPLY_DEFAULT_ACTION | OPEN_IN_BROWSER. */
070: private int processOutput;
071:
072: /** Do nothing with output file. */
073: public static final int DO_NOTHING = 0;
074: /** Apply default action on output file. */
075: public static final int APPLY_DEFAULT_ACTION = 1;
076: /** Open output file int browser. */
077: public static final int OPEN_IN_BROWSER = 2;
078:
079: /** FileObject's attribute name. */
080: public static final String TRANSFORM_HISTORY_ATTRIBUTE = "org.netbeans.modules.xsl.settings.TransformHistory"; // NOI18N
081:
082: //
083: // init
084: //
085:
086: /** Creates new TransformHistory.
087: */
088: public TransformHistory() {
089: xmlOutputMap = null;
090: xslOutputMap = null;
091: overwriteOutput = false;
092: processOutput = OPEN_IN_BROWSER;
093: }
094:
095: public String[] getXMLs() {
096: return getXMLOutputMap().getInputs();
097: }
098:
099: public String getLastXML() {
100: return getXMLOutputMap().getLastInput();
101: }
102:
103: public String[] getXSLs() {
104: return getXSLOutputMap().getInputs();
105: }
106:
107: public String getLastXSL() {
108: return getXSLOutputMap().getLastInput();
109: }
110:
111: public String getXMLOutput(String xml) {
112: return getXMLOutputMap().getOutput(xml);
113: }
114:
115: public String getLastXMLOutput() {
116: return getXMLOutput(getLastXML());
117: }
118:
119: public String getXSLOutput(String xsl) {
120: return getXSLOutputMap().getOutput(xsl);
121: }
122:
123: public String getLastXSLOutput() {
124: return getXSLOutput(getLastXSL());
125: }
126:
127: public void addXML(String xml, String output) {
128: getXMLOutputMap().put(xml, output);
129: }
130:
131: public void addXSL(String xsl, String output) {
132: getXSLOutputMap().put(xsl, output);
133: }
134:
135: public boolean isOverwriteOutput() {
136: return overwriteOutput;
137: }
138:
139: public void setOverwriteOutput(boolean overwrite) {
140: overwriteOutput = overwrite;
141: }
142:
143: public int getProcessOutput() {
144: return processOutput;
145: }
146:
147: public void setProcessOutput(int process) {
148: processOutput = process;
149: }
150:
151: private ListMap getXMLOutputMap() {
152: if (xmlOutputMap == null) {
153: xmlOutputMap = new ListMap();
154: }
155: return xmlOutputMap;
156: }
157:
158: private ListMap getXSLOutputMap() {
159: if (xslOutputMap == null) {
160: xslOutputMap = new ListMap();
161: }
162: return xslOutputMap;
163: }
164:
165: public String toString() {
166: StringBuffer sb = new StringBuffer(super .toString());
167: sb.append(" [ xmlOutputMap= ").append(xmlOutputMap);
168: sb.append(", xslOutputMap= ").append(xslOutputMap);
169: sb.append(", overwriteOutput= ").append(overwriteOutput);
170: sb.append(", processOutput= ").append(processOutput).append(
171: " ]");
172: return sb.toString();
173: }
174:
175: public boolean equals(Object obj) {
176: if ((obj instanceof TransformHistory) == false) {
177: return false;
178: }
179: TransformHistory peer = (TransformHistory) obj;
180: if (equals(this .xmlOutputMap, peer.xmlOutputMap) == false) {
181: return false;
182: }
183: if (equals(this .xslOutputMap, peer.xslOutputMap) == false) {
184: return false;
185: }
186: if (this .overwriteOutput != peer.overwriteOutput) {
187: return false;
188: }
189: if (this .processOutput != peer.processOutput) {
190: return false;
191: }
192: return true;
193: }
194:
195: //
196: // utils
197: //
198: static boolean equals(Object obj1, Object obj2) {
199: if (obj1 != null) {
200: return (obj1.equals(obj2));
201: } else {
202: return (obj1 == obj2);
203: }
204: }
205:
206: //
207: // class ListMap
208: //
209: private static class ListMap implements Serializable {
210: /** Serial Version UID */
211: private static final long serialVersionUID = 6341102578706167575L;
212:
213: /** Max length of history. */
214: public static final int MAX = 5;
215:
216: transient private List inputList;
217: transient private Map inputOutputMap;
218: /** Serializable mirror of inputList and inputOutputMap fields. */
219: private Object[] inputOutputArray;
220:
221: public ListMap() {
222: init();
223: }
224:
225: private void init() {
226: inputList = new LinkedList();
227: inputOutputMap = new HashMap();
228:
229: if (inputOutputArray == null) {
230: return;
231: }
232: for (int i = 0; i < inputOutputArray.length; i += 2) {
233: Object input = inputOutputArray[i];
234: Object output = inputOutputArray[i + 1];
235:
236: try { // just hacks to avoid non-String values
237: // check input
238: if (input instanceof FileObject) {
239: input = TransformUtil
240: .getURLName((FileObject) input);
241: } else if ((input != null)
242: && (input instanceof String) == false) {
243: input = input.toString();
244: }
245: // check output
246: if (output instanceof FileObject) {
247: output = TransformUtil
248: .getURLName((FileObject) output);
249: } else if ((output != null)
250: && (output instanceof String) == false) {
251: output = output.toString();
252: }
253:
254: inputList.add(input);
255: inputOutputMap.put(input, output);
256: } catch (IOException exc) { // TransformUtil.getURLName
257: // ignore it
258:
259: //Util.THIS.debug (exc);
260: }
261: }
262: }
263:
264: public void put(String input, String output) {
265: // remove old value
266: Object old = inputOutputMap.remove(input);
267: inputList.remove(input);
268:
269: // add new value at first place
270: inputOutputMap.put(input, output);
271: inputList.add(0, input);
272:
273: // keep just ${MAX} entries
274: if (inputList.size() > MAX) {
275: Object over = inputList.remove(inputList.size() - 1);
276: inputOutputMap.remove(over);
277: }
278: }
279:
280: public String[] getInputs() {
281: return (String[]) inputList.toArray(new String[0]);
282: }
283:
284: public String getLastInput() {
285: if (inputList.isEmpty()) {
286: return null;
287: }
288: return (String) inputList.get(0);
289: }
290:
291: public String getOutput(String input) {
292: return (String) inputOutputMap.get(input);
293: }
294:
295: public String[] getArray() {
296: if (inputList.size() == 0) {
297: return null;
298: }
299: String[] array = new String[2 * inputList.size()];
300: for (int i = 0; i < inputList.size(); i++) {
301: String input = (String) inputList.get(i);
302: array[2 * i] = input;
303: array[(2 * i) + 1] = (String) inputOutputMap.get(input);
304: }
305: return array;
306: }
307:
308: public String toString() {
309: StringBuffer sb = new StringBuffer(super .toString());
310: sb.append(" [ inputList= ").append(inputList);
311: sb.append(", inputOutputMap.keySet= ").append(
312: inputOutputMap.keySet());
313: sb.append(", inputOutputMap.values= ").append(
314: inputOutputMap.values());
315: sb.append(", xmlOutputArray= ").append(
316: inputOutputArray == null ? "null" : Arrays.asList(
317: inputOutputArray).toString());
318: sb.append(" ]");
319: return sb.toString();
320: }
321:
322: public boolean equals(Object obj) {
323: if ((obj instanceof ListMap) == false) {
324: return false;
325: }
326: ListMap peer = (ListMap) obj;
327: if (TransformHistory.equals(this .inputList, peer.inputList) == false) {
328: return false;
329: }
330: if (TransformHistory.equals(this .inputOutputMap,
331: peer.inputOutputMap) == false) {
332: return false;
333: }
334: return true;
335: }
336:
337: private void readObject(ObjectInputStream ois)
338: throws IOException, ClassNotFoundException {
339: ois.defaultReadObject();
340:
341: init();
342: inputOutputArray = null;
343: }
344:
345: private void writeObject(ObjectOutputStream oos)
346: throws IOException {
347: inputOutputArray = getArray();
348:
349: oos.defaultWriteObject();
350:
351: inputOutputArray = null;
352: }
353:
354: } // class ListMap
355:
356: }
|