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.parser.html; |
16 | |
17 | import com.google.caja.lexer.HtmlTokenType; |
18 | import com.google.caja.lexer.Token; |
19 | import com.google.caja.reporting.MessagePart; |
20 | import com.google.caja.reporting.MessageQueue; |
21 | import com.google.caja.reporting.MessageType; |
22 | |
23 | import org.w3c.dom.Attr; |
24 | import org.w3c.dom.Document; |
25 | |
26 | final class AttrStub { |
27 | final Token<HtmlTokenType> nameTok; |
28 | final Token<HtmlTokenType> valueTok; |
29 | final String value; |
30 | |
31 | AttrStub(Token<HtmlTokenType> nameTok, |
32 | Token<HtmlTokenType> valueTok, String value) { |
33 | this.nameTok = nameTok; |
34 | this.valueTok = valueTok; |
35 | this.value = value; |
36 | } |
37 | |
38 | Namespaces toNamespace(Namespaces ns, MessageQueue mq) { |
39 | String rawName = nameTok.text; |
40 | if (rawName.startsWith("xmlns")) { |
41 | if (rawName.length() == 5) { |
42 | return new Namespaces(ns, "", value); |
43 | } else if (':' == rawName.charAt(5)) { |
44 | String prefix = rawName.substring(6); |
45 | if ("".equals(prefix) || "xml".equals(prefix) |
46 | || "xmlns".equals(prefix)) { |
47 | mq.addMessage( |
48 | MessageType.ILLEGAL_NAMESPACE_NAME, nameTok.pos, |
49 | MessagePart.Factory.valueOf(prefix)); |
50 | } else { |
51 | return new Namespaces(ns, prefix, value); |
52 | } |
53 | } |
54 | } |
55 | return null; |
56 | } |
57 | |
58 | Attr toAttr(Document doc, String attrUri, String attrQName) { |
59 | Attr attrNode = doc.createAttributeNS(attrUri, attrQName); |
60 | attrNode.setValue(value); |
61 | Nodes.setFilePositionFor(attrNode, nameTok.pos); |
62 | Nodes.setFilePositionForValue(attrNode, valueTok.pos); |
63 | Nodes.setRawValue(attrNode, valueTok.text); |
64 | return attrNode; |
65 | } |
66 | } |