1 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 35 public final class TransformHistory implements Serializable { 36 37 private static final long serialVersionUID = -6268945703343989727L; 38 39 40 private ListMap xmlOutputMap; 42 43 private ListMap xslOutputMap; 45 46 private boolean overwriteOutput; 47 48 49 private int processOutput; 50 51 52 public static final int DO_NOTHING = 0; 53 54 public static final int APPLY_DEFAULT_ACTION = 1; 55 56 public static final int OPEN_IN_BROWSER = 2; 57 58 59 public static final String TRANSFORM_HISTORY_ATTRIBUTE = 60 "org.netbeans.modules.xsl.settings.TransformHistory"; 62 66 68 public TransformHistory () { 69 xmlOutputMap = null; 70 xslOutputMap = null; 71 overwriteOutput = false; 72 processOutput = OPEN_IN_BROWSER; 73 } 74 75 76 public String [] getXMLs () { 77 return getXMLOutputMap().getInputs(); 78 } 79 80 public String getLastXML () { 81 return getXMLOutputMap().getLastInput(); 82 } 83 84 public String [] getXSLs () { 85 return getXSLOutputMap().getInputs(); 86 } 87 88 public String getLastXSL () { 89 return getXSLOutputMap().getLastInput(); 90 } 91 92 public String getXMLOutput (String xml) { 93 return getXMLOutputMap().getOutput (xml); 94 } 95 96 public String getLastXMLOutput () { 97 return getXMLOutput (getLastXML()); 98 } 99 100 public String getXSLOutput (String xsl) { 101 return getXSLOutputMap().getOutput (xsl); 102 } 103 104 public String getLastXSLOutput () { 105 return getXSLOutput (getLastXSL()); 106 } 107 108 public void addXML (String xml, String output) { 109 getXMLOutputMap().put (xml, output); 110 } 111 112 public void addXSL (String xsl, String 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 toString () { 148 StringBuffer sb = new StringBuffer (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 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 static boolean equals (Object obj1, Object obj2) { 181 if ( obj1 != null ) { 182 return (obj1.equals (obj2)); 183 } else { 184 return (obj1 == obj2); 185 } 186 } 187 188 189 private static class ListMap implements Serializable { 193 194 private static final long serialVersionUID = 6341102578706167575L; 195 196 197 public static final int MAX = 5; 198 199 transient private List inputList; 200 transient private Map inputOutputMap; 201 202 private Object [] 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 input = inputOutputArray[i]; 219 Object output = inputOutputArray[i+1]; 220 221 try { if ( input instanceof FileObject ) { 224 input = TransformUtil.getURLName ((FileObject) input); 225 } else if ( ( input != null ) && 226 ( input instanceof String ) == false ) { 227 input = input.toString(); 228 } 229 if ( output instanceof FileObject ) { 231 output = TransformUtil.getURLName ((FileObject) output); 232 } else if ( ( output != null ) && 233 ( output instanceof String ) == false ) { 234 output = output.toString(); 235 } 236 237 inputList.add (input); 238 inputOutputMap.put (input, output); 239 } catch (IOException exc) { 242 Util.THIS.debug (exc); 243 } 244 } 245 } 246 247 public void put (String input, String output) { 248 Object old = inputOutputMap.remove (input); 250 inputList.remove (input); 251 252 inputOutputMap.put (input, output); 254 inputList.add (0, input); 255 256 if ( inputList.size() > MAX ) { 258 Object over = inputList.remove (inputList.size() - 1); 259 inputOutputMap.remove (over); 260 } 261 } 262 263 public String [] getInputs () { 264 return (String []) inputList.toArray (new String [0]); 265 } 266 267 public String getLastInput () { 268 if ( inputList.isEmpty() ) { 269 return null; 270 } 271 return (String ) inputList.get (0); 272 } 273 274 public String getOutput (String input) { 275 return (String ) inputOutputMap.get (input); 276 } 277 278 public String [] getArray () { 279 if ( inputList.size() == 0 ) { 280 return null; 281 } 282 String [] array = new String [2 * inputList.size()]; 283 for ( int i = 0; i < inputList.size(); i++ ) { 284 String input = (String ) inputList.get (i); 285 array[2*i] = input; 286 array[(2*i)+1] = (String ) inputOutputMap.get (input); 287 } 288 return array; 289 } 290 291 public String toString () { 292 StringBuffer sb = new StringBuffer (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 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 { 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 } 333 } 334 | Popular Tags |