| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| VariableTool |
|
| 1.8333333333333333;1.833 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | ||
| 23 | package org.webmacro.servlet; | |
| 24 | ||
| 25 | import org.slf4j.Logger; | |
| 26 | import org.slf4j.LoggerFactory; | |
| 27 | import org.webmacro.Context; | |
| 28 | import org.webmacro.ContextTool; | |
| 29 | import org.webmacro.PropertyException; | |
| 30 | ||
| 31 | /** | |
| 32 | * A ContextTool which allows one to snoop information about an object | |
| 33 | * in the active Context. | |
| 34 | * | |
| 35 | * @author Zeljko Trogrlic | |
| 36 | * @author Eric B. Ridge (mailto: ebr@tcdi.com) | |
| 37 | */ | |
| 38 | ||
| 39 | public class VariableTool extends ContextTool | |
| 40 | { | |
| 41 | ||
| 42 | 2 | static Logger _log = LoggerFactory.getLogger(VariableTool.class); |
| 43 | Context context; | |
| 44 | ||
| 45 | public VariableTool () | |
| 46 | 6 | { |
| 47 | 6 | } |
| 48 | ||
| 49 | public VariableTool (Context newContext) | |
| 50 | 0 | { |
| 51 | 0 | context = newContext; |
| 52 | 0 | } |
| 53 | ||
| 54 | public Object init (Context c) throws PropertyException | |
| 55 | { | |
| 56 | 0 | return new VariableTool(c); |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Is the specified object <code>name</code> defined in the active | |
| 61 | * Context? | |
| 62 | */ | |
| 63 | public boolean isDefined (Object name) | |
| 64 | { | |
| 65 | 0 | return context.containsKey(name); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Get the specified object <code>name</code> defined from the active | |
| 70 | * Context. | |
| 71 | */ | |
| 72 | public Object get (Object name) | |
| 73 | { | |
| 74 | 0 | return context.get(name); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Is the specified object, <code>obj</code>, an instance of the | |
| 79 | * specified <code>className</code>?<p> | |
| 80 | * | |
| 81 | * If either parameter is <code>null</code> this method returns false.<br> | |
| 82 | * If <code>className</code> cannot be found, this method returns false.<br> | |
| 83 | * | |
| 84 | * @param obj an Object from your template Context | |
| 85 | * @param className the <b>fully-qualified</b> class name to check | |
| 86 | */ | |
| 87 | public boolean isInstanceOf (Object obj, String className) | |
| 88 | { | |
| 89 | try | |
| 90 | { | |
| 91 | 0 | return (obj != null && className != null) |
| 92 | && (context.getBroker().classForName(className).isAssignableFrom( | |
| 93 | obj.getClass())); | |
| 94 | } | |
| 95 | 0 | catch (ClassNotFoundException cnfe) |
| 96 | { | |
| 97 | 0 | _log.error("VariableTool could not locate the class: /" |
| 98 | + className + "/"); | |
| 99 | } | |
| 100 | 0 | catch (Exception e) |
| 101 | { | |
| 102 | 0 | _log.error("An unexpected exception occured", e); |
| 103 | 0 | } |
| 104 | 0 | return false; |
| 105 | } | |
| 106 | } |