EMMA Coverage Report (generated Mon Nov 01 16:48:29 PDT 2010)
[all classes][com.google.caja.plugin.stages]

COVERAGE SUMMARY FOR SOURCE FILE [DebuggingSymbols.java]

nameclass, %method, %block, %line, %
DebuggingSymbols.java100% (1/1)100% (7/7)97%  (173/179)94%  (34/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DebuggingSymbols100% (1/1)100% (7/7)97%  (173/179)94%  (34/36)
DebuggingSymbols (): void 100% (1/1)100% (8/8)100% (2/2)
allInputSources (): Set 100% (1/1)100% (24/24)100% (4/4)
commonPrefixLength (String, String): int 100% (1/1)100% (22/22)100% (5/5)
formatPos (FilePosition, MessageContext): String 100% (1/1)67%  (12/18)67%  (4/6)
indexForPosition (FilePosition): int 100% (1/1)100% (22/22)100% (4/4)
isEmpty (): boolean 100% (1/1)100% (4/4)100% (1/1)
toJavascriptSideTable (): ParseTreeNodeContainer 100% (1/1)100% (81/81)100% (14/14)

1// Copyright (C) 2008 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package com.google.caja.plugin.stages;
16 
17import com.google.caja.SomethingWidgyHappenedError;
18import com.google.caja.lexer.FilePosition;
19import com.google.caja.lexer.InputSource;
20import com.google.caja.parser.ParseTreeNodeContainer;
21import com.google.caja.parser.js.Expression;
22import com.google.caja.parser.js.IntegerLiteral;
23import com.google.caja.parser.js.StringLiteral;
24import com.google.caja.reporting.MessageContext;
25 
26import java.io.IOException;
27import java.util.ArrayList;
28import java.util.HashSet;
29import java.util.LinkedHashMap;
30import java.util.List;
31import java.util.Map;
32import java.util.Set;
33 
34/**
35 * A module-specific collection of file positions of key pieces of cajoled code
36 * that can be bundled with a cajoled module.
37 *
38 * @author mikesamuel@gmail.com
39 */
40final class DebuggingSymbols {
41  private Map<FilePosition, Integer> positions
42      = new LinkedHashMap<FilePosition, Integer>();
43 
44  /** Returns an index into the debugging symbols table. */
45  public int indexForPosition(FilePosition pos) {
46    Integer index = positions.get(pos);
47    if (index == null) {
48      positions.put(pos, index = positions.size());
49    }
50    return index;
51  }
52 
53  /**
54   * Produces a set of actuals that can be consumed by
55   * {@code ___.useDebugSymbols} from cajita-debugmode.js.
56   */
57  public ParseTreeNodeContainer toJavascriptSideTable() {
58    MessageContext mc = new MessageContext();
59    for (InputSource is : allInputSources()) { mc.addInputSource(is); }
60    List<Expression> debugTable = new ArrayList<Expression>(
61        positions.size() * 2  - 1);
62    String last = null;
63    for (FilePosition p : positions.keySet()) {
64      String posStr = formatPos(p, mc);
65      int prefixLen = 0;
66      if (last != null) {
67        prefixLen = commonPrefixLength(posStr, last);
68        debugTable.add(new IntegerLiteral(FilePosition.UNKNOWN, prefixLen));
69      }
70      debugTable.add(StringLiteral.valueOf(
71          FilePosition.UNKNOWN, posStr.substring(prefixLen)));
72      last = posStr;
73    }
74    return new ParseTreeNodeContainer(debugTable);
75  }
76 
77  public boolean isEmpty() { return positions.isEmpty(); }
78 
79  private Set<InputSource> allInputSources() {
80    Set<InputSource> sources = new HashSet<InputSource>();
81    for (FilePosition p : positions.keySet()) {
82      sources.add(p.source());
83    }
84    return sources;
85  }
86 
87  /** Length of the longest string that is a prefix of both. */
88  private static int commonPrefixLength(String a, String b) {
89    int n = Math.min(a.length(), b.length());
90    int prefixLen = 0;
91    while (prefixLen < n && a.charAt(prefixLen) == b.charAt(prefixLen)) {
92      ++prefixLen;
93    }
94    return prefixLen;
95  }
96 
97  private static String formatPos(FilePosition pos, MessageContext mc) {
98    StringBuilder sb = new StringBuilder();
99    try {
100      pos.format(mc, sb);
101    } catch (IOException ex) {
102      throw new SomethingWidgyHappenedError(ex);
103    }
104    return sb.toString();
105  }
106}

[all classes][com.google.caja.plugin.stages]
EMMA 2.0.5312 (C) Vladimir Roubtsov