001// Copyright 2008-2013 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 015package org.apache.tapestry5.internal.services; 016 017import org.apache.tapestry5.internal.InternalConstants; 018import org.apache.tapestry5.json.JSONArray; 019import org.apache.tapestry5.json.JSONObject; 020import org.apache.tapestry5.services.javascript.InitializationPriority; 021import org.apache.tapestry5.services.javascript.ModuleConfigurationCallback; 022import org.apache.tapestry5.services.javascript.StylesheetLink; 023 024import java.util.List; 025 026public class PartialMarkupDocumentLinker implements DocumentLinker 027{ 028 private final JSONArray libraryURLs = new JSONArray(); 029 030 private final JSONArray stylesheets = new JSONArray(); 031 032 private final ModuleInitsManager initsManager = new ModuleInitsManager(); 033 034 public void addCoreLibrary(String libraryURL) 035 { 036 notImplemented("addCoreLibrary"); 037 } 038 039 public void addLibrary(String libraryURL) 040 { 041 libraryURLs.put(libraryURL); 042 } 043 044 public void addStylesheetLink(StylesheetLink stylesheet) 045 { 046 JSONObject object = new JSONObject( 047 048 "href", stylesheet.getURL(), 049 050 "media", stylesheet.getOptions().media); 051 052 stylesheets.put(object); 053 } 054 055 private void notImplemented(String methodName) 056 { 057 throw new UnsupportedOperationException(String.format("DocumentLinker.%s() is not implemented for partial page renders.", methodName)); 058 } 059 060 public void addScript(InitializationPriority priority, String script) 061 { 062 notImplemented("addScript"); 063 } 064 065 public void addModuleConfigurationCallback(ModuleConfigurationCallback callback) 066 { 067 notImplemented("moduleConfigurationCallback"); 068 } 069 070 public void addInitialization(InitializationPriority priority, String moduleName, String functionName, JSONArray arguments) 071 { 072 initsManager.addInitialization(priority, moduleName, functionName, arguments); 073 } 074 075 /** 076 * Commits changes, adding one or more keys to the reply. 077 * 078 * @param reply 079 * JSON Object to be sent to client 080 */ 081 public void commit(JSONObject reply) 082 { 083 if (libraryURLs.length() > 0) 084 { 085 reply.in(InternalConstants.PARTIAL_KEY).put("libraries", libraryURLs); 086 } 087 088 if (stylesheets.length() > 0) 089 { 090 reply.in(InternalConstants.PARTIAL_KEY).put("stylesheets", stylesheets); 091 } 092 093 List<?> inits = initsManager.getSortedInits(); 094 095 if (inits.size() > 0) 096 { 097 reply.in(InternalConstants.PARTIAL_KEY).put("inits", JSONArray.from(inits)); 098 } 099 } 100}