| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MacroAdapter |
|
| 2.0;2 |
| 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.engine; | |
| 24 | ||
| 25 | import java.io.IOException; | |
| 26 | ||
| 27 | import org.webmacro.Context; | |
| 28 | import org.webmacro.FastWriter; | |
| 29 | import org.webmacro.Macro; | |
| 30 | import org.webmacro.TemplateVisitor; | |
| 31 | import org.webmacro.Visitable; | |
| 32 | ||
| 33 | /** | |
| 34 | * Looks like a Macro, but really it's not. Wrap any object as | |
| 35 | * a Macro via the createMacro method. You can use this when you | |
| 36 | * have to return a Macro, but what you have is something else. | |
| 37 | */ | |
| 38 | final public class MacroAdapter implements Macro, Visitable | |
| 39 | { | |
| 40 | ||
| 41 | private Object _self; | |
| 42 | ||
| 43 | private MacroAdapter (Object wrapMe) | |
| 44 | throws BuildException | |
| 45 | 0 | { |
| 46 | 0 | _self = wrapMe; |
| 47 | 0 | if (_self == null) |
| 48 | { | |
| 49 | 0 | throw new BuildException("MacroAdapter cannot wrap null"); |
| 50 | } | |
| 51 | 0 | } |
| 52 | ||
| 53 | public final String toString () | |
| 54 | { | |
| 55 | 0 | return _self.toString(); |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Returns the wrapped object, context is ignored. | |
| 60 | */ | |
| 61 | public final Object evaluate (Context context) | |
| 62 | { | |
| 63 | 0 | return _self; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Just calls toString() and writes that, context is ignored. | |
| 68 | */ | |
| 69 | public final void write (FastWriter out, Context context) | |
| 70 | throws IOException | |
| 71 | { | |
| 72 | 0 | out.write(_self.toString()); |
| 73 | 0 | } |
| 74 | ||
| 75 | public void accept (TemplateVisitor v) | |
| 76 | { | |
| 77 | 0 | v.visitString(_self.toString()); |
| 78 | 0 | } |
| 79 | ||
| 80 | /** | |
| 81 | * If wrapMe is not a Macro, wrap it and return it. If it | |
| 82 | * is a Macro already, just return it. | |
| 83 | */ | |
| 84 | public final static Macro createMacro (Object wrapMe) | |
| 85 | throws BuildException | |
| 86 | { | |
| 87 | 0 | if (wrapMe == null) |
| 88 | { | |
| 89 | 0 | throw new BuildException("Bug in WM: attempt to write null"); |
| 90 | } | |
| 91 | 0 | if (wrapMe instanceof Macro) |
| 92 | { | |
| 93 | 0 | return (Macro) wrapMe; |
| 94 | } | |
| 95 | else | |
| 96 | { | |
| 97 | 0 | return new MacroAdapter(wrapMe); |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | } | |
| 102 |