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 @Override 057 protected void writeComponent(Response response, String markupId, CharSequence contents) 058 { 059 response.write("<component id=\""); 060 response.write(markupId); 061 response.write("\" ><![CDATA["); 062 response.write(encode(contents)); 063 response.write("]]></component>"); 064 } 065 066 @Override 067 protected void writeFooter(Response response, String encoding) 068 { 069 response.write(END_ROOT_ELEMENT); 070 } 071 072 @Override 073 protected void writePriorityEvaluation(Response response, CharSequence contents) 074 { 075 writeHeaderContribution(response, "priority-evaluate", contents); 076 } 077 078 @Override 079 protected void writeHeaderContribution(Response response, CharSequence contents) 080 { 081 writeHeaderContribution(response, "header-contribution", contents); 082 } 083 084 @Override 085 protected void writeEvaluation(Response response, CharSequence contents) 086 { 087 writeHeaderContribution(response, "evaluate", contents); 088 } 089 090 private void writeHeaderContribution(Response response, String elementName, CharSequence contents) 091 { 092 if (Strings.isEmpty(contents) == false) 093 { 094 response.write("<" + elementName + ">"); 095 096 // we need to write response as CDATA and parse it on client, 097 response.write("<![CDATA[<head xmlns:wicket=\"http://wicket.apache.org\">"); 098 response.write(encode(contents)); 099 response.write("</head>]]>"); 100 response.write("</" + elementName + ">"); 101 } 102 } 103 104 protected CharSequence encode(CharSequence str) 105 { 106 return Strings.replaceAll(str, "]]>", "]]]]><![CDATA[>"); 107 } 108 109 110}