    001    // Copyright 2008, 2009, 2010, 2011 The Apache Software Foundation
    002    //
    003    // Licensed under the Apache License, Version 2.0 (the "License");
    004    // you may not use this file except in compliance with the License.
    005    // You may obtain a copy of the License at
    006    //
    007    // http://www.apache.org/licenses/LICENSE-2.0
    008    //
    009    // Unless required by applicable law or agreed to in writing, software
    010    // distributed under the License is distributed on an "AS IS" BASIS,
    011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012    // See the License for the specific language governing permissions and
    013    // limitations under the License.
    014
    015    package org.apache.tapestry5.services.javascript;
    016
    017    import org.apache.tapestry5.Asset;
    018    import org.apache.tapestry5.dom.Document;
    019    import org.apache.tapestry5.dom.Element;
    020    import org.apache.tapestry5.internal.TapestryInternalUtils;
    021    import org.apache.tapestry5.internal.services.DocumentLinker;
    022    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    023
    024    /**
    025     * Captures the information needed to create a stylesheet link in the final {@link Document}, or
    026     * as part of a JSON partial page render response.
    027     *
    028     * @see DocumentLinker
    029     * @see JavaScriptStack
    030     * @since 5.2.0
    031     */
    032    public final class StylesheetLink
    033    {
    034        private final String url;
    035
    036        private final StylesheetOptions options;
    037
    038        private static final StylesheetOptions BLANK_OPTIONS = new StylesheetOptions(null);
    039
    040        public StylesheetLink(Asset asset)
    041        {
    042            this(asset, null);
    043        }
    044
    045        public StylesheetLink(Asset asset, StylesheetOptions options)
    046        {
    047            this(asset.toClientURL(), options);
    048        }
    049
    050        public StylesheetLink(String url)
    051        {
    052            this(url, null);
    053        }
    054
    055        public StylesheetLink(String url, StylesheetOptions options)
    056        {
    057            assert InternalUtils.isNonBlank(url);
    058            this.url = url;
    059            this.options = options != null ? options : BLANK_OPTIONS;
    060        }
    061
    062        public String getURL()
    063        {
    064            return url;
    065        }
    066
    067        /**
    068         * Returns an instance of {@link StylesheetOptions}. Never returns null (a blank options
    069         * object is returned if null is passed to the constructor).
    070         */
    071        public StylesheetOptions getOptions()
    072        {
    073            return options;
    074        }
    075
    076        /**
    077         * Invoked to add the stylesheet link to a container element.
    078         *
    079         * @param container to add the new element to
    080         */
    081        public void add(Element container)
    082        {
    083            String condition = options.getCondition();
    084            boolean hasCondition = InternalUtils.isNonBlank(condition);
    085
    086            if (hasCondition)
    087            {
    088                container.raw(String.format("\n<!--[if %s]>\n", condition));
    089            }
    090
    091            String rel = options.ajaxInsertionPoint ? "stylesheet t-ajax-insertion-point" : "stylesheet";
    092
    093            container.element("link", "href", url, "rel", rel, "type", "text/css", "media", options.media);
    094
    095            if (hasCondition)
    096            {
    097                container.raw("\n<![endif]-->\n");
    098            }
    099        }
    100
    101        @Override
    102        public String toString()
    103        {
    104            return String.format("StylesheetLink[%s %s]", url, options);
    105        }
    106
    107        @Override
    108        public boolean equals(Object obj)
    109        {
    110            if (obj == this)
    111                return true;
    112
    113            if (obj == null || !(obj instanceof StylesheetLink))
    114                return false;
    115
    116            StylesheetLink ssl = (StylesheetLink) obj;
    117
    118            return TapestryInternalUtils.isEqual(url, ssl.url) && TapestryInternalUtils.isEqual(options, ssl.options);
    119        }
    120
    121    }