KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > attr > DataAttributes


1 /*
2  * $Id: DataAttributes.java,v 1.4 2005/01/27 14:40:30 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.attr;
9
10 import org.jdesktop.swing.data.DefaultTableModelExt;
11 import org.jdesktop.swing.data.TableModelExtTextLoader;
12
13 import org.jdesktop.swing.data.DOMAdapter;
14
15 import org.jdesktop.jdnc.markup.Attributes;
16 import org.jdesktop.jdnc.markup.Namespace;
17
18 import java.io.IOException JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23
24 import net.openmarkup.ApplierError;
25 import net.openmarkup.ApplierException;
26 import net.openmarkup.AttributeApplier;
27 import net.openmarkup.Realizable;
28
29 import org.w3c.dom.Document JavaDoc;
30
31 import org.jdesktop.jdnc.markup.RealizationUtils;
32
33 /**
34  * @author Amy Fowler
35  * @author Ramesh Gupta
36  */

37 public class DataAttributes {
38     // ...
39

40     public static void actuateData(DefaultTableModelExt data, String JavaDoc actuateValue) throws IOException JavaDoc {
41         if (actuateValue.equals("") || actuateValue.equals("onLoad")) {
42         data.startLoading();
43         }
44         // "other", "none", "onRequest" values handled elsewhere
45
}
46
47     public static final AttributeApplier actuateApplier = new AttributeApplier() {
48         public void apply(Realizable target, String JavaDoc namespaceURI,
49                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
50             ApplierException {
51             //REMIND(aim): TabularData is only one type of data object - also need to support
52
// RowSet and possible other data object types
53
String JavaDoc mediaType = target.getAttributeNSOptional(Namespace.JDNC,
54                 Attributes.MEDIA_TYPE);
55             try {
56                 if (mediaType.equals("text/jdnc-hierarchical-data")) {
57                     //REMIND(aim):data already loaded?
58
}
59                 else {
60                     DefaultTableModelExt data = (DefaultTableModelExt) target.getObject();
61                     actuateData(data, attributeValue);
62                 }
63             }
64             catch (Exception JavaDoc ex) {
65                 throw new ApplierException("Couldn't actuate data " +
66                                            attributeName + "=" + attributeValue,
67                                            ex);
68             }
69         }
70     };
71
72     public static final AttributeApplier columnDelimiterApplier = new AttributeApplier() {
73         public void apply(Realizable target, String JavaDoc namespaceURI,
74                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
75             DefaultTableModelExt data = (DefaultTableModelExt) target.getObject();
76             /** @todo Fix this! Can't assume TabularDataTextReader */
77             ((TableModelExtTextLoader) data.getLoader()).setColumnDelimiter(
78                 attributeValue);
79         }
80     };
81
82     public static final AttributeApplier firstRowHeaderApplier = new AttributeApplier() {
83         public void apply(Realizable target, String JavaDoc namespaceURI,
84                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
85             DefaultTableModelExt data = (DefaultTableModelExt) target.getObject();
86             /** @todo Fix this! Can't assume TabularDataTextReader */
87             try {
88                 ((TableModelExtTextLoader)data.getLoader()).setFirstRowHeader(
89                     Boolean.valueOf(attributeValue).booleanValue());
90             } catch (Exception JavaDoc e) {
91                 throw new ApplierException(attributeName + "not supported for loader", e);
92             }
93         }
94     };
95
96     public static final AttributeApplier sourceApplier = new AttributeApplier() {
97         public void apply(Realizable target, String JavaDoc namespaceURI,
98                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
99         // Validate the URL before we do anything else.
100
URL JavaDoc url = target.getResolvedURL(attributeValue);
101         RealizationUtils.validateURL(url);
102
103             //REMIND(aim): TabularData is only one type of data object - also need to support
104
// RowSet and possible other data object types
105
String JavaDoc mediaType = target.getAttributeNSOptional(Namespace.JDNC,
106                                                         Attributes.MEDIA_TYPE);
107             try {
108                 if (mediaType.equals("text/jdnc-hierarchical-data")) {
109                     DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
110                     dbf.setNamespaceAware(true);
111                     dbf.setIgnoringComments(true);
112                     Document JavaDoc dom = dbf.newDocumentBuilder().parse(
113                         url.toExternalForm());
114                     ((DOMAdapter) target.getObject()).bind(dom);
115                 }
116                 else {
117                     DefaultTableModelExt data = (DefaultTableModelExt) target.getObject();
118             data.setSource(url);
119                 }
120             }
121             catch (Exception JavaDoc ex) {
122                 throw new ApplierError("Couldn't set data source " +
123                     attributeName + "=" + attributeValue, ex);
124             }
125         }
126     };
127
128 }
129
Popular Tags