// Copyright (C) 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.caja.ancillary.jsdoc;

import com.google.caja.lexer.FilePosition;
import com.google.caja.reporting.RenderContext;
import java.util.Collections;

/**
 * A chunk of comment text.
 * @author mikesamuel@gmail.com
 */
public final class TextAnnotation extends AbstractAnnotation {
  TextAnnotation(String text, FilePosition pos) {
    super(text, Collections.<Annotation>emptyList(), pos);
    if (text == null) { throw new NullPointerException(); }
  }

  public void render(RenderContext r) {
    r.getOut().mark(getFilePosition());
    r.getOut().consume(getValue());
  }

  public TextAnnotation slice(int start, int end) {
    return slice(getValue(), getFilePosition(), start, end);
  }

  public static TextAnnotation slice(
      String value, FilePosition pos, int start, int end) {
    int sline = pos.startLineNo(), scil = pos.startCharInLine();
    // The below may give incorrect results if the CharProducer that produced
    // this annotation does unescaping, but it should be close enough to produce
    // good error messages.
    for (int i = 0; i < start; ++i) {
      char ch = value.charAt(i);
      if (ch == '\r') {
        if (i + 1 >= value.length() || '\n' != value.charAt(i + 1)) { ++sline; }
        scil = 1;
      } else if (ch == '\n') {
        ++sline;
        scil = 1;
      } else {
        ++scil;
      }
    }
    int eline = sline, ecil = scil;
    for (int i = start; i < end; ++i) {
      char ch = value.charAt(i);
      if (ch == '\r') {
        if (i + 1 >= value.length() || '\n' != value.charAt(i + 1)) { ++eline; }
        ecil = 1;
      } else if (ch == '\n') {
        ++eline;
        ecil = 1;
      } else {
        ++ecil;
      }
    }
    return new TextAnnotation(
        value.substring(start, end),
        FilePosition.instance(
            pos.source(),
            sline, pos.startCharInFile() + start, scil, end - start));
  }
}

@@ -1954,351 +1954,8 @@
   %7D%0A
-    int eline = sline, ecil = scil;%0A    for (int i = start; i %3C end; ++i) %7B%0A      char ch = value.charAt(i);%0A      if (ch == '%5Cr') %7B%0A        if (i + 1 %3E= value.length() %7C%7C '%5Cn' != value.charAt(i + 1)) %7B ++eline; %7D%0A        ecil = 1;%0A      %7D else if (ch == '%5Cn') %7B%0A        ++eline;%0A        ecil = 1;%0A      %7D else %7B%0A        ++ecil;%0A      %7D%0A    %7D%0A
     

remove useless code in TextAnnotation.java http://codereview.appspot.com/5310053