• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.util.regex.Pattern;
18 import java.util.regex.Matcher;
19 import org.clearsilver.HDF;
20 import org.clearsilver.CS;
21 
22 
23 public class AttrTagInfo extends TagInfo
24 {
25     private static final String REF_COMMAND = "ref";
26     private static final String NAME_COMMAND = "name";
27     private static final String DESCRIPTION_COMMAND = "description";
28     private static final Pattern TEXT = Pattern.compile("(\\S+)\\s*(.*)", Pattern.DOTALL);
29     private static final Pattern NAME_TEXT = Pattern.compile("(\\S+)(.*)",
30                                                 Pattern.DOTALL);
31 
32     private ContainerInfo mBase;
33     private String mCommand;
34 
35     // if mCommand == "ref"
36     private FieldInfo mRefField;
37     private AttributeInfo mAttrInfo;
38 
39     // if mCommand == "name"
40     private String mAttrName;
41 
42     // if mCommand == "description"
43     private Comment mDescrComment;
44 
AttrTagInfo(String name, String kind, String text, ContainerInfo base, SourcePositionInfo position)45     AttrTagInfo(String name, String kind, String text, ContainerInfo base,
46             SourcePositionInfo position)
47     {
48         super(name, kind, text, position);
49         mBase = base;
50 
51         parse(text, base, position);
52     }
53 
parse(String text, ContainerInfo base, SourcePositionInfo position)54     void parse(String text, ContainerInfo base, SourcePositionInfo position) {
55         Matcher m;
56 
57         m = TEXT.matcher(text);
58         if (!m.matches()) {
59             Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr tag: " + text);
60             return;
61         }
62 
63         String command = m.group(1);
64         String more = m.group(2);
65 
66         if (REF_COMMAND.equals(command)) {
67             String ref = more.trim();
68             LinkReference linkRef = LinkReference.parse(ref, mBase, position, false);
69             if (!linkRef.good) {
70                 Errors.error(Errors.BAD_ATTR_TAG, position, "Unresolved @attr ref: " + ref);
71                 return;
72             }
73             if (!(linkRef.memberInfo instanceof FieldInfo)) {
74                 Errors.error(Errors.BAD_ATTR_TAG, position, "@attr must be a field: " + ref);
75                 return;
76             }
77             mCommand = command;
78             mRefField = (FieldInfo)linkRef.memberInfo;
79         }
80         else if (NAME_COMMAND.equals(command)) {
81             m = NAME_TEXT.matcher(more);
82             if (!m.matches() || m.group(2).trim().length() != 0) {
83                 Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr name tag: " + more);
84                 return;
85             }
86             mCommand = command;
87             mAttrName = m.group(1);
88         }
89         else if (DESCRIPTION_COMMAND.equals(command)) {
90             mCommand = command;
91             mDescrComment = new Comment(more, base, position);
92         }
93         else {
94             Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr command: " + command);
95         }
96     }
97 
reference()98     public FieldInfo reference() {
99         return REF_COMMAND.equals(mCommand) ? mRefField : null;
100     }
101 
102     @Override
name()103     public String name() {
104         return NAME_COMMAND.equals(mCommand) ? mAttrName : null;
105     }
106 
description()107     public Comment description() {
108         return DESCRIPTION_COMMAND.equals(mCommand) ? mDescrComment : null;
109     }
110 
111     @Override
makeHDF(HDF data, String base)112     public void makeHDF(HDF data, String base)
113     {
114         super.makeHDF(data, base);
115     }
116 
setAttribute(AttributeInfo info)117     public void setAttribute(AttributeInfo info) {
118         mAttrInfo = info;
119     }
120 
makeReferenceHDF(HDF data, String base, AttrTagInfo[] tags)121     public static void makeReferenceHDF(HDF data, String base, AttrTagInfo[] tags)
122     {
123         int i=0;
124         for (AttrTagInfo t: tags) {
125             if (REF_COMMAND.equals(t.mCommand)) {
126                 if (t.mAttrInfo == null) {
127                     String msg = "ERROR: unlinked attr: " + t.mRefField.name();
128                     if (false) {
129                         System.out.println(msg);
130                     } else {
131                         throw new RuntimeException(msg);
132                     }
133                 } else {
134                     data.setValue(base + "." + i + ".name", t.mAttrInfo.name());
135                     data.setValue(base + "." + i + ".href", t.mAttrInfo.htmlPage());
136                     i++;
137                 }
138             }
139         }
140     }
141 
142 }
143