KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > content > ContentParser


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
20 package org.netbeans.modules.welcome.content;
21
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.Repository;
34 import org.openide.util.Lookup;
35 import org.openide.xml.XMLUtil;
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42
43 /**
44  * XML parser for Start Page content layout files.
45  *
46  * @author S. Aubrecht
47  */

48 class ContentParser extends DefaultHandler JavaDoc {
49     
50     private static final String JavaDoc ELEM_ROOT = "welcomepage"; // NOI18N
51
private static final String JavaDoc ELEM_BACKGROUND = "background"; // NOI18N
52
private static final String JavaDoc ELEM_PANEL = "panel"; // NOI18N
53
private static final String JavaDoc ELEM_CONSTRAINTS = "constraints"; // NOI18N
54

55     private static final String JavaDoc CURRENT_VERSION = "1.0"; // NOI18N
56

57     private Map JavaDoc<String JavaDoc, Integer JavaDoc> anchors = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>( 10 );
58     private Map JavaDoc<String JavaDoc, Integer JavaDoc> fills = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>( 4 );
59
60     private Map JavaDoc<String JavaDoc,ComponentDescriptor> descriptors = new HashMap JavaDoc<String JavaDoc,ComponentDescriptor>( 5 );
61     private ArrayList JavaDoc<String JavaDoc> contentIds = new ArrayList JavaDoc<String JavaDoc>( 5 );
62     private String JavaDoc backgroundPainterClassName;
63     
64     private ComponentDescriptor currentDescriptor;
65     
66     private static final String JavaDoc CONTENT_ROOT_FOLDER = "WelcomePage"; // NOI18N
67

68     public ContentParser() {
69         anchors.put( "CENTER", Integer.valueOf( GridBagConstraints.CENTER ) ); // NOI18N
70
anchors.put( "WEST", Integer.valueOf( GridBagConstraints.WEST ) ); // NOI18N
71
anchors.put( "NORTH", Integer.valueOf( GridBagConstraints.NORTH ) ); // NOI18N
72
anchors.put( "NORTHEAST", Integer.valueOf( GridBagConstraints.NORTHEAST ) ); // NOI18N
73
anchors.put( "NORTHEAST", Integer.valueOf( GridBagConstraints.NORTHEAST ) ); // NOI18N
74
anchors.put( "EAST", Integer.valueOf( GridBagConstraints.EAST ) ); // NOI18N
75
anchors.put( "SOUTH", Integer.valueOf( GridBagConstraints.SOUTH ) ); // NOI18N
76
anchors.put( "SOUTHEAST", Integer.valueOf( GridBagConstraints.SOUTHEAST ) ); // NOI18N
77
anchors.put( "SOUTHWEST", Integer.valueOf( GridBagConstraints.SOUTHWEST ) ); // NOI18N
78

79         fills.put( "NONE", Integer.valueOf( GridBagConstraints.NONE ) ); // NOI18N
80
fills.put( "HORIZONTAL", Integer.valueOf( GridBagConstraints.HORIZONTAL ) ); // NOI18N
81
fills.put( "VERTICAL", Integer.valueOf( GridBagConstraints.VERTICAL ) ); // NOI18N
82
fills.put( "BOTH", Integer.valueOf( GridBagConstraints.BOTH ) ); // NOI18N
83
}
84     
85     public void parse() throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
86         FileObject root = Repository.getDefault().getDefaultFileSystem().findResource( CONTENT_ROOT_FOLDER );
87         if( null != root ) {
88             FileObject[] children = root.getChildren();
89             for( int i=0; i<children.length; i++ ) {
90                 FileObject contentFile = children[i];
91                 if( contentFile.canRead() && contentFile.isData() ) {
92                     parse( contentFile.getInputStream() );
93                 }
94             }
95         }
96     }
97
98     public void parseLocal() throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
99         String JavaDoc layoutFilePath = BundleSupport.getURL( "Layout" ); // NOI18N
100
URL JavaDoc url = new URL JavaDoc(layoutFilePath);
101         parse( url.openStream() );
102     }
103     
104     private void parse( InputStream JavaDoc input ) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
105         XMLReader JavaDoc reader = XMLUtil.createXMLReader(false, false);
106         reader.setContentHandler(this);
107         reader.parse(new InputSource JavaDoc(input));
108     }
109     
110     public ComponentDescriptor[] getContents() {
111         ComponentDescriptor[] res = new ComponentDescriptor[ contentIds.size() ];
112         int index = 0;
113         for( Iterator JavaDoc<String JavaDoc> i=contentIds.iterator(); i.hasNext(); index++ ) {
114             res[index] = descriptors.get( i.next() );
115         }
116         return res;
117     }
118
119     public BackgroundPainter getBackgroundPainter() {
120         BackgroundPainter painter = null;
121         if( null != backgroundPainterClassName ) {
122             ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup( ClassLoader JavaDoc.class );
123             if( null == loader )
124                 loader = ClassLoader.getSystemClassLoader();
125             try {
126                 Class JavaDoc clazz = Class.forName(backgroundPainterClassName, true, loader );
127                 painter = (BackgroundPainter)clazz.newInstance();
128             } catch( Throwable JavaDoc e ) {
129                 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e );
130             }
131         }
132         return painter;
133     }
134     
135         
136         
137     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
138         if( qName.equals( ELEM_ROOT ) ) {
139             
140             //check version
141
String JavaDoc version = attributes.getValue( "version" ); // NOI18N
142
if( !CURRENT_VERSION.equals( version ) ) {
143                 throw new SAXParseException JavaDoc( "Unsupported file version", null ); // NOI18N
144
}
145             
146         } else if( qName.equals( ELEM_BACKGROUND ) ) {
147             
148             //background painter
149
backgroundPainterClassName = attributes.getValue( "class" );
150             
151         } else if( qName.equals( ELEM_PANEL ) ) {
152             
153             //new panel
154
String JavaDoc id = attributes.getValue( "id" ); // NOI18N
155
if( null == id )
156                 throw new SAXParseException JavaDoc( "Required attribute missing: id", null ); // NOI18N
157
String JavaDoc className = attributes.getValue( "class" ); // NOI18N
158
String JavaDoc method = attributes.getValue( "method" ); // NOI18N
159
if( null == className && method == null)
160                 throw new SAXParseException JavaDoc( "Either class or method attribute must be specified", null ); // NOI18N
161

162             ComponentDescriptor descriptor = descriptors.get( id );
163             if( null == descriptor ) {
164                 descriptor = new DefaultComponentDescriptor( id );
165                 descriptors.put( id, descriptor );
166             }
167             
168             descriptor.setClassName( className );
169             descriptor.setMethod( method );
170             
171             currentDescriptor = descriptor;
172             
173             if( !contentIds.contains( id ) )
174                 contentIds.add( id );
175             
176         } else if( qName.equals( ELEM_CONSTRAINTS ) ) {
177             
178             //panel constraints
179
if( null == currentDescriptor ) {
180                 throw new SAXParseException JavaDoc( "Unexpected element: " + ELEM_CONSTRAINTS, null ); // NOI18N
181
}
182             
183             currentDescriptor.setConstraints( parseConstraints( attributes, currentDescriptor.getConstraints() ) );
184         }
185     }
186
187     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
188         if( qName.equals( ELEM_PANEL ) ) {
189             currentDescriptor = null;
190         }
191     }
192     
193     private GridBagConstraints JavaDoc parseConstraints( Attributes JavaDoc attrs, GridBagConstraints JavaDoc defaults ) {
194         GridBagConstraints JavaDoc res = defaults;
195         if( null == res )
196             res = new GridBagConstraints JavaDoc();
197         
198         res.gridx = parseInt( attrs, "gridx", 0, 100, res.gridx ); // NOI18N
199
res.gridy = parseInt( attrs, "gridy", 0, 100, res.gridy ); // NOI18N
200

201         res.gridwidth = parseInt( attrs, "gridwidth", 0, 100, res.gridwidth ); // NOI18N
202
res.gridheight = parseInt( attrs, "gridheight", 0, 100, res.gridheight ); // NOI18N
203

204         res.weightx = parseDouble( attrs, "weightx", 0.0, 1.0, res.weightx ); // NOI18N
205
res.weighty = parseDouble( attrs, "weighty", 0.0, 1.0, res.weighty ); // NOI18N
206

207         res.insets.left = parseInt( attrs, "insetsleft", 0, 1000, res.insets.left ); // NOI18N
208
res.insets.right = parseInt( attrs, "insetsright", 0, 1000, res.insets.right ); // NOI18N
209
res.insets.top = parseInt( attrs, "insetstop", 0, 1000, res.insets.top ); // NOI18N
210
res.insets.bottom = parseInt( attrs, "insetsbottom", 0, 1000, res.insets.bottom ); // NOI18N
211

212         String JavaDoc anchor = attrs.getValue( "anchor" ); // NOI18N
213
if( null != anchor ) {
214             anchor = anchor.toUpperCase();
215             Integer JavaDoc intValue = anchors.get( anchor );
216             if( null != intValue ) {
217                 res.anchor = intValue.intValue();
218             }
219         }
220
221         String JavaDoc fill = attrs.getValue( "fill" ); // NOI18N
222
if( null != fill ) {
223             fill = fill.toUpperCase();
224             Integer JavaDoc intValue = fills.get( fill );
225             if( null != intValue ) {
226                 res.fill = intValue.intValue();
227             }
228         }
229
230         res.ipadx = parseInt( attrs, "padx", 0, 100, res.ipadx ); // NOI18N
231
res.ipady = parseInt( attrs, "pady", 0, 100, res.ipady ); // NOI18N
232

233         return res;
234     }
235
236     private int parseInt( Attributes JavaDoc attrs, String JavaDoc name, int min, int max, int defaultValue ) {
237         int res = defaultValue;
238         String JavaDoc str = attrs.getValue( name );
239         if( null != str ) {
240             try {
241                 res = Integer.parseInt( str );
242             } catch( NumberFormatException JavaDoc nfE ) {
243                 // ignore
244
}
245             if( res < min )
246                 res = min;
247             if( res > max )
248                 res = max;
249         }
250         return res;
251     }
252
253     private double parseDouble( Attributes JavaDoc attrs, String JavaDoc name, double min, double max, double defaultValue ) {
254         double res = defaultValue;
255         String JavaDoc str = attrs.getValue( name );
256         if( null != str ) {
257             try {
258                 res = Double.parseDouble( str );
259             } catch( NumberFormatException JavaDoc nfE ) {
260                 // ignore
261
}
262             if( res < min )
263                 res = min;
264             if( res > max )
265                 res = max;
266         }
267         return res;
268     }
269 }
270
Popular Tags