1 18 package org.apache.beehive.netui.core.urltemplates; 19 20 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateConfigDocument.UrlTemplateConfig; 21 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateDocument; 22 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefGroupDocument; 23 import org.apache.beehive.netui.core.urltemplates.schema.UrlTemplateRefDocument; 24 import org.apache.beehive.netui.util.logging.Logger; 25 import org.apache.xmlbeans.XmlCursor; 26 import org.apache.xmlbeans.XmlException; 27 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 35 38 public class URLTemplatesFactory 39 { 40 private static final Logger _log = Logger.getInstance( DefaultURLTemplateFileParser.class ); 41 42 43 public static final String DEFAULT_URL_TEMPLATE_CONFIG_FILE_PATH = "/WEB-INF/beehive-url-template-config.xml"; 44 45 private String _configFilePath = DEFAULT_URL_TEMPLATE_CONFIG_FILE_PATH; 47 48 private URLTemplateFileParser _parser = new DefaultURLTemplateFileParser(); 50 51 private Collection _knownTokens = null; 53 54 private Collection _requiredTokens = null; 56 57 62 public void setConfigFilePath( String configFilePath ) 63 { 64 if ( configFilePath == null ) 65 { 66 throw new IllegalArgumentException ( "Config file path cannot be null." ); 67 } 68 69 _configFilePath = configFilePath; 70 } 71 72 81 public void setParser( URLTemplateFileParser parser ) 82 { 83 if ( parser == null ) 84 { 85 throw new IllegalArgumentException ( "Parser cannot be null." ); 86 } 87 88 _parser = parser; 89 } 90 91 101 public void setKnownTokens( Collection knownTokens ) 102 { 103 _knownTokens = knownTokens; 104 } 105 106 116 public void setRequiredTokens( Collection requiredTokens ) 117 { 118 _requiredTokens = requiredTokens; 119 } 120 121 127 public URLTemplates getTemplates( ServletContext servletContext ) 128 { 129 URLTemplates urlTemplates = new URLTemplates(); 130 InputStream stream = null; 131 132 try 133 { 134 UrlTemplateConfig urlTemplateConfig = null; 135 stream = servletContext.getResourceAsStream( _configFilePath ); 136 if ( stream != null ) 137 { 138 urlTemplateConfig = _parser.parse( stream ); 139 urlTemplates = getTemplatesFromConfig( urlTemplateConfig ); 140 } 141 else 142 { 143 _log.info( "Running without URL template descriptor, " + _configFilePath ); 145 } 146 } 147 catch ( XmlException xe ) 148 { 149 _log.error( "Malformed URL template descriptor in " + _configFilePath, xe ); 151 } 152 catch ( IOException ioe ) 153 { 154 _log.error( "Problem parsing URL template descriptor in " + _configFilePath, ioe ); 156 } 157 finally 158 { 159 if ( stream != null ) 161 { 162 try 163 { 164 stream.close(); 165 } 166 catch ( Exception ignore ) 167 { 168 } 169 } 170 } 171 172 return urlTemplates; 173 } 174 175 181 protected URLTemplates getTemplatesFromConfig( UrlTemplateConfig urlTemplateConfig ) 182 { 183 URLTemplates urlTemplates = new URLTemplates(); 184 185 UrlTemplateDocument.UrlTemplate[] templates = urlTemplateConfig.getUrlTemplateArray(); 187 for ( int i = 0; i < templates.length; i++ ) 188 { 189 String name = templates[i].getName().trim(); 190 String value = templates[i].getValue().trim(); 191 if ( value != null ) 192 { 193 value = value.trim(); 194 if ( _log.isDebugEnabled() ) 195 { 196 _log.debug( "[URLTemplate] " + name + " = " + value ); 197 } 198 URLTemplate urlTemplate = new URLTemplate( value, _knownTokens, _requiredTokens ); 199 urlTemplate.verify(); 200 urlTemplates.addTemplate( name, urlTemplate ); 201 } 202 } 203 204 UrlTemplateRefGroupDocument.UrlTemplateRefGroup[] templateRefGroups = urlTemplateConfig.getUrlTemplateRefGroupArray (); 206 for ( int i = 0; i < templateRefGroups.length; i++ ) 207 { 208 HashMap refGroup = new HashMap (); 209 String refGroupName = templateRefGroups[i].getName().trim(); 210 UrlTemplateRefDocument.UrlTemplateRef[] templateRefs = templateRefGroups[i].getUrlTemplateRefArray(); 211 for ( int j = 0; j < templateRefs.length; j++ ) 212 { 213 String key = templateRefs[j].getKey().toString().trim(); 214 String name = templateRefs[j].getTemplateName().trim(); 215 if ( _log.isDebugEnabled() ) 216 { 217 _log.debug( "[" + refGroupName + " URLTemplate] " + key + " = " + name ); 218 } 219 refGroup.put( key, name ); 220 } 221 urlTemplates.addTemplateRefGroup( refGroupName, refGroup ); 222 } 223 224 return urlTemplates; 225 } 226 } 227 | Popular Tags |