001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of HtmlCleaner may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "HtmlCleaner" in the
035: subject line.
036: */
037:
038: package org.htmlcleaner;
039:
040: import java.io.IOException;
041:
042: import org.htmlcleaner.BaseToken;
043:
044: /**
045: * <p>HTML doctype token.</p>
046: *
047: * Created by: Vladimir Nikic<br/>
048: * Date: November, 2006.
049: */
050: public class DoctypeToken implements BaseToken {
051:
052: private String part1;
053: private String part2;
054: private String part3;
055: private String part4;
056:
057: public DoctypeToken(String part1, String part2, String part3,
058: String part4) {
059: this .part1 = part1 != null ? part1.toUpperCase() : part1;
060: this .part2 = part2 != null ? part2.toUpperCase() : part2;
061: this .part3 = clean(part3);
062: this .part4 = clean(part4);
063: }
064:
065: private String clean(String s) {
066: if (s != null) {
067: s = s.replace('>', ' ');
068: s = s.replace('<', ' ');
069: s = s.replace('&', ' ');
070: s = s.replace('\'', ' ');
071: s = s.replace('\"', ' ');
072: }
073:
074: return s;
075: }
076:
077: public boolean isValid() {
078: if (part1 == null || "".equals(part1)) {
079: return false;
080: }
081:
082: if (!"public".equalsIgnoreCase(part2)
083: && !"system".equalsIgnoreCase(part2)) {
084: return false;
085: }
086:
087: if ("system".equalsIgnoreCase(part2) && part4 != null
088: && !"".equals(part4)) {
089: return false;
090: }
091:
092: if ("public".equalsIgnoreCase(part2)
093: && (part4 == null || "".equals(part4))) {
094: return false;
095: }
096:
097: return true;
098: }
099:
100: public String getContent() {
101: String result = "<!DOCTYPE " + part1 + " ";
102: result += part2 + " \"" + part3 + "\"";
103: if (part4 != null && !"".equals(part4)) {
104: result += " \"" + part4 + "\"";
105: }
106:
107: result += ">";
108:
109: return result;
110: }
111:
112: public String toString() {
113: return getContent();
114: }
115:
116: public String getName() {
117: return "";
118: }
119:
120: public void serialize(XmlSerializer xmlSerializer)
121: throws IOException {
122: xmlSerializer.getWriter().write(getContent() + "\n");
123: }
124:
125: }
|