001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import java.util.Map;
041:
042: import org.apache.commons.httpclient.Cookie;
043: import org.apache.commons.httpclient.util.DateParseException;
044: import org.apache.commons.httpclient.util.DateUtil;
045: import org.apache.commons.lang.StringUtils;
046:
047: /**
048: * Wrapper for the HTML element "meta".
049: *
050: * @version $Revision: 2132 $
051: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
052: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
053: * @author Ahmed Ashour
054: */
055: public class HtmlMeta extends HtmlElement {
056:
057: private static final long serialVersionUID = 7408601325303605790L;
058:
059: /** the HTML tag represented by this element */
060: public static final String TAG_NAME = "meta";
061:
062: /**
063: * Create an instance of HtmlMeta
064: *
065: * @param page The HtmlPage that contains this element.
066: * @param attributes the initial attributes
067: * @deprecated You should not directly construct HtmlMeta.
068: */
069: //TODO: to be removed, deprecated after 1.11
070: public HtmlMeta(final HtmlPage page, final Map attributes) {
071: this (null, TAG_NAME, page, attributes);
072: }
073:
074: /**
075: * Create an instance of HtmlMeta
076: *
077: * @param namespaceURI the URI that identifies an XML namespace.
078: * @param qualifiedName The qualified name of the element type to instantiate
079: * @param page The HtmlPage that contains this element.
080: * @param attributes the initial attributes
081: */
082: HtmlMeta(final String namespaceURI, final String qualifiedName,
083: final HtmlPage page, final Map attributes) {
084: super (namespaceURI, qualifiedName, page, attributes);
085:
086: if ("set-cookie".equalsIgnoreCase(getHttpEquivAttribute())) {
087: performSetCookie();
088: }
089: }
090:
091: /**
092: * Handles the cookies specified in meta tags
093: * like <meta http-equiv='set-cookie' content='webm=none; path=/;'>
094: */
095: protected void performSetCookie() {
096: final String[] parts = getContentAttribute().split("\\s*;\\s*");
097: final String name = StringUtils.substringBefore(parts[0], "=");
098: final String value = StringUtils.substringAfter(parts[0], "=");
099: final Cookie cookie = new Cookie(getPage().getWebResponse()
100: .getUrl().getHost(), name, value);
101: for (int i = 1; i < parts.length; i++) {
102: final String partName = StringUtils.substringBefore(
103: parts[i], "=").trim().toLowerCase();
104: final String partValue = StringUtils.substringAfter(
105: parts[i], "=").trim();
106: if ("path".equals(partName)) {
107: cookie.setPath(partValue);
108: } else if ("expires".equals(partName)) {
109: try {
110: cookie.setExpiryDate(DateUtil.parseDate(partValue));
111: } catch (final DateParseException e) {
112: notifyIncorrectness("set-cookie http-equiv meta tag: can't parse expiration date >"
113: + partValue + "<.");
114: }
115: } else {
116: notifyIncorrectness("set-cookie http-equiv meta tag: unknown attribute >"
117: + partName + "<");
118: }
119: getPage().getWebClient().getWebConnection().getState()
120: .addCookie(cookie);
121: }
122: }
123:
124: /**
125: * Return the value of the attribute "lang". Refer to the
126: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
127: * documentation for details on the use of this attribute.
128: *
129: * @return The value of the attribute "lang"
130: * or an empty string if that attribute isn't defined.
131: */
132: public final String getLangAttribute() {
133: return getAttributeValue("lang");
134: }
135:
136: /**
137: * Return the value of the attribute "xml:lang". Refer to the
138: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
139: * documentation for details on the use of this attribute.
140: *
141: * @return The value of the attribute "xml:lang"
142: * or an empty string if that attribute isn't defined.
143: */
144: public final String getXmlLangAttribute() {
145: return getAttributeValue("xml:lang");
146: }
147:
148: /**
149: * Return the value of the attribute "dir". Refer to the
150: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
151: * documentation for details on the use of this attribute.
152: *
153: * @return The value of the attribute "dir"
154: * or an empty string if that attribute isn't defined.
155: */
156: public final String getTextDirectionAttribute() {
157: return getAttributeValue("dir");
158: }
159:
160: /**
161: * Return the value of the attribute "http-equiv". Refer to the
162: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
163: * documentation for details on the use of this attribute.
164: *
165: * @return The value of the attribute "http-equiv"
166: * or an empty string if that attribute isn't defined.
167: */
168: public final String getHttpEquivAttribute() {
169: return getAttributeValue("http-equiv");
170: }
171:
172: /**
173: * Return the value of the attribute "name". Refer to the
174: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
175: * documentation for details on the use of this attribute.
176: *
177: * @return The value of the attribute "name"
178: * or an empty string if that attribute isn't defined.
179: */
180: public final String getNameAttribute() {
181: return getAttributeValue("name");
182: }
183:
184: /**
185: * Return the value of the attribute "content". Refer to the
186: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
187: * documentation for details on the use of this attribute.
188: *
189: * @return The value of the attribute "content"
190: * or an empty string if that attribute isn't defined.
191: */
192: public final String getContentAttribute() {
193: return getAttributeValue("content");
194: }
195:
196: /**
197: * Return the value of the attribute "scheme". Refer to the
198: * <a href='http://www.w3.org/TR/html401/'>HTML 4.01</a>
199: * documentation for details on the use of this attribute.
200: *
201: * @return The value of the attribute "scheme"
202: * or an empty string if that attribute isn't defined.
203: */
204: public final String getSchemeAttribute() {
205: return getAttributeValue("scheme");
206: }
207:
208: }
|