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