All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajaxanywhere.XMLBuilder Maven / Gradle / Ivy

Go to download

本项目主要弥补在使用mybatis3+springmvc+jquery easyui快速搭建web应用系统是遇到的框架不足. 主要工作包括: 1,扩展了ApplicationContextAware,通过单例注入spring容器,提供spring容器外的bean获取方法 2,扩展了apache shiro框架,统一了安全结构 3,扩展了mybatis3拦截器,在两个拦截器中自动完成分页注入,实现内存分页。 4,分页设计数据库方言 5,提供了一个easyuigrid的模型 6,提供了java泛型的jstl 7, xml工具包等一些小工具

The newest version!
/*
Copyright 2005  Vitaliy Shevchuk ([email protected])

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

package org.ajaxanywhere;

import org.ajaxanywhere.parser.HTMLBlock;
import org.ajaxanywhere.parser.ResponseParser;
import org.ajaxanywhere.parser.ResponseParserFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.logging.Logger;

/**
 * Date: 23 juil. 2005 Time: 21:50:21
 */
public class XMLBuilder implements AAConstants {
	private final static Logger LOGGER = Logger.getLogger(XMLBuilder.class.getName());

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void sendZones(BufferResponseWrapper bufferResponseWrapper, Set refreshZones) {
		StringBuilder xml = new StringBuilder(8192);
		xml.append("").append("<").append(AA_XML_ZONES)
				.append(">");

		List scripts = new ArrayList();
		Set images = new HashSet();

		for (Iterator iterator = refreshZones.iterator(); iterator.hasNext();) {
			String zone = (String) iterator.next();
			String content = AAUtils.getZoneContent(zone, bufferResponseWrapper);

			// if zone added to refresh list but not present in content, then exclude zone
			// info in response
			if (content == null) {
				continue;
			}

			HTMLBlock bean = handleZoneContent(content);

			xml.append("<").append(AA_XML_ZONE).append(" ").append(AA_XML_NAME).append("=\"").append(zone).append("\"")
					.append(">");
			xml.append("");
			xml.append("");

			scripts.addAll(bean.getScriptContents());
			images.addAll(bean.getImages());
		}

		for (int i = 0; i < scripts.size(); i++) {
			String script = (String) scripts.get(i);
			int posScriptEnd = script.indexOf('>');
			if (posScriptEnd != -1)
				script = script.substring(posScriptEnd + 1);

			xml.append("<").append(AA_XML_SCRIPT).append(">");
			xml.append("");
			xml.append("");
		}

		for (Iterator it = images.iterator(); it.hasNext();) {
			String image = (String) it.next();

			xml.append("<").append(AA_XML_IMAGE).append(">");
			xml.append("");
			xml.append("");
		}
		xml.append("");

		sendDOMDocument(bufferResponseWrapper.getOriginalResponse(), xml.toString());
	}

	/**
	 * Find out script, image tags in content, wrapp them in new XML node
	 * 
	 * @param content
	 * @param zoneNode
	 * @param doc
	 * @param scripts
	 * @param images
	 * @param root
	 */
	private static HTMLBlock handleZoneContent(String content) {
		ResponseParser parser = ResponseParserFactory.getInstance().getParser();
		return parser.parse(content);
	}

	private static void sendDOMDocument(HttpServletResponse originalResponse, String doc) {
		try {
			OutputStream out = originalResponse.getOutputStream();
			String encoding = originalResponse.getCharacterEncoding();
			if (encoding == null) {
				out.write(doc.getBytes());
				LOGGER.warning("charset is not set for response, this will use default String.getBytes()");
			} else {
				out.write(doc.getBytes(encoding));
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void handleError(HttpServletResponse response, Throwable exception) {
		StringBuilder xml = new StringBuilder(1024);
		xml.append("").append("<").append(AA_XML_ZONES)
				.append(">");
		xml.append("<").append(AA_XML_EXCEPTION).append(">");
		xml.append("");
		xml.append("");

		sendDOMDocument(response, xml.toString());
	}

	public static void sendRedirect(BufferResponseWrapper bufferResponseWrapper) {
		StringBuilder xml = new StringBuilder(1024);
		xml.append("").append("<").append(AA_XML_ZONES)
				.append(">");
		xml.append("<").append(AA_XML_REDIRECT).append(">");
		xml.append("");
		xml.append("");

		sendDOMDocument(bufferResponseWrapper.getOriginalResponse(), xml.toString());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy