001    // Copyright 2008, 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.internal.services;
016    
017    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
018    import org.apache.tapestry5.json.JSONArray;
019    import org.apache.tapestry5.json.JSONObject;
020    import org.apache.tapestry5.services.javascript.InitializationPriority;
021    import org.apache.tapestry5.services.javascript.StylesheetLink;
022    
023    import java.util.Map;
024    
025    public class PartialMarkupDocumentLinker implements DocumentLinker
026    {
027        private final JSONArray scripts = new JSONArray();
028    
029        private final JSONArray stylesheets = new JSONArray();
030    
031        private final Map<InitializationPriority, JSONObject> priorityToInits = CollectionFactory.newMap();
032    
033        public void addScriptLink(String scriptURL)
034        {
035            scripts.put(scriptURL);
036        }
037    
038        public void addStylesheetLink(StylesheetLink stylesheet)
039        {
040            JSONObject object = new JSONObject(
041    
042                    "href", stylesheet.getURL(),
043    
044                    "media", stylesheet.getOptions().media);
045    
046            stylesheets.put(object);
047        }
048    
049        public void addScript(InitializationPriority priority, String script)
050        {
051            throw new UnsupportedOperationException(
052                    "DocumentLinker.addScript() is not implemented for partial page renders.");
053        }
054    
055        public void setInitialization(InitializationPriority priority, JSONObject initialization)
056        {
057            priorityToInits.put(priority, initialization);
058        }
059    
060        /**
061         * Commits changes, adding one or more keys to the reply.
062         *
063         * @param reply JSON Object to be sent to client
064         */
065        public void commit(JSONObject reply)
066        {
067            if (scripts.length() > 0)
068                reply.put("scripts", scripts);
069    
070            if (stylesheets.length() > 0)
071                reply.put("stylesheets", stylesheets);
072    
073            JSONArray inits = new JSONArray();
074    
075            for (InitializationPriority p : InitializationPriority.values())
076            {
077                JSONObject init = priorityToInits.get(p);
078    
079                if (init != null)
080                    inits.put(init);
081            }
082    
083            if (inits.length() > 0)
084                reply.put("inits", inits);
085        }
086    }