1/*
2 * Copyright (C) 2010 Google Inc.
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
17package com.google.doclava;
18
19import com.google.clearsilver.jsilver.data.Data;
20
21import java.util.regex.Pattern;
22import java.util.regex.Matcher;
23
24public class AttrTagInfo extends TagInfo {
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+)(.*)", Pattern.DOTALL);
30
31  private ContainerInfo mBase;
32  private String mCommand;
33
34  // if mCommand == "ref"
35  private FieldInfo mRefField;
36  private AttributeInfo mAttrInfo;
37
38  // if mCommand == "name"
39  private String mAttrName;
40
41  // if mCommand == "description"
42  private Comment mDescrComment;
43
44  AttrTagInfo(String name, String kind, String text, ContainerInfo base, SourcePositionInfo position) {
45    super(name, kind, text, position);
46    mBase = base;
47
48    parse(text, base, position);
49  }
50
51  void parse(String text, ContainerInfo base, SourcePositionInfo position) {
52    Matcher m;
53
54    m = TEXT.matcher(text);
55    if (!m.matches()) {
56      Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr tag: " + text);
57      return;
58    }
59
60    String command = m.group(1);
61    String more = m.group(2);
62
63    if (REF_COMMAND.equals(command)) {
64      String ref = more.trim();
65      LinkReference linkRef = LinkReference.parse(ref, mBase, position, false);
66      if (!linkRef.good) {
67        Errors.error(Errors.BAD_ATTR_TAG, position, "Unresolved @attr ref: " + ref);
68        return;
69      }
70      if (!(linkRef.memberInfo instanceof FieldInfo)) {
71        Errors.error(Errors.BAD_ATTR_TAG, position, "@attr must be a field: " + ref);
72        return;
73      }
74      mCommand = command;
75      mRefField = (FieldInfo) linkRef.memberInfo;
76    } else if (NAME_COMMAND.equals(command)) {
77      m = NAME_TEXT.matcher(more);
78      if (!m.matches() || m.group(2).trim().length() != 0) {
79        Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr name tag: " + more);
80        return;
81      }
82      mCommand = command;
83      mAttrName = m.group(1);
84    } else if (DESCRIPTION_COMMAND.equals(command)) {
85      mCommand = command;
86      mDescrComment = new Comment(more, base, position);
87    } else {
88      Errors.error(Errors.BAD_ATTR_TAG, position, "Bad @attr command: " + command);
89    }
90  }
91
92  public FieldInfo reference() {
93    return REF_COMMAND.equals(mCommand) ? mRefField : null;
94  }
95
96  @Override
97  public String name() {
98    return NAME_COMMAND.equals(mCommand) ? mAttrName : null;
99  }
100
101  public Comment description() {
102    return DESCRIPTION_COMMAND.equals(mCommand) ? mDescrComment : null;
103  }
104
105  @Override
106  public void makeHDF(Data data, String base) {
107    super.makeHDF(data, base);
108  }
109
110  public void setAttribute(AttributeInfo info) {
111    mAttrInfo = info;
112  }
113
114  public static void makeReferenceHDF(Data data, String base, AttrTagInfo[] tags) {
115    int i = 0;
116    for (AttrTagInfo t : tags) {
117      if (REF_COMMAND.equals(t.mCommand)) {
118        if (t.mAttrInfo == null) {
119          String msg = "ERROR: unlinked attr: " + t.mRefField.name();
120          if (false) {
121            System.out.println(msg);
122          } else {
123            throw new RuntimeException(msg);
124          }
125        } else {
126          data.setValue(base + "." + i + ".name", t.mAttrInfo.name());
127          data.setValue(base + "." + i + ".href", t.mAttrInfo.htmlPage());
128          i++;
129        }
130      }
131    }
132  }
133
134}
135