KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > settings > TransformHistory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xsl.settings;
20
21 import java.util.*;
22 import java.io.*;
23
24 import org.openide.filesystems.FileObject;
25
26 import org.netbeans.modules.xsl.utils.TransformUtil;
27
28
29 /**
30  * Transformation history of one XML or XSLT document. Used as FileObject attribute and also as TransformPanel model.
31  *
32  * @author Libor Kramolis
33  * @version 0.1
34  */

35 public final class TransformHistory implements Serializable {
36     /** Serial Version UID */
37     private static final long serialVersionUID = -6268945703343989727L;
38
39     /** Last selected XMLs with associated Outputs. Can be null. */
40     private ListMap xmlOutputMap; // Map<String,String>
41

42     /** Last selected XSLs with associated Outputs. Can be null. */
43     private ListMap xslOutputMap; // Map<String,String>
44

45     /** Automatically overwrite output. */
46     private boolean overwriteOutput;
47
48     /** What to do with output file: DO_NOTHING | APPLY_DEFAULT_ACTION | OPEN_IN_BROWSER. */
49     private int processOutput;
50
51     /** Do nothing with output file. */
52     public static final int DO_NOTHING = 0;
53     /** Apply default action on output file. */
54     public static final int APPLY_DEFAULT_ACTION = 1;
55     /** Open output file int browser. */
56     public static final int OPEN_IN_BROWSER = 2;
57
58     /** FileObject's attribute name. */
59     public static final String JavaDoc TRANSFORM_HISTORY_ATTRIBUTE =
60         "org.netbeans.modules.xsl.settings.TransformHistory"; // NOI18N
61

62     //
63
// init
64
//
65

66     /** Creates new TransformHistory.
67      */

68     public TransformHistory () {
69         xmlOutputMap = null;
70         xslOutputMap = null;
71         overwriteOutput = false;
72         processOutput = OPEN_IN_BROWSER;
73     }
74
75
76     public String JavaDoc[] getXMLs () {
77         return getXMLOutputMap().getInputs();
78     }
79
80     public String JavaDoc getLastXML () {
81         return getXMLOutputMap().getLastInput();
82     }
83
84     public String JavaDoc[] getXSLs () {
85         return getXSLOutputMap().getInputs();
86     }
87
88     public String JavaDoc getLastXSL () {
89         return getXSLOutputMap().getLastInput();
90     }
91
92     public String JavaDoc getXMLOutput (String JavaDoc xml) {
93         return getXMLOutputMap().getOutput (xml);
94     }
95
96     public String JavaDoc getLastXMLOutput () {
97         return getXMLOutput (getLastXML());
98     }
99
100     public String JavaDoc getXSLOutput (String JavaDoc xsl) {
101         return getXSLOutputMap().getOutput (xsl);
102     }
103
104     public String JavaDoc getLastXSLOutput () {
105         return getXSLOutput (getLastXSL());
106     }
107
108     public void addXML (String JavaDoc xml, String JavaDoc output) {
109         getXMLOutputMap().put (xml, output);
110     }
111
112     public void addXSL (String JavaDoc xsl, String JavaDoc output) {
113         getXSLOutputMap().put (xsl, output);
114     }
115
116     public boolean isOverwriteOutput () {
117         return overwriteOutput;
118     }
119
120     public void setOverwriteOutput (boolean overwrite) {
121         overwriteOutput = overwrite;
122     }
123
124     public int getProcessOutput () {
125         return processOutput;
126     }
127
128     public void setProcessOutput (int process) {
129         processOutput = process;
130     }
131
132
133     private ListMap getXMLOutputMap () {
134         if ( xmlOutputMap == null ) {
135             xmlOutputMap = new ListMap();
136         }
137         return xmlOutputMap;
138     }
139
140     private ListMap getXSLOutputMap () {
141         if ( xslOutputMap == null ) {
142             xslOutputMap = new ListMap();
143         }
144         return xslOutputMap;
145     }
146
147     public String JavaDoc toString () {
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (super.toString());
149         sb.append (" [ xmlOutputMap= ").append (xmlOutputMap);
150         sb.append (", xslOutputMap= ").append (xslOutputMap);
151         sb.append (", overwriteOutput= ").append (overwriteOutput);
152         sb.append (", processOutput= ").append (processOutput).append (" ]");
153         return sb.toString();
154     }
155     
156     public boolean equals (Object JavaDoc obj) {
157         if ( ( obj instanceof TransformHistory ) == false ) {
158             return false;
159         }
160         TransformHistory peer = (TransformHistory)obj;
161         if ( equals (this.xmlOutputMap, peer.xmlOutputMap) == false ) {
162             return false;
163         }
164         if ( equals (this.xslOutputMap, peer.xslOutputMap) == false ) {
165             return false;
166         }
167         if ( this.overwriteOutput != peer.overwriteOutput ) {
168             return false;
169         }
170         if ( this.processOutput != peer.processOutput ) {
171             return false;
172         }
173         return true;
174     }
175     
176     
177     //
178
// utils
179
//
180
static boolean equals (Object JavaDoc obj1, Object JavaDoc obj2) {
181         if ( obj1 != null ) {
182             return (obj1.equals (obj2));
183         } else {
184             return (obj1 == obj2);
185         }
186     }
187
188     
189     //
190
// class ListMap
191
//
192
private static class ListMap implements Serializable {
193         /** Serial Version UID */
194         private static final long serialVersionUID = 6341102578706167575L;
195
196         /** Max length of history. */
197         public static final int MAX = 5;
198         
199         transient private List inputList;
200         transient private Map inputOutputMap;
201         /** Serializable mirror of inputList and inputOutputMap fields. */
202         private Object JavaDoc[] inputOutputArray;
203
204
205
206         public ListMap () {
207             init();
208         }
209
210         private void init () {
211             inputList = new LinkedList();
212             inputOutputMap = new HashMap();
213
214             if ( inputOutputArray == null ) {
215                 return;
216             }
217             for ( int i = 0; i < inputOutputArray.length; i+=2 ) {
218                 Object JavaDoc input = inputOutputArray[i];
219                 Object JavaDoc output = inputOutputArray[i+1];
220
221                 try { // just hacks to avoid non-String values
222
// check input
223
if ( input instanceof FileObject ) {
224                         input = TransformUtil.getURLName ((FileObject) input);
225                     } else if ( ( input != null ) &&
226                                 ( input instanceof String JavaDoc ) == false ) {
227                         input = input.toString();
228                     }
229                     // check output
230
if ( output instanceof FileObject ) {
231                         output = TransformUtil.getURLName ((FileObject) output);
232                     } else if ( ( output != null ) &&
233                                 ( output instanceof String JavaDoc ) == false ) {
234                         output = output.toString();
235                     }
236
237                     inputList.add (input);
238                     inputOutputMap.put (input, output);
239                 } catch (IOException exc) { // TransformUtil.getURLName
240
// ignore it
241

242                     Util.THIS.debug (exc);
243                 }
244             }
245         }
246
247         public void put (String JavaDoc input, String JavaDoc output) {
248             // remove old value
249
Object JavaDoc old = inputOutputMap.remove (input);
250             inputList.remove (input);
251
252             // add new value at first place
253
inputOutputMap.put (input, output);
254             inputList.add (0, input);
255             
256             // keep just ${MAX} entries
257
if ( inputList.size() > MAX ) {
258                 Object JavaDoc over = inputList.remove (inputList.size() - 1);
259                 inputOutputMap.remove (over);
260             }
261         }
262
263         public String JavaDoc[] getInputs () {
264             return (String JavaDoc[]) inputList.toArray (new String JavaDoc[0]);
265         }
266         
267         public String JavaDoc getLastInput () {
268             if ( inputList.isEmpty() ) {
269                 return null;
270             }
271             return (String JavaDoc) inputList.get (0);
272         }
273
274         public String JavaDoc getOutput (String JavaDoc input) {
275             return (String JavaDoc) inputOutputMap.get (input);
276         }
277
278         public String JavaDoc[] getArray () {
279             if ( inputList.size() == 0 ) {
280                 return null;
281             }
282             String JavaDoc[] array = new String JavaDoc [2 * inputList.size()];
283             for ( int i = 0; i < inputList.size(); i++ ) {
284                 String JavaDoc input = (String JavaDoc) inputList.get (i);
285                 array[2*i] = input;
286                 array[(2*i)+1] = (String JavaDoc) inputOutputMap.get (input);
287             }
288             return array;
289         }
290
291         public String JavaDoc toString () {
292             StringBuffer JavaDoc sb = new StringBuffer JavaDoc (super.toString());
293             sb.append (" [ inputList= ").append (inputList);
294             sb.append (", inputOutputMap.keySet= ").append (inputOutputMap.keySet());
295             sb.append (", inputOutputMap.values= ").append (inputOutputMap.values());
296             sb.append (", xmlOutputArray= ").append (inputOutputArray == null ? "null" : Arrays.asList (inputOutputArray).toString());
297             sb.append (" ]");
298             return sb.toString();
299         }
300
301         public boolean equals (Object JavaDoc obj) {
302             if ( ( obj instanceof ListMap ) == false ) {
303                 return false;
304             }
305             ListMap peer = (ListMap)obj;
306             if ( TransformHistory.equals (this.inputList, peer.inputList) == false ) {
307                 return false;
308             }
309             if ( TransformHistory.equals (this.inputOutputMap, peer.inputOutputMap) == false ) {
310                 return false;
311             }
312             return true;
313         }
314     
315         private void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException JavaDoc {
316             ois.defaultReadObject();
317
318             init();
319             inputOutputArray = null;
320         }
321         
322
323         private void writeObject (ObjectOutputStream oos) throws IOException {
324             inputOutputArray = getArray();
325             
326             oos.defaultWriteObject();
327             
328             inputOutputArray = null;
329         }
330         
331     } // class ListMap
332

333 }
334
Popular Tags