//# 4 errors, 105 messages
//#
/*
    //#ThemeResourceLoader.java:1:1: class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
* Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.  For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/
/*
 * ThemeResourceLoader.java
 *
 * Created on June 28, 2005, 12:25 PM
 */

package org.apache.roller.weblogger.ui.rendering.velocity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.roller.weblogger.WebloggerException;
import org.apache.roller.weblogger.business.themes.ThemeNotFoundException;
import org.apache.roller.weblogger.business.WebloggerFactory;
import org.apache.roller.weblogger.business.themes.ThemeManager;
import org.apache.roller.weblogger.pojos.Theme;
import org.apache.roller.weblogger.pojos.ThemeTemplate;


/**
 * The ThemeResourceLoader is a Velocity template loader which loads
 * templates from shared themes.
 *
 * @author Allen Gilliland
 */
public class ThemeResourceLoader extends ResourceLoader {
    //#ThemeResourceLoader.java:49: method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader()
    //#ThemeResourceLoader.java:49: Warning: method not available
    //#    -- call on void org.apache.velocity.runtime.resource.loader.ResourceLoader()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader()
    //#    unanalyzed callee: void org.apache.velocity.runtime.resource.loader.ResourceLoader()
    //#input(void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader()): this
    //#ThemeResourceLoader.java:49: end of method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader()
    
    private static Log mLogger = 
    //#ThemeResourceLoader.java:51: method: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init
    //#ThemeResourceLoader.java:51: Warning: method not available
    //#    -- call on LogFactory org.apache.commons.logging.LogFactory:getFactory()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init
    //#    unanalyzed callee: LogFactory org.apache.commons.logging.LogFactory:getFactory()
    //#ThemeResourceLoader.java:51: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getInstance(Class)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getInstance(Class)
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/velocity/ThemeResourceLoader]
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.getLastModified(Lorg/apache/velocity/runtime/resource/Resource;)J
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.getResourceStream(Ljava/lang/String;)Ljava/io/InputStream;
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.init(Lorg/apache/commons/collections/ExtendedProperties;)V
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.isSourceModified(Lorg/apache/velocity/runtime/resource/Resource;)Z
    //#output(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): mLogger
    //#presumption(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): org.apache.commons.logging.LogFactory:getFactory(...)@51 != null
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/velocity/ThemeResourceLoader] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.getLastModified(Lorg/apache/velocity/runtime/resource/Resource;)J == &getLastModified
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.getResourceStream(Ljava/lang/String;)Ljava/io/InputStream; == &getResourceStream
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.init(Lorg/apache/commons/collections/ExtendedProperties;)V == &init
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): __Dispatch_Table.isSourceModified(Lorg/apache/velocity/runtime/resource/Resource;)Z == &isSourceModified
    //#post(org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init): init'ed(mLogger)
    //#ThemeResourceLoader.java:51: end of method: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init
        LogFactory.getFactory().getInstance(ThemeResourceLoader.class);
        
    
    public void init(ExtendedProperties configuration) {
        mLogger.debug(configuration);
    //#ThemeResourceLoader.java:56: method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.init(ExtendedProperties)
    //#ThemeResourceLoader.java:56: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: void init(ExtendedProperties)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    //#input(void init(ExtendedProperties)): configuration
    //#input(void init(ExtendedProperties)): mLogger
    //#pre[2] (void init(ExtendedProperties)): mLogger != null
    }
    //#ThemeResourceLoader.java:57: end of method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.init(ExtendedProperties)
    
    
    public InputStream getResourceStream( String name )
        throws ResourceNotFoundException {
        
        mLogger.debug("Looking up resource named ... "+name);
    //#ThemeResourceLoader.java:63: method: InputStream org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.getResourceStream(String)
    //#ThemeResourceLoader.java:63: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
    //#ThemeResourceLoader.java:63: ?check always fails: InputStream getResourceStream(String) always ends with an exception
    //#input(InputStream getResourceStream(String)): "Invalid ThemeRL key "._tainted
    //#input(InputStream getResourceStream(String)): "Looking up resource named ... "._tainted
    //#input(InputStream getResourceStream(String)): "RollerResourceLoader Error: "._tainted
    //#input(InputStream getResourceStream(String)): "Template ["._tainted
    //#input(InputStream getResourceStream(String)): "ThemeResourceLoader Error: "._tainted
    //#input(InputStream getResourceStream(String)): "] doesn't seem to be part of theme ["._tainted
    //#input(InputStream getResourceStream(String)): "]"._tainted
    //#input(InputStream getResourceStream(String)): mLogger
    //#input(InputStream getResourceStream(String)): name
    //#input(InputStream getResourceStream(String)): name._tainted
    //#output(InputStream getResourceStream(String)): new ByteArrayInputStream(getResourceStream#6) num objects
    //#output(InputStream getResourceStream(String)): return_value
    //#new obj(InputStream getResourceStream(String)): new ByteArrayInputStream(getResourceStream#6)
    //#pre[1] (InputStream getResourceStream(String)): (soft) mLogger != null
    //#pre[2] (InputStream getResourceStream(String)): (soft) name != null
    //#presumption(InputStream getResourceStream(String)): java.lang.String:length(...)@65 >= 1
        
        if (name == null || name.length() < 1) {
            throw new ResourceNotFoundException("Need to specify a template name!");
        }
        
        try {
            // parse the name ... theme templates name are <theme>:<template>
            String[] split = name.split(":", 2);
            if(split.length < 2)
    //#ThemeResourceLoader.java:72: ?use of default init
    //#    init'ed(split.length)
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    basic block: bb_2
    //#    assertion: init'ed(split.length)
    //#    VN: undefined
    //#    Expected: {-Inf..+Inf}
    //#    Bad: {Invalid}
    //#    Attribs:  Int  Bad only invalid
    //#ThemeResourceLoader.java:72: Warning: test always goes same way
    //#    test predetermined because split.length == 0
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    from bb: bb_2
    //#    live edge: bb_2-->bb_3
    //#    tested vn: undefined - 2
    //#    tested vn values: {-2}
                throw new ResourceNotFoundException("Invalid ThemeRL key "+name);
    //#ThemeResourceLoader.java:73: ?!conditional throw
    //#    split.length >= 2
    //#    severity: HIGH
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    basic block: bb_2
    //#    assertion: split.length >= 2
    //#    VN: undefined - 2
    //#    Expected: {0..+Inf}
    //#    Bad: {-2}
    //#    Attribs:  Int  Bad singleton  Bad overlaps +/-1000  Bad < Exp
    //#ThemeResourceLoader.java:73: Warning: method not available
    //#    -- call on void org.apache.velocity.exception.ResourceNotFoundException(String)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    unanalyzed callee: void org.apache.velocity.exception.ResourceNotFoundException(String)
            
            // lookup the template from the proper theme
            ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
    //#ThemeResourceLoader.java:76: Warning: dead code
    //#    dead code here because split.length == 0
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    dead bb: bb_4
            Theme theme = themeMgr.getTheme(split[0]);
            ThemeTemplate template = theme.getTemplateByName(split[1]);
            
            if(template == null)
                throw new ResourceNotFoundException("Template ["+split[1]+
                        "] doesn't seem to be part of theme ["+split[0]+"]");
            
            mLogger.debug("Resource found!");
    //#ThemeResourceLoader.java:84: Warning: dead code continues
    //#    dead code continues
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: InputStream getResourceStream(String)
    //#    dead bb: bb_6
            
            // return the input stream
            return new ByteArrayInputStream(template.getContents().getBytes("UTF-8"));
            
        } catch (UnsupportedEncodingException uex) {
            // We expect UTF-8 in all JRE installation.
            // This rethrows as a Runtime exception after logging.
            mLogger.error(uex);
            throw new RuntimeException(uex);
           
        } catch (ThemeNotFoundException tnfe) {
            String msg = "ThemeResourceLoader Error: " + tnfe.getMessage();
            mLogger.error(msg, tnfe);
            throw new ResourceNotFoundException(msg);
            
        } catch (WebloggerException re) {
            String msg = "RollerResourceLoader Error: " + re.getMessage();
            mLogger.error( msg, re );
            throw new ResourceNotFoundException(msg);
    //#ThemeResourceLoader.java:103: end of method: InputStream org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.getResourceStream(String)
        }
    }
    
    
    public boolean isSourceModified(Resource resource) {
        return (resource.getLastModified() != this.getLastModified(resource));
    //#ThemeResourceLoader.java:109: method: bool org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.isSourceModified(Resource)
    //#ThemeResourceLoader.java:109: Warning: method not available
    //#    -- call on long org.apache.velocity.runtime.resource.Resource:getLastModified()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: bool isSourceModified(Resource)
    //#    unanalyzed callee: long org.apache.velocity.runtime.resource.Resource:getLastModified()
    //#input(bool isSourceModified(Resource)): "Checking last modified time for resource named ... "._tainted
    //#input(bool isSourceModified(Resource)): __Descendant_Table[org/apache/roller/weblogger/ui/rendering/velocity/ThemeResourceLoader]
    //#input(bool isSourceModified(Resource)): __Descendant_Table[others]
    //#input(bool isSourceModified(Resource)): __Dispatch_Table.getLastModified(Lorg/apache/velocity/runtime/resource/Resource;)J
    //#input(bool isSourceModified(Resource)): mLogger
    //#input(bool isSourceModified(Resource)): resource
    //#input(bool isSourceModified(Resource)): this
    //#input(bool isSourceModified(Resource)): this.__Tag
    //#output(bool isSourceModified(Resource)): return_value
    //#pre[1] (bool isSourceModified(Resource)): mLogger != null
    //#pre[2] (bool isSourceModified(Resource)): resource != null
    //#pre[4] (bool isSourceModified(Resource)): this.__Tag == org/apache/roller/weblogger/ui/rendering/velocity/ThemeResourceLoader
    //#post(bool isSourceModified(Resource)): init'ed(return_value)
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.commons.logging.Log:debug
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.StringBuilder
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.StringBuilder:append
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.StringBuilder:toString
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.String:length
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.String:split
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.roller.weblogger.business.Weblogger:getThemeManager
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.roller.weblogger.business.themes.ThemeManager:getTheme
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.roller.weblogger.pojos.Theme:getTemplateByName
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.velocity.runtime.resource.Resource:getName
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:org.apache.roller.weblogger.pojos.ThemeTemplate:getLastModified
    //#unanalyzed(bool isSourceModified(Resource)): Effects-of-calling:java.util.Date:getTime
    //#ThemeResourceLoader.java:109: end of method: bool org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.isSourceModified(Resource)
    }
    
    
    public long getLastModified(Resource resource) {
        long last_mod = 0;
    //#ThemeResourceLoader.java:114: method: long org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.getLastModified(Resource)
    //#input(long getLastModified(Resource)): "Checking last modified time for resource named ... "._tainted
    //#input(long getLastModified(Resource)): mLogger
    //#input(long getLastModified(Resource)): resource
    //#output(long getLastModified(Resource)): return_value
    //#pre[1] (long getLastModified(Resource)): mLogger != null
    //#pre[2] (long getLastModified(Resource)): resource != null
    //#post(long getLastModified(Resource)): return_value == 0
    //#test_vector(long getLastModified(Resource)): java.lang.String:length(...)@119: {1..4_294_967_295}, {0}
    //#test_vector(long getLastModified(Resource)): org.apache.velocity.runtime.resource.Resource:getName(...)@115: Addr_Set{null}, Inverse{null}
        String name = resource.getName();
    //#ThemeResourceLoader.java:115: Warning: method not available
    //#    -- call on String org.apache.velocity.runtime.resource.Resource:getName()
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    unanalyzed callee: String org.apache.velocity.runtime.resource.Resource:getName()
        
        mLogger.debug("Checking last modified time for resource named ... "+name);
    //#ThemeResourceLoader.java:117: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:debug(Object)
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    unanalyzed callee: void org.apache.commons.logging.Log:debug(Object)
        
        if (name == null || name.length() < 1)
            return last_mod;
        
        try {
            // parse the name ... theme templates name are <theme>:<template>
            String[] split = name.split(":", 2);
            if(split.length < 2)
    //#ThemeResourceLoader.java:125: ?use of default init
    //#    init'ed(split.length)
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    basic block: bb_5
    //#    assertion: init'ed(split.length)
    //#    VN: undefined
    //#    Expected: {-Inf..+Inf}
    //#    Bad: {Invalid}
    //#    Attribs:  Int  Bad only invalid
    //#ThemeResourceLoader.java:125: Warning: test always goes same way
    //#    test predetermined because split.length == 0
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    from bb: bb_5
    //#    live edge: bb_5-->bb_6
    //#    tested vn: undefined - 2
    //#    tested vn values: {-2}
                return last_mod;
            
            // lookup the template from the proper theme
            ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
    //#ThemeResourceLoader.java:129: Warning: dead code
    //#    dead code here because split.length == 0
    //#    severity: LOW
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    dead bb: bb_7
    //#ThemeResourceLoader.java:129: Warning: dead code continues
    //#    dead code continues
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    dead bb: bb_8
            Theme theme = themeMgr.getTheme(split[0]);
            ThemeTemplate template = theme.getTemplateByName(split[1]);
            
            if(template == null)
                return last_mod;
    //#ThemeResourceLoader.java:134: Warning: dead code continues
    //#    dead code continues
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    dead bb: bb_9
            
            last_mod = template.getLastModified().getTime();
    //#ThemeResourceLoader.java:136: Warning: dead code continues
    //#    dead code continues
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    dead bb: bb_10
    //#ThemeResourceLoader.java:136: Warning: dead code continues
    //#    dead code continues
    //#    severity: INFORMATIONAL
    //#    class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
    //#    method: long getLastModified(Resource)
    //#    dead bb: bb_11
            
        } catch (ThemeNotFoundException tnfe) {
            // ignore
        } catch (WebloggerException re) {
            // we don't like to see this happen, but oh well
        }
        
        return last_mod;
    //#ThemeResourceLoader.java:144: end of method: long org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader.getLastModified(Resource)
    }
    
}
    //#ThemeResourceLoader.java:: end of class: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader
