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

http://github.com/dhanji/sitebricks · Java · 52 lines · 25 code · 14 blank · 13 comment · 3 complexity · d16fcd16ecc6a2075a5fa2ff6d4dad3b MD5 · raw file

  1. package com.google.sitebricks.compiler;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import org.jsoup.nodes.Node;
  5. /**
  6. * @author shawn
  7. */
  8. public class AnnotationParser {
  9. // TODO regex is not powerful enough to parse annotation expressions
  10. public static final Pattern WIDGET_ANNOTATION_REGEX = Pattern.compile("(@\\w+(\\([\\w,=\"'/()?:><!\\[\\];{}. \\|&+-]*\\))?[ \n\r\t]*)\\Z");
  11. public static String readAnnotation(String text) {
  12. String annotation = null;
  13. final Matcher matcher = WIDGET_ANNOTATION_REGEX.matcher(text);
  14. if (matcher.find())
  15. annotation = matcher.group().trim();
  16. return annotation;
  17. }
  18. public static String readAnnotation(Node node) {
  19. if (null == node) return null;
  20. Node preceding = node.previousSibling();
  21. //if this is a text node, then match for annotations
  22. return readAnnotation(node.outerHtml());
  23. }
  24. public static String stripAnnotation(String text) {
  25. return Dom.stripAnnotation(text); // TODO - move it here?
  26. }
  27. /**
  28. * @param annotation A string representing an unparsed annotation of the form: <pre>
  29. * "{@literal @}MyAnn(property = [expr], ...)"</pre>
  30. * @return A partially parsed array following this structure:<pre>
  31. * [0] -> "MyAnn" <br/>
  32. * [1] -> "prop = [expr], ..."
  33. * </pre>
  34. */
  35. public static String[] extractKeyAndContent(String annotation) {
  36. return Dom.extractKeyAndContent(annotation); // TODO - move it here?
  37. }
  38. }