PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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