Clover coverage report - Code Coverage for tapestry-contrib release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:28:27 EST
file stats: LOC: 320   Methods: 11
NCLOC: 191   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ShowTemplate.java 0% 0% 0% 0%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.contrib.inspector;
 16   
 17    import java.util.Iterator;
 18    import java.util.Map;
 19   
 20    import org.apache.tapestry.BaseComponent;
 21    import org.apache.tapestry.IComponent;
 22    import org.apache.tapestry.IDirect;
 23    import org.apache.tapestry.IMarkupWriter;
 24    import org.apache.tapestry.IRender;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.Tapestry;
 27    import org.apache.tapestry.engine.DirectServiceParameter;
 28    import org.apache.tapestry.engine.IEngineService;
 29    import org.apache.tapestry.engine.ILink;
 30    import org.apache.tapestry.parse.CloseToken;
 31    import org.apache.tapestry.parse.ComponentTemplate;
 32    import org.apache.tapestry.parse.LocalizationToken;
 33    import org.apache.tapestry.parse.OpenToken;
 34    import org.apache.tapestry.parse.TemplateToken;
 35    import org.apache.tapestry.parse.TextToken;
 36    import org.apache.tapestry.parse.TokenType;
 37    import org.apache.tapestry.services.TemplateSource;
 38   
 39    /**
 40    * Component of the {@link Inspector}page used to display the ids and types of all embedded
 41    * components.
 42    *
 43    * @author Howard Lewis Ship
 44    */
 45   
 46    public abstract class ShowTemplate extends BaseComponent implements IDirect
 47    {
 48    /** @since 4.0 */
 49    public abstract TemplateSource getTemplateSource();
 50   
 51  0 public boolean getHasTemplate()
 52    {
 53  0 Inspector inspector;
 54   
 55  0 inspector = (Inspector) getPage();
 56   
 57    // Components that inherit from BaseComponent have templates,
 58    // others do not.
 59   
 60  0 return inspector.getInspectedComponent() instanceof BaseComponent;
 61    }
 62   
 63  0 public IRender getTemplateDelegate()
 64    {
 65  0 return new IRender()
 66    {
 67  0 public void render(IMarkupWriter writer, IRequestCycle cycle)
 68    {
 69  0 writeTemplate(writer, cycle);
 70    }
 71    };
 72    }
 73   
 74    /**
 75    * Writes the HTML template for the component. When <jwc> tags are written, the id is made
 76    * a link (that selects the named component). We use some magic to accomplish this, creating
 77    * links as if we were a {@link DirectLink}component, and attributing those links to the
 78    * captive {@link DirectLink}component embedded here.
 79    */
 80   
 81  0 private void writeTemplate(IMarkupWriter writer, IRequestCycle cycle)
 82    {
 83  0 IComponent inspectedComponent = getInspectedComponent();
 84  0 ComponentTemplate template = null;
 85   
 86  0 try
 87    {
 88  0 template = getTemplateSource().getTemplate(cycle, inspectedComponent);
 89    }
 90    catch (Exception ex)
 91    {
 92  0 return;
 93    }
 94   
 95  0 writer.begin("pre");
 96   
 97  0 int count = template.getTokenCount();
 98   
 99  0 for (int i = 0; i < count; i++)
 100    {
 101  0 TemplateToken token = template.getToken(i);
 102  0 TokenType type = token.getType();
 103   
 104  0 if (type == TokenType.TEXT)
 105    {
 106  0 write(writer, (TextToken) token);
 107  0 continue;
 108    }
 109   
 110  0 if (type == TokenType.CLOSE)
 111    {
 112  0 write(writer, (CloseToken) token);
 113   
 114  0 continue;
 115    }
 116   
 117  0 if (token.getType() == TokenType.LOCALIZATION)
 118    {
 119   
 120  0 write(writer, (LocalizationToken) token);
 121  0 continue;
 122    }
 123   
 124  0 if (token.getType() == TokenType.OPEN)
 125    {
 126  0 boolean nextIsClose = (i + 1 < count)
 127    && (template.getToken(i + 1).getType() == TokenType.CLOSE);
 128   
 129  0 write(writer, nextIsClose, (OpenToken) token);
 130   
 131  0 if (nextIsClose)
 132  0 i++;
 133   
 134  0 continue;
 135    }
 136   
 137    // That's all the types known at this time.
 138    }
 139   
 140  0 writer.end(); // <pre>
 141    }
 142   
 143    /** @since 3.0 * */
 144   
 145  0 private IComponent getInspectedComponent()
 146    {
 147  0 Inspector page = (Inspector) getPage();
 148   
 149  0 return page.getInspectedComponent();
 150    }
 151   
 152    /** @since 3.0 * */
 153   
 154  0 private void write(IMarkupWriter writer, TextToken token)
 155    {
 156    // Print the section of the template ... print() will
 157    // escape and invalid characters as HTML entities. Also,
 158    // we show the full stretch of text, not the trimmed version.
 159   
 160  0 writer.print(token.getTemplateDataAsString());
 161    }
 162   
 163    /** @since 3.0 * */
 164   
 165  0 private void write(IMarkupWriter writer, CloseToken token)
 166    {
 167  0 writer.begin("span");
 168  0 writer.attribute("class", "jwc-tag");
 169   
 170  0 writer.print("</");
 171  0 writer.print(token.getTag());
 172  0 writer.print(">");
 173   
 174  0 writer.end(); // <span>
 175    }
 176   
 177    /** @since 3.0 * */
 178   
 179  0 private void write(IMarkupWriter writer, LocalizationToken token)
 180    {
 181  0 IComponent component = getInspectedComponent();
 182   
 183  0 writer.begin("span");
 184  0 writer.attribute("class", "jwc-tag");
 185   
 186  0 writer.print("<span key=\"");
 187  0 writer.print(token.getKey());
 188  0 writer.print('"');
 189   
 190  0 Map attributes = token.getAttributes();
 191  0 if (attributes != null && !attributes.isEmpty())
 192    {
 193  0 Iterator it = attributes.entrySet().iterator();
 194  0 while (it.hasNext())
 195    {
 196  0 Map.Entry entry = (Map.Entry) it.next();
 197  0 String attributeName = (String) entry.getKey();
 198  0 String attributeValue = (String) entry.getValue();
 199   
 200  0 writer.print(' ');
 201  0 writer.print(attributeName);
 202  0 writer.print("=\"");
 203  0 writer.print(attributeValue);
 204  0 writer.print('"');
 205   
 206    }
 207    }
 208   
 209  0 writer.print('>');
 210  0 writer.begin("span");
 211  0 writer.attribute("class", "localized-string");
 212   
 213  0 writer.print(component.getMessages().getMessage(token.getKey()));
 214  0 writer.end(); // <span>
 215   
 216  0 writer.print("</span>");
 217   
 218  0 writer.end(); // <span>
 219    }
 220   
 221    /** @since 3.0 * */
 222   
 223  0 private void write(IMarkupWriter writer, boolean nextIsClose, OpenToken token)
 224    {
 225  0 IComponent component = getInspectedComponent();
 226  0 IEngineService service = getPage().getEngine().getService(Tapestry.DIRECT_SERVICE);
 227   
 228    // Each id references a component embedded in the inspected component.
 229    // Get that component.
 230   
 231  0 String id = token.getId();
 232  0 IComponent embedded = component.getComponent(id);
 233  0 Object[] serviceParameters = new Object[]
 234    { embedded.getIdPath() };
 235   
 236    // Build a URL to select that component, as if by the captive
 237    // component itself (it's a Direct).
 238   
 239  0 DirectServiceParameter dsp = new DirectServiceParameter(this, serviceParameters);
 240  0 ILink link = service.getLink(getPage().getRequestCycle(), false, dsp);
 241   
 242  0 writer.begin("span");
 243  0 writer.attribute("class", "jwc-tag");
 244   
 245  0 writer.print("<");
 246  0 writer.print(token.getTag());
 247   
 248  0 writer.print(" jwcid=\"");
 249   
 250  0 writer.begin("span");
 251  0 writer.attribute("class", "jwc-id");
 252   
 253  0 writer.begin("a");
 254  0 writer.attribute("href", link.getURL());
 255  0 writer.print(id);
 256   
 257  0 writer.end(); // <a>
 258  0 writer.end(); // <span>
 259  0 writer.print('"');
 260   
 261  0 Map attributes = token.getAttributesMap();
 262   
 263  0 if (attributes != null)
 264    {
 265  0 Iterator ii = attributes.entrySet().iterator();
 266   
 267  0 while (ii.hasNext())
 268    {
 269  0 Map.Entry e = (Map.Entry) ii.next();
 270   
 271  0 String value = (String) e.getValue();
 272   
 273  0 writer.print(' ');
 274  0 writer.print(e.getKey().toString());
 275  0 writer.print("=\"");
 276  0 writer.print(value);
 277  0 writer.print('"');
 278    }
 279    }
 280   
 281    // Collapse an open & close down to a single tag.
 282   
 283  0 if (nextIsClose)
 284  0 writer.print('/');
 285   
 286  0 writer.print('>');
 287  0 writer.end(); // <span>
 288    }
 289   
 290    /**
 291    * Invoked when a component id is clicked.
 292    */
 293   
 294  0 public void trigger(IRequestCycle cycle)
 295    {
 296  0 Inspector inspector = (Inspector) getPage();
 297   
 298  0 String componentId = (String) cycle.getListenerParameters()[0];
 299  0 inspector.selectComponent(componentId);
 300   
 301  0 IComponent newComponent = inspector.getInspectedComponent();
 302   
 303    // If the component is not a BaseComponent then it won't have
 304    // a template, so switch to the specification view.
 305   
 306  0 if (!(newComponent instanceof BaseComponent))
 307  0 inspector.setView(View.SPECIFICATION);
 308    }
 309   
 310    /**
 311    * Always returns true.
 312    *
 313    * @since 2.3
 314    */
 315   
 316  0 public boolean isStateful()
 317    {
 318  0 return true;
 319    }
 320    }