1 | // Copyright (C) 2009 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 | |
15 | package com.google.caja.ancillary.servlet; |
16 | |
17 | import com.google.caja.parser.css.CssTree; |
18 | import com.google.caja.parser.js.Block; |
19 | import com.google.caja.parser.js.ObjectConstructor; |
20 | import com.google.caja.util.ContentType; |
21 | |
22 | import java.net.URI; |
23 | |
24 | import org.w3c.dom.Attr; |
25 | import org.w3c.dom.DocumentFragment; |
26 | import org.w3c.dom.Element; |
27 | import org.w3c.dom.Node; |
28 | |
29 | /** |
30 | * A parse tree with meta information. |
31 | * |
32 | * @author mikesamuel@gmail.com |
33 | */ |
34 | final class Job { |
35 | /** The type of the root. */ |
36 | final ContentType t; |
37 | /** A parse tree node or other content. */ |
38 | final Object root; |
39 | /** |
40 | * The place in the input HTML from where the job was extracted. |
41 | * For JS this might be a {@code script} element or {@code onclick} attribute. |
42 | */ |
43 | final Node origin; |
44 | /** Base URI for the job */ |
45 | final URI baseUri; |
46 | |
47 | private Job(ContentType t, Object root, Node origin, URI baseUri) { |
48 | this.t = t; |
49 | this.root = root; |
50 | this.origin = origin; |
51 | this.baseUri = baseUri; |
52 | } |
53 | |
54 | static Job js(Block root, Node origin, URI baseUri) { |
55 | return new Job(ContentType.JS, root, origin, baseUri); |
56 | } |
57 | |
58 | static Job json(ObjectConstructor root, URI baseUri) { |
59 | return new Job(ContentType.JSON, root, null, baseUri); |
60 | } |
61 | |
62 | static Job css(CssTree.StyleSheet css, Element origin, URI baseUri) { |
63 | return new Job(ContentType.CSS, css, origin, baseUri); |
64 | } |
65 | |
66 | static Job css(CssTree.DeclarationGroup css, Attr origin, URI baseUri) { |
67 | return new Job(ContentType.CSS, css, origin, baseUri); |
68 | } |
69 | |
70 | static Job html(DocumentFragment fragment, URI baseUri) { |
71 | return new Job(ContentType.HTML, fragment, null, baseUri); |
72 | } |
73 | |
74 | static Job zip(byte[] zipBody) { |
75 | return new Job(ContentType.ZIP, zipBody, null, null); |
76 | } |
77 | |
78 | @Override |
79 | public String toString() { return "[Job " + t + "]"; } |
80 | } |