KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > report > html > HTMLAttribute


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.diagnostics.report.html;
24
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29
30
31 /**
32  * Provide a basic implementation of an attribute.
33  */

34 public class HTMLAttribute implements Attribute, Text {
35     
36     /**
37      * The name of this attribute.
38      */

39     private String JavaDoc name = null;
40     
41     /**
42      * The value of this attribute.
43      */

44     private String JavaDoc value = null;
45
46     private static final String JavaDoc DOUBLE_QUOTES ="\"";
47     
48     private static final char EQUALS = '=';
49     /**
50      * Create a new attribute.
51      * @param name The name of the attribute.
52      * @param value The value of the attribute.
53      */

54     public HTMLAttribute(String JavaDoc name, String JavaDoc value) {
55         setName(name);
56         setValue(value);
57     }
58     
59     /**
60      *
61      */

62     public String JavaDoc getName() {
63         return name;
64     }
65
66     /**
67      *
68      */

69     public void setName(String JavaDoc name) {
70         if (name == null) {
71             throw new NullPointerException JavaDoc("Attribute name is null");
72         }
73         this.name = name;
74     }
75
76
77     /**
78      *
79      */

80     public String JavaDoc getValue() {
81         return value;
82     }
83
84
85     /**
86      *
87      */

88     public void setValue(String JavaDoc value) {
89         if (value == null) {
90             throw new NullPointerException JavaDoc("Attribute value is null.");
91         }
92         this.value = value;
93     }
94     
95
96     /**
97      * @see java.lang.Object#toString()
98      */

99     public String JavaDoc toString() {
100         String JavaDoc nameStr = Escape.getInstance().encodeEntities(name, " \t\r\n");
101         String JavaDoc valueStr = Escape.getInstance().encodeEntities(value, "");
102         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103         buf.append(nameStr)
104             .append(EQUALS+DOUBLE_QUOTES)
105             .append(valueStr)
106             .append(DOUBLE_QUOTES);
107         return buf.toString();
108     }
109
110
111     /**
112      */

113     public void write(Writer JavaDoc output) throws IOException JavaDoc {
114         output.append(toString());
115         output.flush();
116     }
117
118
119     /**
120      */

121     public void write(File JavaDoc file) throws IOException JavaDoc {
122         FileWriter JavaDoc fw = new FileWriter JavaDoc(file);
123         write(fw);
124         fw.close();
125     }
126
127    
128 }
129
Popular Tags