File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\jelly\MorphTagLibrary.java.html

1 package hudson.util.jelly;
2
3 import org.apache.commons.jelly.JellyContext;
4 import org.apache.commons.jelly.JellyException;
5 import org.apache.commons.jelly.JellyTagException;
6 import org.apache.commons.jelly.Tag;
7 import org.apache.commons.jelly.TagLibrary;
8 import org.apache.commons.jelly.XMLOutput;
9 import org.apache.commons.jelly.expression.Expression;
10 import org.apache.commons.jelly.impl.ExpressionAttribute;
11 import org.apache.commons.jelly.impl.TagScript;
12 import org.xml.sax.Attributes;
13 import org.xml.sax.SAXException;
14 import org.xml.sax.helpers.AttributesImpl;
15
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Map;
20
21 /**
22 * Jelly tag library for literal-like tags, with an ability to add arbitrary attributes taken from a map.
23 *
24 * <p>
25 * Tags from this namespace ("jelly:hudson.util.jelly.MorphTagLibrary") behaves mostly like literal static tags,
26 * except it interprets two attributes "ATTRIBUTES" and "EXCEPT" in a special way.
27 *
28 * The "ATTRIBUTES" attribute should have a Jelly expression that points to a {@link Map} object,
29 * and the contents of the map are added as attributes of this tag, with the exceptions of entries whose key
30 * values are listed in the "EXCEPT" attribute.
31 *
32 * The "EXCEPT" attribute takes a white-space separated list of attribute names that should be ignored even
33 * if it's in the map.
34 *
35 * <p>
36 * The explicit literal attributes, if specified, always take precedence over the dynamic attributes added by the map.
37 *
38 * <p>
39 * See textbox.jelly as an example of using this tag library.
40 *
41 * @author Kohsuke Kawaguchi
42 * @since 1.342
43 */
44 public class MorphTagLibrary extends TagLibrary {
45 /**
46 * This code is really only used for dealing with dynamic tag libraries, so no point in implementing
47 * this for statically used tag libraries.
48 */
49 @Override
50 public Tag createTag(final String name, Attributes attributes) throws JellyException {
51 return null;
52 }
53
54 @Override
55 public TagScript createTagScript(final String tagName, Attributes attributes) throws JellyException {
56 return new TagScript() {
57 private Object evalAttribute(String name, JellyContext context) {
58 ExpressionAttribute e = attributes.get(name);
59 if (e==null) return null;
60 return e.exp.evaluate(context);
61 }
62
63 private Collection<?> getExclusions(JellyContext context) {
64 Object exclusion = evalAttribute(EXCEPT_ATTRIBUTES,context);
65 if (exclusion==null)
66 return Collections.emptySet();
67 if (exclusion instanceof String)
68 return Arrays.asList(exclusion.toString().split("\\s+")); // split by whitespace
69 if (exclusion instanceof Collection)
70 return (Collection)exclusion;
71 throw new IllegalArgumentException("Expected collection for exclusion but found :"+exclusion);
72 }
73
74 @Override
75 public void run(JellyContext context, XMLOutput output) throws JellyTagException {
76 AttributesImpl actual = new AttributesImpl();
77
78 Collection<?> exclusions = getExclusions(context);
79
80 Map<String,?> meta = (Map)evalAttribute(META_ATTRIBUTES,context);
81 if (meta!=null) {
82 for (Map.Entry<String,?> e : meta.entrySet()) {
83 String key = e.getKey();
84 // @see jelly.impl.DynamicTag.setAttribute() -- ${attrs} has duplicates with "Attr" suffix
85 if (key.endsWith("Attr") && meta.containsKey(key.substring(0, key.length()-4))) continue;
86 // @see http://github.com/jenkinsci/jelly/commit/4ae67d15957b5b4d32751619997a3cb2a6ad56ed
87 if (key.equals("ownerTag")) continue;
88 if (!exclusions.contains(key)) {
89 Object v = e.getValue();
90 if (v!=null)
91 actual.addAttribute("", key, key,"CDATA", v.toString());
92 }
93 }
94 } else {
95 meta = Collections.emptyMap();
96 }
97
98 for (Map.Entry<String,ExpressionAttribute> e : attributes.entrySet()) {
99 String name = e.getKey();
100 if (name.equals(META_ATTRIBUTES) || name.equals(EXCEPT_ATTRIBUTES)) continue; // already handled
101 if (meta.containsKey(name)) {
102 // if the explicit value is also generated by a map, delete it first.
103 // this is O(N) operation, but we don't expect there to be a lot of collisions.
104 int idx = actual.getIndex(name);
105 if(idx>=0) actual.removeAttribute(idx);
106 }
107
108 Expression expression = e.getValue().exp;
109 actual.addAttribute("",name,name,"CDATA",expression.evaluateAsString(context));
110 }
111
112 try {
113 output.startElement(tagName,actual);
114 getTagBody().run(context,output);
115 output.endElement(tagName);
116 } catch (SAXException x) {
117 throw new JellyTagException(x);
118 }
119 }
120 };
121 }
122
123 private static final String META_ATTRIBUTES = "ATTRIBUTES";
124 private static final String EXCEPT_ATTRIBUTES = "EXCEPT";
125 }
126