1 19 20 package org.netbeans.modules.welcome.content; 21 22 import java.awt.GridBagConstraints ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import javax.xml.parsers.ParserConfigurationException ; 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 ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.SAXParseException ; 40 import org.xml.sax.XMLReader ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 43 48 class ContentParser extends DefaultHandler { 49 50 private static final String ELEM_ROOT = "welcomepage"; private static final String ELEM_BACKGROUND = "background"; private static final String ELEM_PANEL = "panel"; private static final String ELEM_CONSTRAINTS = "constraints"; 55 private static final String CURRENT_VERSION = "1.0"; 57 private Map <String , Integer > anchors = new HashMap <String , Integer >( 10 ); 58 private Map <String , Integer > fills = new HashMap <String , Integer >( 4 ); 59 60 private Map <String ,ComponentDescriptor> descriptors = new HashMap <String ,ComponentDescriptor>( 5 ); 61 private ArrayList <String > contentIds = new ArrayList <String >( 5 ); 62 private String backgroundPainterClassName; 63 64 private ComponentDescriptor currentDescriptor; 65 66 private static final String CONTENT_ROOT_FOLDER = "WelcomePage"; 68 public ContentParser() { 69 anchors.put( "CENTER", Integer.valueOf( GridBagConstraints.CENTER ) ); anchors.put( "WEST", Integer.valueOf( GridBagConstraints.WEST ) ); anchors.put( "NORTH", Integer.valueOf( GridBagConstraints.NORTH ) ); anchors.put( "NORTHEAST", Integer.valueOf( GridBagConstraints.NORTHEAST ) ); anchors.put( "NORTHEAST", Integer.valueOf( GridBagConstraints.NORTHEAST ) ); anchors.put( "EAST", Integer.valueOf( GridBagConstraints.EAST ) ); anchors.put( "SOUTH", Integer.valueOf( GridBagConstraints.SOUTH ) ); anchors.put( "SOUTHEAST", Integer.valueOf( GridBagConstraints.SOUTHEAST ) ); anchors.put( "SOUTHWEST", Integer.valueOf( GridBagConstraints.SOUTHWEST ) ); 79 fills.put( "NONE", Integer.valueOf( GridBagConstraints.NONE ) ); fills.put( "HORIZONTAL", Integer.valueOf( GridBagConstraints.HORIZONTAL ) ); fills.put( "VERTICAL", Integer.valueOf( GridBagConstraints.VERTICAL ) ); fills.put( "BOTH", Integer.valueOf( GridBagConstraints.BOTH ) ); } 84 85 public void parse() throws ParserConfigurationException , SAXException , IOException { 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 , SAXException , IOException { 99 String layoutFilePath = BundleSupport.getURL( "Layout" ); URL url = new URL (layoutFilePath); 101 parse( url.openStream() ); 102 } 103 104 private void parse( InputStream input ) throws ParserConfigurationException , SAXException , IOException { 105 XMLReader reader = XMLUtil.createXMLReader(false, false); 106 reader.setContentHandler(this); 107 reader.parse(new InputSource (input)); 108 } 109 110 public ComponentDescriptor[] getContents() { 111 ComponentDescriptor[] res = new ComponentDescriptor[ contentIds.size() ]; 112 int index = 0; 113 for( Iterator <String > 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 loader = (ClassLoader )Lookup.getDefault().lookup( ClassLoader .class ); 123 if( null == loader ) 124 loader = ClassLoader.getSystemClassLoader(); 125 try { 126 Class clazz = Class.forName(backgroundPainterClassName, true, loader ); 127 painter = (BackgroundPainter)clazz.newInstance(); 128 } catch( Throwable e ) { 129 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e ); 130 } 131 } 132 return painter; 133 } 134 135 136 137 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 138 if( qName.equals( ELEM_ROOT ) ) { 139 140 String version = attributes.getValue( "version" ); if( !CURRENT_VERSION.equals( version ) ) { 143 throw new SAXParseException ( "Unsupported file version", null ); } 145 146 } else if( qName.equals( ELEM_BACKGROUND ) ) { 147 148 backgroundPainterClassName = attributes.getValue( "class" ); 150 151 } else if( qName.equals( ELEM_PANEL ) ) { 152 153 String id = attributes.getValue( "id" ); if( null == id ) 156 throw new SAXParseException ( "Required attribute missing: id", null ); String className = attributes.getValue( "class" ); String method = attributes.getValue( "method" ); if( null == className && method == null) 160 throw new SAXParseException ( "Either class or method attribute must be specified", null ); 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 if( null == currentDescriptor ) { 180 throw new SAXParseException ( "Unexpected element: " + ELEM_CONSTRAINTS, null ); } 182 183 currentDescriptor.setConstraints( parseConstraints( attributes, currentDescriptor.getConstraints() ) ); 184 } 185 } 186 187 public void endElement(String uri, String localName, String qName) throws SAXException { 188 if( qName.equals( ELEM_PANEL ) ) { 189 currentDescriptor = null; 190 } 191 } 192 193 private GridBagConstraints parseConstraints( Attributes attrs, GridBagConstraints defaults ) { 194 GridBagConstraints res = defaults; 195 if( null == res ) 196 res = new GridBagConstraints (); 197 198 res.gridx = parseInt( attrs, "gridx", 0, 100, res.gridx ); res.gridy = parseInt( attrs, "gridy", 0, 100, res.gridy ); 201 res.gridwidth = parseInt( attrs, "gridwidth", 0, 100, res.gridwidth ); res.gridheight = parseInt( attrs, "gridheight", 0, 100, res.gridheight ); 204 res.weightx = parseDouble( attrs, "weightx", 0.0, 1.0, res.weightx ); res.weighty = parseDouble( attrs, "weighty", 0.0, 1.0, res.weighty ); 207 res.insets.left = parseInt( attrs, "insetsleft", 0, 1000, res.insets.left ); res.insets.right = parseInt( attrs, "insetsright", 0, 1000, res.insets.right ); res.insets.top = parseInt( attrs, "insetstop", 0, 1000, res.insets.top ); res.insets.bottom = parseInt( attrs, "insetsbottom", 0, 1000, res.insets.bottom ); 212 String anchor = attrs.getValue( "anchor" ); if( null != anchor ) { 214 anchor = anchor.toUpperCase(); 215 Integer intValue = anchors.get( anchor ); 216 if( null != intValue ) { 217 res.anchor = intValue.intValue(); 218 } 219 } 220 221 String fill = attrs.getValue( "fill" ); if( null != fill ) { 223 fill = fill.toUpperCase(); 224 Integer 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 ); res.ipady = parseInt( attrs, "pady", 0, 100, res.ipady ); 233 return res; 234 } 235 236 private int parseInt( Attributes attrs, String name, int min, int max, int defaultValue ) { 237 int res = defaultValue; 238 String str = attrs.getValue( name ); 239 if( null != str ) { 240 try { 241 res = Integer.parseInt( str ); 242 } catch( NumberFormatException nfE ) { 243 } 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 attrs, String name, double min, double max, double defaultValue ) { 254 double res = defaultValue; 255 String str = attrs.getValue( name ); 256 if( null != str ) { 257 try { 258 res = Double.parseDouble( str ); 259 } catch( NumberFormatException nfE ) { 260 } 262 if( res < min ) 263 res = min; 264 if( res > max ) 265 res = max; 266 } 267 return res; 268 } 269 } 270 | Popular Tags |