1 19 package org.netbeans.modules.xml.tax.util; 20 21 import java.io.CharConversionException ; 22 import java.io.IOException ; 23 import java.io.StringWriter ; 24 import java.io.ByteArrayOutputStream ; 25 import javax.swing.SwingUtilities ; 26 27 import org.openide.xml.XMLUtil; 28 import org.openide.NotifyDescriptor; 29 import org.openide.DialogDisplayer; 30 31 import org.netbeans.tax.*; 32 import org.netbeans.tax.io.TreeStreamResult; 33 import org.netbeans.tax.io.TreeWriter; 34 35 39 public final class TAXUtil { 40 41 47 public static void setAttributeValue (TreeAttribute attribute, String value) throws TreeException { 48 try { 49 attribute.setValue (XMLUtil.toAttributeValue (value)); 50 } catch (CharConversionException exc) { 51 throw new TreeException (exc); 52 } 53 } 54 55 61 public static void setTextData (TreeText text, String value) throws TreeException { 62 try { 63 text.setData (XMLUtil.toElementContent (value)); 64 } catch (CharConversionException exc) { 65 throw new TreeException (exc); 66 } 67 } 68 69 70 71 73 public static void notifyWarning (final String message) { 74 SwingUtilities.invokeLater (new Runnable () { 75 public void run () { 76 NotifyDescriptor nd = new NotifyDescriptor.Message 77 (message, NotifyDescriptor.WARNING_MESSAGE); 78 DialogDisplayer.getDefault ().notify (nd); 79 } 80 }); 81 } 82 83 85 public static String printableValue (String value) { 86 if (value == null) 87 return "<null>"; 89 int ch; 90 int MAX_LENGTH = 33; 91 int len = Math.min (value.length (), MAX_LENGTH); 92 93 StringBuffer sb = new StringBuffer (2 * len); 94 for (int i = 0; i < len; i++) { 95 ch = value.charAt (i); 96 if ('\r' == ch) { 97 sb.append ("\\r"); } else if ('\n' == ch) { 99 sb.append ("\\n"); } else if ('\t' == ch) { 101 sb.append ("\\t"); } else if ('\b' == ch) { 103 sb.append ("\\b"); } else if ('\f' == ch) { 105 sb.append ("\\f"); } else { 107 sb.append ((char)ch); 108 } 109 } 110 if (value.length () > len) 111 sb.append ("..."); 113 return sb.toString (); 114 } 115 116 118 public static void notifyTreeException (TreeException exc) { 119 notifyWarning (exc.getMessage()); 120 } 121 122 125 public static String treeToString(TreeDocumentRoot doc) throws IOException { 126 127 StringWriter out = new StringWriter (); 128 TreeStreamResult result = new TreeStreamResult(out); 129 TreeWriter writer = result.getWriter(doc); 130 131 try { 132 writer.writeDocument(); 133 return out.toString(); 134 } catch (TreeException ex) { 135 throw new IOException ("Cannot read tree " + ex.getMessage()); 137 } finally { 138 try { 139 out.close(); 140 } catch (IOException ioex) { 141 } 143 } 144 145 } 146 147 public static byte[] treeToByteArray(TreeDocumentRoot doc) throws IOException { 148 149 ByteArrayOutputStream out = new ByteArrayOutputStream (1024 * 8); 150 TreeStreamResult result = new TreeStreamResult(out); 151 TreeWriter writer = result.getWriter(doc); 152 153 try { 154 writer.writeDocument(); 155 byte[] array = out.toByteArray(); 156 return array; 157 } catch (TreeException ex) { 158 throw new IOException ("Cannot read tree " + ex.getMessage()); 160 } finally { 161 try { 162 out.close(); 163 } catch (IOException ioex) { 164 } 166 } 167 } 168 169 } 170 | Popular Tags |