/sitebricks/src/main/java/com/google/sitebricks/compiler/AnnotationNode.java

http://github.com/dhanji/sitebricks · Java · 58 lines · 32 code · 9 blank · 17 comment · 0 complexity · 4754b245ada999a4d8be9ac57b46d26a MD5 · raw file

  1. package com.google.sitebricks.compiler;
  2. import org.jsoup.nodes.Node;
  3. import org.jsoup.nodes.TextNode;
  4. /**
  5. Based on jsoup.nodes.TextNode by Jonathan Hedley, jonathan@hedley.net
  6. AnnotationNode is for Sitebricks text annotations such as
  7. @Repeat(...) or @ShowIf(true)<div ... />
  8. */
  9. public class AnnotationNode extends TextNode {
  10. static final String ANNOTATION_KEY = "_annokey";
  11. static final String ANNOTATION_CONTENT = "_annocontent";
  12. static final String ANNOTATION = "_annotation";
  13. /**
  14. Create a new AnnotationNode representing the supplied (unencoded) text).
  15. @param annotation raw text
  16. @param baseUri base uri
  17. @see #createFromEncoded(String, String)
  18. */
  19. public AnnotationNode(String annotation, String baseUri) {
  20. super(annotation, baseUri);
  21. this.annotation(annotation);
  22. }
  23. public AnnotationNode(String annotation) {
  24. super(annotation, "");
  25. this.annotation(annotation);
  26. }
  27. public String nodeName() {
  28. return "#annotation";
  29. }
  30. /**
  31. * Set the annotation of this node.
  32. * @param annotation raw annotation
  33. * @return this, for chaining
  34. */
  35. public AnnotationNode annotation(String annotation) {
  36. this.attr(ANNOTATION, annotation);
  37. String[] kc = AnnotationParser.extractKeyAndContent(annotation);
  38. this.attr(ANNOTATION_KEY, kc[0]);
  39. this.attr(ANNOTATION_CONTENT, kc[1]);
  40. return this;
  41. }
  42. public Node apply (Node annotate) {
  43. annotate.attr(ANNOTATION, this.attr(ANNOTATION));
  44. annotate.attr(ANNOTATION_KEY, this.attr(ANNOTATION_KEY));
  45. annotate.attr(ANNOTATION_CONTENT, this.attr(ANNOTATION_CONTENT));
  46. return annotate;
  47. }
  48. }