@Override
    public void onComponentTag(final Component component, final ComponentTag tag) {
      String expr = tag.getAttributes().getString(wicketMessageAttrName);
      if (!Strings.isEmpty(expr)) {
        expr = expr.trim();

        String[] attrsAndKeys = Strings.split(expr, ',');

        for (String attrAndKey : attrsAndKeys) {
          int colon = attrAndKey.lastIndexOf(":");
          // make sure the attribute-key pair is valid
          if (attrAndKey.length() < 3 || colon < 1 || colon > attrAndKey.length() - 2) {
            throw new WicketRuntimeException(
                "wicket:message attribute contains an invalid value [["
                    + expr
                    + "]], must be of form (attr:key)+");
          }

          String attr = attrAndKey.substring(0, colon);
          String key = attrAndKey.substring(colon + 1);

          // we need to call the proper getString() method based on
          // whether or not we have a default value
          final String value;
          if (tag.getAttributes().containsKey(attr)) {
            value = component.getString(key, null, tag.getAttributes().getString(attr));
          } else {
            value = component.getString(key);
          }
          tag.put(attr, value);
        }
      }
    }
    @Override
    public void onComponentTag(Component component, ComponentTag tag) {
      super.onComponentTag(component, tag);

      QuestionCategoryComponentsView view = (QuestionCategoryComponentsView) component;

      String cssClass = GRID_CLASS_PREFIX + "-" + view.getColumns();
      if (tag.getAttributes().containsKey("class")) {
        cssClass += " " + tag.getAttributes().getString("class");
      }
      tag.getAttributes().put("class", cssClass);
    }
Esempio n. 3
0
  /**
   * Adds random noise to the url every request to prevent the browser from caching the image.
   *
   * @param tag
   */
  protected final void addAntiCacheParameter(final ComponentTag tag) {
    String url = tag.getAttributes().getString("src");
    url = url + (url.contains("?") ? "&" : "?");
    url = url + "antiCache=" + System.currentTimeMillis();

    tag.put("src", url);
  }
  /* (non-Javadoc)
   * @see net.jawr.web.wicket.AbstractJawrReference#createRenderer(net.jawr.web.resource.bundle.handler.ResourceBundlesHandler, boolean, org.apache.wicket.markup.ComponentTag)
   */
  @Override
  protected BundleRenderer createRenderer(
      ResourceBundlesHandler rsHandler, Boolean useRandomParam, ComponentTag tag) {

    final IValueMap attributes = tag.getAttributes();
    boolean defer = attributes.getBoolean(JawrConstant.DEFER_ATTR);
    return new JavascriptHTMLBundleLinkRenderer(rsHandler, useRandomParam, defer);
  }
 public static void addAttribute(ComponentTag tag, String attName, String addValue, String sep) {
   String value = tag.getAttributes().getString(attName);
   if (StringUtils.isEmpty(value)) {
     tag.put(attName, addValue);
   } else {
     tag.put(attName, value + sep + addValue);
   }
 }
Esempio n. 6
0
 @Override
 protected void onComponentTag(final ComponentTag tag) {
   super.onComponentTag(tag);
   // our component tag is not actually the input that is submitted
   if (tag.getAttributes().containsKey("name")) {
     tag.remove("name");
   }
 }
  @Override
  public void onComponentTag(Component component, ComponentTag tag) {
    super.onComponentTag(component, tag);

    IValueMap attributes = tag.getAttributes();
    attributes.put("data-header-selector", "#" + header.getMarkupId());
    attributes.put("data-footer-selector", "#" + footer.getMarkupId());
  }
Esempio n. 8
0
 /**
  * @param form
  * @param tag
  * @return
  */
 private String verifyFormName(Form<?> form, ComponentTag tag) {
   IValueMap attributes = tag.getAttributes();
   String value = attributes.getString("name");
   if (value == null) {
     value = form.getId();
     tag.put("name", value);
   }
   return value;
 }
 /**
  * Test that a null model does not append an empty attribute
  * https://issues.apache.org/jira/browse/WICKET-3884
  */
 @Test
 public void nullModelDoesNotAppendEmptyAttribute() {
   AttributeModifier appender = AttributeModifier.append("class", null);
   XmlTag xmlTag = new XmlTag();
   ComponentTag tag = new ComponentTag(xmlTag);
   appender.replaceAttributeValue(null, tag);
   Map<String, Object> attributes = tag.getAttributes();
   assertTrue(attributes.isEmpty());
 }
Esempio n. 10
0
 /**
  * Tests {@link AttributeModifier#remove(String)}
  *
  * <p>https://issues.apache.org/jira/browse/WICKET-3934
  */
 @Test
 public void removeAttribute() {
   AttributeModifier appender = AttributeModifier.remove("class");
   XmlTag xmlTag = new XmlTag();
   ComponentTag tag = new ComponentTag(xmlTag);
   Map<String, Object> attributes = tag.getAttributes();
   attributes.put("class", "someValue");
   appender.replaceAttributeValue(null, tag);
   assertTrue(attributes.isEmpty());
 }
Esempio n. 11
0
 /** Test that a null model does not throw null pointers. */
 @Test
 public void nullModelDoesNotThrowNullPointerExceptions() {
   AttributeModifier modifier = new AttributeModifier("test", null);
   XmlTag xmlTag = new XmlTag();
   ComponentTag tag = new ComponentTag(xmlTag);
   tag.setId("foo");
   tag.setName("test");
   modifier.replaceAttributeValue(null, tag);
   Map<String, Object> attributes = tag.getAttributes();
   assertTrue(attributes.isEmpty());
 }
Esempio n. 12
0
 /**
  * Add an attribute with name equal (Object#equals()) to the special {@link
  * AttributeModifier#VALUELESS_ATTRIBUTE_REMOVE} but not identity equal
  *
  * <p>https://issues.apache.org/jira/browse/WICKET-3934
  */
 @Test
 public void appendSpecialAttribute() {
   String attrName = "attrName";
   AttributeModifier appender = AttributeModifier.append(attrName, "VA_REMOVE");
   XmlTag xmlTag = new XmlTag();
   ComponentTag tag = new ComponentTag(xmlTag);
   Map<String, Object> attributes = tag.getAttributes();
   attributes.put(attrName, "VA_REMOVE");
   appender.replaceAttributeValue(null, tag);
   assertFalse(attributes.isEmpty());
   assertEquals("VA_REMOVE VA_REMOVE", attributes.get(attrName));
 }
Esempio n. 13
0
 /** Test simple model replacement. */
 @Test
 public void testModelReplacement() {
   AttributeModifier modifier = new AttributeModifier("test", Model.of("Ellioth Smith Rocks"));
   XmlTag xmlTag = new XmlTag();
   ComponentTag tag = new ComponentTag(xmlTag);
   tag.setId("test");
   tag.setName("id");
   modifier.replaceAttributeValue(null, tag);
   Map<String, Object> attributes = tag.getAttributes();
   assertTrue(!attributes.isEmpty());
   String replacement = (String) attributes.get("test");
   assertNotNull(replacement);
   assertEquals("Ellioth Smith Rocks", replacement);
 }
 public final void replaceAttributeValue(final Component component, final ComponentTag tag) {
   if (this.isEnabled(component)) {
     final IValueMap attributes = tag.getAttributes();
     final Object replacementValue = this.getReplacementOrNull(component);
     if (AttributeModifier.VALUELESS_ATTRIBUTE_ADD == replacementValue) {
       attributes.put((Object) this.attribute, (Object) null);
     } else if (AttributeModifier.VALUELESS_ATTRIBUTE_REMOVE == replacementValue) {
       attributes.remove((Object) this.attribute);
     } else {
       final String value = this.toStringOrNull(attributes.get((Object) this.attribute));
       final String newValue = this.newValue(value, this.toStringOrNull(replacementValue));
       if (newValue != null) {
         attributes.put((Object) this.attribute, (Object) newValue);
       }
     }
   }
 }
Esempio n. 15
0
  /** Test that that the attribute modifier does nothing with not enabled. */
  @Test
  public void testNoNewValueWhenNotEnabled() {
    AttributeModifier modifier =
        new AttributeModifier("test", Model.of("Ellioth Smith Rocks")) {
          @Override
          public boolean isEnabled(Component component) {
            return false;
          }
        };

    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    tag.setId("test");
    tag.setName("id");
    Map<String, Object> attributes = tag.getAttributes();
    attributes.put("test", "My mother rocks");
    modifier.replaceAttributeValue(null, tag);
    String replacement = (String) attributes.get("test");
    assertNotNull(replacement);
    assertEquals("My mother rocks", replacement);
  }
Esempio n. 16
0
  /** Test overriding newValue (and using a null model). */
  @Test
  public void testNewValue() {
    AttributeModifier modifier =
        new AttributeModifier("test", null) {
          private static final long serialVersionUID = 1L;

          @Override
          protected String newValue(String currentValue, String replacementValue) {
            return "the replacement";
          }
        };
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    tag.setId("test");
    tag.setName("id");
    modifier.replaceAttributeValue(null, tag);
    Map<String, Object> attributes = tag.getAttributes();
    assertTrue(!attributes.isEmpty());
    String replacement = (String) attributes.get("test");
    assertNotNull(replacement);
    assertEquals("the replacement", replacement);
  }
  @Override
  protected final MarkupElement onComponentTag(ComponentTag tag) throws ParseException {
    if (tag.isClose()) {
      return tag;
    }

    final String wicketMessageAttribute = tag.getAttributes().getString(getWicketMessageAttrName());

    if ((wicketMessageAttribute != null) && (wicketMessageAttribute.trim().length() > 0)) {
      // check if this tag is raw markup
      if (tag.getId() == null) {
        // if this is a raw tag we need to set the id to something so
        // that wicket will not merge this as raw markup and instead
        // pass it on to a resolver
        tag.setId(WICKET_MESSAGE_CONTAINER_ID);
        tag.setAutoComponentTag(true);
        tag.setModified(true);
      }
      tag.addBehavior(new AttributeLocalizer(getWicketMessageAttrName()));
    }

    return tag;
  }
Esempio n. 18
0
  @Override
  protected void onComponentTag(final ComponentTag tag) {
    super.onComponentTag(tag);

    IValueMap attributes = tag.getAttributes();

    if (minimum != null) {
      attributes.put("min", Objects.stringValue(minimum));
    } else {
      attributes.remove("min");
    }

    if (maximum != null) {
      attributes.put("max", Objects.stringValue(maximum));
    } else {
      attributes.remove("max");
    }

    if (step != null) {
      attributes.put("step", Objects.stringValue(step));
    } else {
      attributes.remove("step");
    }
  }
Esempio n. 19
0
  @Override
  protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException {
    // We only need ComponentTags
    if (tag instanceof WicketTag) {
      return tag;
    }

    // Has wicket:enclosure attribute?
    String enclosureAttr = getAttribute(tag, null);
    if (enclosureAttr != null) {
      if (tag.isOpen()) {
        // Make sure 'wicket:id' and 'id' are consistent
        String htmlId = tag.getAttribute("id");
        if ((tag.getId() != null) && !Strings.isEmpty(htmlId) && !htmlId.equals(tag.getId())) {
          throw new ParseException(
              "Make sure that 'id' and 'wicket:id' are the same if both are provided. Tag:"
                  + tag.toString(),
              tag.getPos());
        }

        // if it doesn't have a wicket-id already, then assign one now.
        if (Strings.isEmpty(tag.getId())) {
          if (Strings.isEmpty(htmlId)) {
            String id =
                getWicketNamespace() + "_" + INLINE_ENCLOSURE_ID_PREFIX + getRequestUniqueId();
            tag.setId(id);
          } else {
            tag.setId(htmlId);
          }

          tag.setAutoComponentTag(true);
          tag.setAutoComponentFactory(
              new ComponentTag.IAutoComponentFactory() {
                @Override
                public Component newComponent(MarkupContainer container, ComponentTag tag) {
                  String attributeName = getInlineEnclosureAttributeName(null);
                  String childId = tag.getAttribute(attributeName);
                  return new InlineEnclosure(tag.getId(), childId);
                }
              });
          tag.setModified(true);
        }

        // Put the enclosure on the stack. The most current one will be on top
        if (enclosures == null) {
          enclosures = new ArrayDeque<>();
        }
        enclosures.push(tag);
      } else {
        throw new ParseException(
            "Open-close tags don't make sense for InlineEnclosure. Tag:" + tag.toString(),
            tag.getPos());
      }
    }
    // Are we within an enclosure?
    else if ((enclosures != null) && (enclosures.size() > 0)) {
      // In case the enclosure tag did not provide a child component id, then assign the
      // first ComponentTag's id found as the controlling child to the enclosure.
      if (tag.isOpen()
          && (tag.getId() != null)
          && !(tag instanceof WicketTag)
          && !tag.isAutoComponentTag()) {
        Iterator<ComponentTag> componentTagIterator = enclosures.descendingIterator();
        while (componentTagIterator.hasNext()) {
          ComponentTag lastEnclosure = componentTagIterator.next();
          String attr = getAttribute(lastEnclosure, null);
          if (Strings.isEmpty(attr) == true) {
            lastEnclosure.getAttributes().put(getInlineEnclosureAttributeName(null), tag.getId());
            lastEnclosure.setModified(true);
          }
        }
      } else if (tag.isClose() && tag.closes(enclosures.peek())) {
        ComponentTag lastEnclosure = enclosures.pop();
        String attr = getAttribute(lastEnclosure, null);
        if (Strings.isEmpty(attr) == true) {
          throw new ParseException(
              "Did not find any child for InlineEnclosure. Tag:" + lastEnclosure.toString(),
              tag.getPos());
        }
      }
    }

    return tag;
  }
Esempio n. 20
0
 /**
  * @param tag The ComponentTag of the markup element with wicket:enclosure attribute
  * @return The value of wicket:enclosure attribute or null if not found
  */
 private String getAttribute(final ComponentTag tag, MarkupStream markupStream) {
   return tag.getAttributes().getString(getInlineEnclosureAttributeName(markupStream));
 }