| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jelly.impl.DynamicTagLibrary$1 |
|
|
| 1 | /* |
|
| 2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
| 3 | * |
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 | * you may not use this file except in compliance with the License. |
|
| 6 | * You may obtain a copy of the License at |
|
| 7 | * |
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 | * |
|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 | * See the License for the specific language governing permissions and |
|
| 14 | * limitations under the License. |
|
| 15 | */ |
|
| 16 | package org.apache.commons.jelly.impl; |
|
| 17 | ||
| 18 | import java.util.HashMap; |
|
| 19 | import java.util.Map; |
|
| 20 | ||
| 21 | import org.apache.commons.jelly.JellyException; |
|
| 22 | import org.apache.commons.jelly.Script; |
|
| 23 | import org.apache.commons.jelly.Tag; |
|
| 24 | import org.apache.commons.jelly.TagLibrary; |
|
| 25 | import org.xml.sax.Attributes; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * <p><code>DynamicTagLibrary</code> represents a TagLibrary which |
|
| 29 | * gets created by running a Jelly script.</p> |
|
| 30 | * |
|
| 31 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
| 32 | * @version $Revision: 155420 $ |
|
| 33 | */ |
|
| 34 | public class DynamicTagLibrary extends TagLibrary { |
|
| 35 | ||
| 36 | private String uri; |
|
| 37 | private Map templates = new HashMap(); |
|
| 38 | private TagLibrary parent; |
|
| 39 | ||
| 40 | public DynamicTagLibrary() { |
|
| 41 | } |
|
| 42 | ||
| 43 | public DynamicTagLibrary(String uri) { |
|
| 44 | this.uri = uri; |
|
| 45 | } |
|
| 46 | ||
| 47 | /** Creates a new script to execute the given tag name and attributes */ |
|
| 48 | public TagScript createTagScript(final String name, class="keyword">final Attributes attributes) |
|
| 49 | throws JellyException { |
|
| 50 | ||
| 51 | return new TagScript( |
|
| 52 | new TagFactory() { |
|
| 53 | 0 | public Tag createTag(String name, Attributes attributes) throws JellyException { |
| 54 | 0 | return DynamicTagLibrary.this.createTag(name, attributes); |
| 55 | } |
|
| 56 | } |
|
| 57 | ); |
|
| 58 | } |
|
| 59 | ||
| 60 | /** Creates a new Tag for the given tag name if it exists */ |
|
| 61 | public Tag createTag(String name, Attributes attributes) |
|
| 62 | throws JellyException { |
|
| 63 | ||
| 64 | Object value = templates.get(name); |
|
| 65 | if ( value instanceof Script ) { |
|
| 66 | Script template = (Script) value; |
|
| 67 | return new DynamicTag(template); |
|
| 68 | } |
|
| 69 | else if ( value instanceof TagFactory ) { |
|
| 70 | TagFactory factory = (TagFactory) value; |
|
| 71 | return factory.createTag(name, attributes); |
|
| 72 | } |
|
| 73 | else if ( parent != null ) { |
|
| 74 | // delegate to the parent |
|
| 75 | return parent.createTag(name, attributes); |
|
| 76 | } |
|
| 77 | ||
| 78 | return null; |
|
| 79 | } |
|
| 80 | ||
| 81 | /** |
|
| 82 | * Creates a new tag with the given name and template |
|
| 83 | */ |
|
| 84 | public void registerDynamicTag(String name, Script template) { |
|
| 85 | templates.put(name, template); |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Creates a new Jelly Bean Tag with the given name |
|
| 90 | */ |
|
| 91 | public void registerBeanTag(String name, TagFactory factory) { |
|
| 92 | templates.put(name, factory); |
|
| 93 | } |
|
| 94 | ||
| 95 | /** |
|
| 96 | * Returns the script associated with the given tag name |
|
| 97 | * |
|
| 98 | * @param name The tag name |
|
| 99 | * @return The script associated with <code>name</code>, or |
|
| 100 | * <code>null</code> if the tag doesn't exist or isn't a script |
|
| 101 | */ |
|
| 102 | public Script getDynamicTag(String name) { |
|
| 103 | Object result = templates.get(name); |
|
| 104 | return (result instanceof Script) ? (Script) result : null; |
|
| 105 | } |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Returns the tag library instance which contains the named tag. |
|
| 109 | * <p/> |
|
| 110 | * If the tag is not registered within this library, the set of |
|
| 111 | * parent libraries will be searched. |
|
| 112 | * |
|
| 113 | * @param name The tag name |
|
| 114 | * @return The tag library containing the named tag, or <code>null</code> |
|
| 115 | * if the tag is not registered. |
|
| 116 | */ |
|
| 117 | public DynamicTagLibrary find(String name) { |
|
| 118 | DynamicTagLibrary result = null; |
|
| 119 | if (templates.get(name) != null) { |
|
| 120 | result = this; |
|
| 121 | } |
|
| 122 | else if (parent instanceof DynamicTagLibrary) { |
|
| 123 | result = ((DynamicTagLibrary) parent).find(name); |
|
| 124 | } |
|
| 125 | return result; |
|
| 126 | } |
|
| 127 | ||
| 128 | // Properties |
|
| 129 | //------------------------------------------------------------------------- |
|
| 130 | public String getUri() { |
|
| 131 | return uri; |
|
| 132 | } |
|
| 133 | ||
| 134 | public void setUri(String uri) { |
|
| 135 | this.uri = uri; |
|
| 136 | } |
|
| 137 | ||
| 138 | ||
| 139 | /** |
|
| 140 | * Returns the parent library which will be used to resolve unknown tags. |
|
| 141 | * @return TagLibrary |
|
| 142 | */ |
|
| 143 | public TagLibrary getParent() { |
|
| 144 | return parent; |
|
| 145 | } |
|
| 146 | ||
| 147 | /** |
|
| 148 | * Sets the parent to inherit tags from that are not defined in this library. |
|
| 149 | * @param parent The parent to set |
|
| 150 | */ |
|
| 151 | public void setParent(TagLibrary parent) { |
|
| 152 | this.parent = parent; |
|
| 153 | } |
|
| 154 | ||
| 155 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |