001/* Attribute containing information and methods for properties for web content,
002 * for example, an HTML attribute that is part of an HTML element, as in the
003 * "href" attribute of <a href="http://www.w3schools.com">This is a link</a>
004
005 Copyright (c) 2011-2014 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026 PT_COPYRIGHT_VERSION_2
027 COPYRIGHTENDKEY
028
029 */
030
031package ptolemy.vergil.basic.export.web;
032
033import ptolemy.kernel.util.IllegalActionException;
034import ptolemy.kernel.util.NameDuplicationException;
035import ptolemy.kernel.util.NamedObj;
036import ptolemy.kernel.util.StringAttribute;
037
038///////////////////////////////////////////////////////////////////
039//// WebAttribute
040/**
041 * Class containing information and methods for properties for web content,
042 * for example, an HTML attribute that is part of an HTML element, as in the
043 * "href" attribute of &lt;a href="http://www.w3schools.com"&gt;This is a link&lt;/a&gt;
044 *
045 * The _elementName is used as the name of the attribute, e.g. "href" in
046 * &lt;a href="http://www.w3schools.com"&gt;This is a link&lt;/a&gt;.  An object is not
047 * allowed to have two attributes with the same _elementName, since it is
048 * assumed that these attributes will belong to the same web element.  For
049 * example, an HTML tag with two href elements should not be allowed, since
050 * it is unclear what behavior occurs (first takes precedence?  second?  both?)
051 * &lt;a href="http://site1.com" href="http://site2.com"&gt;This is a link&lt;/a&gt;
052 *
053 * @author Elizabeth Latronico
054 * @version $Id$
055 * @since Ptolemy II 10.0
056 * @Pt.ProposedRating Red (ltrnc)
057 * @Pt.AcceptedRating Red (ltrnc)
058 */
059
060public class WebAttribute extends StringAttribute {
061
062    /** Create an instance of this parameter.
063     *  @param container The container.
064     *  @param name The name.
065     *  @exception IllegalActionException If the superclass throws it.
066     *  @exception NameDuplicationException If the superclass throws it.
067     */
068    public WebAttribute(NamedObj container, String name)
069            throws IllegalActionException, NameDuplicationException {
070        super(container, name);
071        setWebName("");
072    }
073
074    ///////////////////////////////////////////////////////////////////
075    ////                         public methods                    ////
076
077    /** Factory method for creating WebAttributes that appends to the
078     *  attribute if it already exists and does not have the specified
079     *  content.  Return the modified or new WebAttribute.
080     *  Set persistent to false.  This is static so that any
081     *  WebExportable may call it.
082     *
083     * @param container  The container object for the WebAttribute
084     * @param id The Ptolemy name for the WebAttribute (needed to ensure a
085     * unique Ptolemy name)
086     * @param webName The web name of this WebAttribute.
087     * @param content The value of this WebAttribute.
088     * @return The WebAttribute that was created (or that previously existed)
089     * with persistent set to false
090     * @exception IllegalActionException if the WebAttribute cannot be created
091     * (perhaps another Attribute exists with the requested name)
092     */
093    public static WebAttribute appendToWebAttribute(NamedObj container,
094            String id, String webName, String content)
095            throws IllegalActionException {
096        WebAttribute webAttribute = createWebAttribute(container, id, webName);
097
098        String previousValue = webAttribute.getExpression();
099        if (previousValue == null || previousValue.trim().length() == 0) {
100            // No previous value.
101            webAttribute.setExpression(content);
102            return webAttribute;
103        }
104
105        // Assume values are space-separated, as they are with the class attribute.
106        String[] previousValues = previousValue.split(" ");
107        for (String value : previousValues) {
108            if (value.equals(content)) {
109                // Already present.
110                return webAttribute;
111            }
112        }
113        // Append to the previous value.
114        webAttribute.setExpression(previousValue + " " + content);
115
116        return webAttribute;
117    }
118
119    /** Return the web name of this element; for example, "myElement" in
120     * &lt;div name="myElement"&gt; &lt;/div&gt; in HTML.
121     *
122     * @return The web name of this element; for example, "myElement" in
123     * &lt;div name="myElement"&gt; &lt;/div&gt; in HTML.
124     * @see #setWebName(String)
125     */
126    public String getWebName() {
127        return _webName;
128    }
129
130    /** Set the web name of this element; for example, "myElement" in
131     * &lt;div name="myElement"&gt; &lt;/div&gt; in HTML.
132     *
133     * @param webName The web name of this element; for example, "myElement" in
134     * &lt;div name="myElement"&gt; &lt;/div&gt; in HTML.
135     * @see #getWebName()
136     */
137    public void setWebName(String webName) {
138        _webName = webName;
139    }
140
141    /** Factory method for creating WebAttributes.  Checks if there is an
142     * existing WebAttribute with the same name.  If so, return it; if not
143     * create a new one.  Sets persistent to false.  Static so that any
144     * WebExportable may call it.
145     *
146     * @param container  The container object for the WebAttribute
147     * @param id The Ptolemy name for the WebAttribute (needed to ensure a
148     * unique Ptolemy name)
149     * @param webName The web name of this WebAttribute
150     * @return The WebAttribute that was created (or that previously existed)
151     * with persistent set to false
152     * @exception IllegalActionException if the WebAttribute cannot be created
153     * (perhaps another Attribute exists with the requested name)
154     */
155    public static WebAttribute createWebAttribute(NamedObj container, String id,
156            String webName) throws IllegalActionException {
157        WebAttribute webAttribute;
158
159        try {
160            if (id != null && container.getAttribute(id) == null) {
161                webAttribute = new WebAttribute(container, id);
162                webAttribute.setPersistent(false);
163            }
164        } catch (NameDuplicationException e) {
165            throw new IllegalActionException(container,
166                    "Cannot create web content.  Duplicate name for "
167                            + "WebAttribute: " + id);
168        }
169
170        webAttribute = (WebAttribute) container.getAttribute(id,
171                WebAttribute.class);
172        webAttribute.setWebName(webName);
173        webAttribute.setPersistent(false);
174        webAttribute.setVisibility(NONE);
175        return webAttribute;
176    }
177
178    /** The desired name of this attribute in the web file, for example,
179     * "href" in &lt;a href="http://ptolemy.org"&gt;&lt;/div&gt;.
180     */
181    private String _webName;
182}