KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > util > TAXUtil


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.xml.tax.util;
20
21 import java.io.CharConversionException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
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 /**
36  *
37  * @author Libor Kramolis
38  */

39 public final class TAXUtil {
40
41     /**
42      * Try to set new value to the attribute. Method <code>XMLUtil.toAttributeValue</code> is used
43      * to convert value to correct attribute value.
44      *
45      * @see org.openide.xml.XMLUtil#toAttributeValue
46      */

47     public static void setAttributeValue (TreeAttribute attribute, String JavaDoc value) throws TreeException {
48         try {
49             attribute.setValue (XMLUtil.toAttributeValue (value));
50         } catch (CharConversionException JavaDoc exc) {
51             throw new TreeException (exc);
52         }
53     }
54     
55     /**
56      * Try to set new value to the text. Method <code>XMLUtil.toElementContent</code> is used
57      * to convert value to correct element content.
58      *
59      * @see org.openide.xml.XMLUtil#toElementContent
60      */

61     public static void setTextData (TreeText text, String JavaDoc value) throws TreeException {
62         try {
63             text.setData (XMLUtil.toElementContent (value));
64         } catch (CharConversionException JavaDoc exc) {
65             throw new TreeException (exc);
66         }
67     }
68     
69
70
71     /**
72      */

73     public static void notifyWarning (final String JavaDoc message) {
74         SwingUtilities.invokeLater (new Runnable JavaDoc () {
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     /**
84      */

85     public static String JavaDoc printableValue (String JavaDoc value) {
86         if (value == null)
87             return "<null>"; // NOI18N
88

89         int ch;
90         int MAX_LENGTH = 33;
91         int len = Math.min (value.length (), MAX_LENGTH);
92         
93         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (2 * len);
94         for (int i = 0; i < len; i++) {
95             ch = value.charAt (i);
96             if ('\r' == ch) {
97                 sb.append ("\\r"); // NOI18N
98
} else if ('\n' == ch) {
99                 sb.append ("\\n"); // NOI18N
100
} else if ('\t' == ch) {
101                 sb.append ("\\t"); // NOI18N
102
} else if ('\b' == ch) {
103                 sb.append ("\\b"); // NOI18N
104
} else if ('\f' == ch) {
105                 sb.append ("\\f"); // NOI18N
106
} else {
107                 sb.append ((char)ch);
108             }
109         }
110         if (value.length () > len)
111             sb.append ("..."); // NOI18N
112

113         return sb.toString ();
114     }
115
116     /**
117      */

118     public static void notifyTreeException (TreeException exc) {
119         notifyWarning (exc.getMessage());
120     }
121
122     /*
123      *
124      */

125     public static String JavaDoc treeToString(TreeDocumentRoot doc) throws IOException JavaDoc {
126
127         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc("Cannot read tree " + ex.getMessage()); // NOI18N
136

137         } finally {
138             try {
139                 out.close();
140             } catch (IOException JavaDoc ioex) {
141                 // do not know
142
}
143         }
144
145     }
146
147     public static byte[] treeToByteArray(TreeDocumentRoot doc) throws IOException JavaDoc {
148
149         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(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 JavaDoc("Cannot read tree " + ex.getMessage()); // NOI18N
159

160         } finally {
161             try {
162                 out.close();
163             } catch (IOException JavaDoc ioex) {
164                 // do not know
165
}
166         }
167     }
168
169 }
170
Popular Tags