001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.page; 018 019import org.apache.wicket.Component; 020import org.apache.wicket.Page; 021import org.apache.wicket.request.Response; 022import org.apache.wicket.request.http.WebResponse; 023import org.apache.wicket.util.string.Strings; 024 025/** 026 * A {@link PartialPageUpdate} that serializes itself to XML. 027 */ 028public class XmlPartialPageUpdate extends PartialPageUpdate 029{ 030 /** 031 * The name of the root element in the produced XML document. 032 */ 033 public static final String START_ROOT_ELEMENT = "<ajax-response>"; 034 public static final String END_ROOT_ELEMENT = "</ajax-response>"; 035 036 public XmlPartialPageUpdate(final Page page) 037 { 038 super(page); 039 } 040 041 @Override 042 public void setContentType(WebResponse response, String encoding) 043 { 044 response.setContentType("text/xml; charset=" + encoding); 045 } 046 047 @Override 048 protected void writeHeader(Response response, String encoding) 049 { 050 response.write("<?xml version=\"1.0\" encoding=\""); 051 response.write(encoding); 052 response.write("\"?>"); 053 response.write(START_ROOT_ELEMENT); 054 } 055 056 /** 057 * TODO remove in Wicket 10 058 */ 059 @Override 060 protected void writeComponent(Response response, String markupId, Component component, 061 String encoding) 062 { 063 super.writeComponent(response, markupId, component, encoding); 064 } 065 066 @Override 067 protected void writeComponent(Response response, String markupId, CharSequence contents) 068 { 069 response.write("<component id=\""); 070 response.write(markupId); 071 response.write("\" ><![CDATA["); 072 response.write(encode(contents)); 073 response.write("]]></component>"); 074 } 075 076 @Override 077 protected void writeFooter(Response response, String encoding) 078 { 079 response.write(END_ROOT_ELEMENT); 080 } 081 082 @Override 083 protected void writePriorityEvaluation(Response response, CharSequence contents) 084 { 085 writeHeaderContribution(response, "priority-evaluate", contents); 086 } 087 088 @Override 089 protected void writeHeaderContribution(Response response, CharSequence contents) 090 { 091 writeHeaderContribution(response, "header-contribution", contents); 092 } 093 094 @Override 095 protected void writeEvaluation(Response response, CharSequence contents) 096 { 097 writeHeaderContribution(response, "evaluate", contents); 098 } 099 100 private void writeHeaderContribution(Response response, String elementName, CharSequence contents) 101 { 102 if (Strings.isEmpty(contents) == false) 103 { 104 response.write("<" + elementName + ">"); 105 106 // we need to write response as CDATA and parse it on client, 107 response.write("<![CDATA[<head xmlns:wicket=\"http://wicket.apache.org\">"); 108 response.write(encode(contents)); 109 response.write("</head>]]>"); 110 response.write("</" + elementName + ">"); 111 } 112 } 113 114 protected CharSequence encode(CharSequence str) 115 { 116 return Strings.replaceAll(str, "]]>", "]]]]><![CDATA[>"); 117 } 118 119 120}