1 package com.ca.directory.jxplorer.viewer; 2 3 4 import javax.swing.text.*; 5 import javax.swing.text.html.*; 6 import javax.naming.directory.*; 7 import java.io.UnsupportedEncodingException ; 8 import java.util.Enumeration ; 9 import java.util.HashSet ; 10 import java.util.logging.Logger ; 11 import java.util.logging.Level ; 12 import java.net.URLDecoder ; 13 import com.ca.commons.naming.*; 14 import com.ca.commons.cbutil.*; 15 16 21 class MyFormView extends FormView 22 { 23 27 public static final String SUBMIT = "Submit"; 28 29 HTMLTemplateDisplay templateDisplay; 30 31 private static Logger log = Logger.getLogger(MyHTMLEditorKit.class.getName()); 32 33 36 public MyFormView(Element elem, HTMLTemplateDisplay display) 37 { 38 super(elem); 39 40 templateDisplay = display; 41 } 42 43 49 50 protected void submitData(String data) 51 { 52 DXEntry oldEntry = templateDisplay.currentEntry; 54 log.finest("Entry before changes: " + oldEntry); 55 56 DN dn = oldEntry.getDN(); RDN rdn = new RDN(oldEntry.getRDN()); 59 String [] namingTypes = rdn.getAtts(); String [] namingRawValues = rdn.getRawVals(); boolean nameChanged = false; 62 63 DXEntry newEntry = parseData(data, oldEntry); 65 newEntry.remove(SUBMIT); 67 68 log.finest("Entry after changes: " + newEntry); 69 70 if (newEntry == null) return; 72 try 73 { 74 String newName; for(int i=0;i<namingTypes.length;i++) { 77 Attribute namingAtt = newEntry.get(namingTypes[i]); 79 80 if(namingAtt != null) 81 { 82 newName = (String )namingAtt.get(); 84 if (newName!=null) { 86 if (newName.trim().equalsIgnoreCase(newName) == false) 90 { 91 newName = newName.trim(); 92 if (newName.length() == 0) newName = " "; 93 namingAtt.remove(0); 94 namingAtt.add(0, newName); 95 } 96 97 if (newName.equals(namingRawValues) == false) 98 { 99 nameChanged = true; 100 rdn.setRawVal(newName, i); 101 } 102 } 103 } 104 } 105 } 106 catch (Exception e) { 108 log.log(Level.WARNING, "error parsing entry name in HTMLEditorKit ", e); 109 } 110 111 if (nameChanged) 112 { 113 DN newDN = new DN(dn); newDN.setRDN(rdn, newDN.size()-1); newEntry.setDN(newDN); } 117 else 118 { 119 newEntry.setDN(dn); } 121 122 Enumeration allOldAtts = oldEntry.getAll(); 123 124 128 while (allOldAtts.hasMoreElements()) 129 { 130 Attribute att = (Attribute)allOldAtts.nextElement(); 131 if (att.size() != 0) 132 if (newEntry.get(att.getID()) == null) 133 { 134 newEntry.put(att); 135 } 136 } 137 138 140 141 templateDisplay.currentDataSource.modifyEntry(oldEntry, newEntry); 142 } 143 144 152 153 public DXEntry parseData(String data, DXEntry oldEntry) 154 { 155 DXEntry newEntry = new DXEntry(); 156 157 HashSet forbiddenAttributes = null; 159 int start=0; 160 int equalpos=0; 161 int end=0; 162 int length = data.length(); 163 164 try 165 { 166 while (start < length) 167 { 168 end = data.indexOf('&', start); 169 if (end == -1) end = length; 170 171 equalpos = data.indexOf('=', start); 172 173 String attribute = data.substring(start, equalpos); 174 175 String value = URLDecoder.decode(data.substring(equalpos+1, end)); Object val = value; 179 180 String stringVal = null; 181 182 if (templateDisplay.currentBinaryAttributes.contains(attribute.toLowerCase())) 184 { 185 Object original = null; 186 String oldVal = null; 187 Attribute binAtt = templateDisplay.currentEntry.get(attribute); 188 if (binAtt != null && binAtt.size()>0) 189 { 190 original = binAtt.get(); 191 oldVal = CBBase64.binaryToString((byte[])original); 192 } 193 194 if (value.equals(oldVal)) 195 { 196 val = original; } 198 else 199 { 200 if (attribute.toLowerCase().indexOf("password") > -1) 202 { 203 try 204 { 205 val = value.getBytes("UTF-8"); 206 } 207 catch (UnsupportedEncodingException e2) 208 { 209 CBUtility.error(templateDisplay, CBIntText.get("Unable to UTF-8 encode password"), e2); 210 } 211 } 212 else 213 { 214 val = CBBase64.stringToBinary(value); } 216 } 217 } 218 else if (attribute.toLowerCase().indexOf("address") > -1) { 220 val = value.replace('\n','$'); 221 } 222 223 if (val instanceof String ) 224 stringVal = (String )val; 225 226 int dataLength = (stringVal != null)?stringVal.length():((byte[])val).length; 228 235 236 if (stringVal != null && stringVal.equals(HTMLTemplateDisplay.ILLEGAL_VALUE)) 237 { 238 if (forbiddenAttributes == null) 239 { 240 forbiddenAttributes = new HashSet (); 241 } 242 Attribute att = oldEntry.get(attribute); 243 newEntry.put(att); 244 forbiddenAttributes.add(att); } 247 else 248 { 249 250 if (newEntry.get(attribute) != null) { 252 if (dataLength != 0) { 254 Attribute att = newEntry.get(attribute); 255 256 if (forbiddenAttributes == null || forbiddenAttributes.contains(att) == false) 259 att.add(val); 260 } 261 } 262 else 263 { 264 267 if (dataLength != 0) 268 newEntry.put(new DXAttribute(attribute,val)); else 270 newEntry.put(new DXAttribute(attribute)); } 272 } 273 274 start = end + 1; 275 } 276 } 277 catch (Exception e) 278 { 279 CBUtility.error(templateDisplay, "Unable to submit form due\nto a problem with the query url.", new Exception ("Parser error in MyHTMLEditorKit - badly formed query url: " + data + "\n " + e.toString())); 280 return null; } 282 283 return newEntry; 284 } 285 } 286 287 288 public class MyHTMLEditorKit extends HTMLEditorKit 289 { 290 private final ViewFactory newDefaultFactory; 291 292 public MyHTMLEditorKit(HTMLTemplateDisplay display) 293 { 294 super(); 295 newDefaultFactory = new MyHTMLFactory(display); 296 } 297 298 public ViewFactory getViewFactory() 299 { 300 return newDefaultFactory; 301 } 302 303 public static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory 304 { 305 private final HTMLTemplateDisplay templateDisplay; 306 307 public MyHTMLFactory(HTMLTemplateDisplay display) 308 { 309 super(); 310 templateDisplay = display; 311 } 312 313 316 public View create(Element elem) 317 { 318 View v = super.create(elem); 319 if (v instanceof FormView) 320 v = new MyFormView(elem, templateDisplay); 321 return v; 322 } 323 } 324 325 } | Popular Tags |