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

COVERAGE SUMMARY FOR SOURCE FILE [UriFetcher.java]

nameclass, %method, %block, %line, %
UriFetcher.java33%  (1/3)29%  (2/7)34%  (35/102)35%  (6/17)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class UriFetcher$ChainingUriFetcher0%   (0/1)0%   (0/2)0%   (0/8)0%   (0/2)
UriFetcher$ChainingUriFetcher (): void 0%   (0/1)0%   (0/3)0%   (0/1)
make (UriFetcher []): UriFetcher 0%   (0/1)0%   (0/5)0%   (0/1)
     
class UriFetcher$ChainingUriFetcher$10%   (0/1)0%   (0/2)0%   (0/45)0%   (0/7)
UriFetcher$ChainingUriFetcher$1 (UriFetcher []): void 0%   (0/1)0%   (0/9)0%   (0/2)
fetch (ExternalReference, String): FetchedData 0%   (0/1)0%   (0/36)0%   (0/5)
     
class UriFetcher$UriFetchException100% (1/1)67%  (2/3)71%  (35/49)75%  (6/8)
UriFetcher$UriFetchException (ExternalReference, String): void 100% (1/1)100% (7/7)100% (2/2)
UriFetcher$UriFetchException (ExternalReference, String, List): void 0%   (0/1)0%   (0/14)0%   (0/2)
UriFetcher$UriFetchException (ExternalReference, String, Throwable): void 100% (1/1)100% (28/28)100% (4/4)

1// Copyright (C) 2007 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;
16 
17import java.util.List;
18import com.google.caja.CajaException;
19import com.google.caja.lexer.ExternalReference;
20import com.google.caja.lexer.FetchedData;
21import com.google.caja.reporting.Message;
22import com.google.caja.reporting.MessagePart;
23import com.google.caja.util.Lists;
24 
25/**
26 * Specifies how the cajoler resolves external resources such as scripts and
27 * stylesheets in the code being cajoled.
28 *
29 * @author mikesamuel@gmail.com
30 */
31public interface UriFetcher {
32 
33  /**
34   * Loads an external resource such as the {@code src} of a {@code script}
35   * tag or a stylesheet.
36   *
37   * @return non-null resource
38   * @throws UriFetchException if the resource could not be loaded
39   */
40  FetchedData fetch(ExternalReference ref, String mimeType)
41      throws UriFetchException;
42 
43  /** A fetcher that will not load any URI. */
44  public static final UriFetcher NULL_NETWORK = new DataUriFetcher();
45 
46  public static class UriFetchException extends CajaException {
47    public final ExternalReference ref;
48    public final String expectedMimeType;
49 
50    public UriFetchException(
51        ExternalReference ref, String mimeType, Throwable cause) {
52      super(
53          new Message(
54              PluginMessageType.FAILED_TO_LOAD_EXTERNAL_URL,
55              ref.getReferencePosition(),
56              MessagePart.Factory.valueOf(ref.getUri().toString())),
57          cause);
58      this.ref = ref;
59      this.expectedMimeType = mimeType;
60    }
61 
62    public UriFetchException(
63        ExternalReference ref, String mimeType, List<Throwable> causes) {
64      this(ref, mimeType, causes.isEmpty() ? null : causes.get(0));
65    }
66 
67    public UriFetchException(ExternalReference ref, String mimeType) {
68      this(ref, mimeType, (Throwable) null);
69    }
70  }
71 
72  /**
73   * Chains one of more uri fetchers in order and returns the first
74   * successfully loaded resource
75   */
76  public final static class ChainingUriFetcher {
77    public static UriFetcher make(final UriFetcher... fetchers) {
78      return new UriFetcher() {
79        List<Throwable> causes = Lists.newArrayList();
80        public FetchedData fetch(ExternalReference ref, String mimeType)
81            throws UriFetchException {
82          for (UriFetcher fetcher : fetchers) {
83            try {
84              return fetcher.fetch(ref, mimeType);
85            } catch (UriFetchException e) {
86              causes.add(e);
87            }
88          }
89          // None of the fetchers succeeded
90          throw new UriFetchException(ref, mimeType, causes);
91        }
92      };
93    }
94  }
95 
96}

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