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

org.ajaxanywhere.parser.HTMLEditorKitResponseParser 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.parser;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.*;
import java.util.regex.Pattern;

/**
 * HTMLEditorKitResponseParser.java is
 * 
 * events 基于 HTMLEditorKit的html转换器
 * 
 * @author Alex
 *
 */
public class HTMLEditorKitResponseParser implements ResponseParser {
	private static ResponseParser ourInstance = new HTMLEditorKitResponseParser();
	public static final Pattern SCRIPT_START_PATTERN = Pattern.compile(" and  substrings
		// therefore we replace <> and  with dummy strings and replace them back
		// after parsing.

		String dummy = findDummy(html);
		html = html.replaceAll("<>", "<" + dummy + ">").replaceAll("", "");

		// JSE 6 regression workarpond :
		// http://forum.java.sun.com/thread.jspa?threadID=5118473
		// let's use a depricated DIR tag instead of SCRIPT
		html = SCRIPT_START_PATTERN.matcher(html).replaceAll("", "<>").replaceAll("", ""));
		for (int i = 0; i < responseBean.getScriptContents().size(); i++) {
			String s = (String) responseBean.getScriptContents().get(i);
			responseBean.getScriptContents().set(i,
					s.replaceAll("<" + dummy + ">", "<>").replaceAll("", ""));
		}
		return responseBean;
	}

	private String findDummy(String html) {
		String dummy;
		do {
			dummy = Double.toString(Math.random());
		} while (html.indexOf(dummy) != -1);

		return dummy;
	}

	@SuppressWarnings("rawtypes")
	public HTMLBlock doParse(final String html) {

		HTMLBlock res = new HTMLBlock();
		try {
			final StringBuffer contentHTML = new StringBuffer();
			final List scripts = res.getScriptContents();
			final Set images = res.getImages();

			HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
				private boolean insideScript;
				private StringBuffer scriptContent = new StringBuffer();
				private int lastStop;

				private void append(String str) {
					if (insideScript) {
						scriptContent.append(str);
					} else {
						contentHTML.append(str);
					}
				}

				public void appendSinceLastStop(int newPos) {
					if (lastStop > newPos)
						return;
					append(html.substring(lastStop, newPos));
					lastStop = newPos;
				}

				@SuppressWarnings("unchecked")
				private void flushScript() {
					int posScriptEnd = scriptContent.indexOf(">");
					if (posScriptEnd == -1)
						posScriptEnd = 0;
					int posC1 = scriptContent.indexOf("", posScriptEnd);
					int posC22 = scriptContent.indexOf("//]]>", posScriptEnd);

					if ((posC2 != -1) && (posQ2 == -1 || posC2 > posQ2) && (posQ1 == -1 || posC2 > posQ1))
						scriptContent.delete(posC2, posC2 + 3);
					if ((posC22 != -1) && (posQ2 == -1 || posC22 > posQ2) && (posQ1 == -1 || posC22 > posQ1))
						scriptContent.delete(posC22, posC22 + 5);

					int len = scriptContent.length();
					if (len > 0 && scriptContent.charAt(len - 1) == '>') {
						int lastEndTagPos = scriptContent.lastIndexOf(" pos)
						return;
					appendSinceLastStop(pos);

					if (tag == HTML.Tag.DIR && insideScript) { /** DIR is prevously replaced SCRIPT tag to **/
						int posScriptEnd = html.indexOf('>', pos);

						if (posScriptEnd != -1)
							lastStop = posScriptEnd + 1;

						insideScript = false;
						flushScript();
					}
				}

				public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attributes, int pos) {
					handleStartTag(tag, attributes, pos);
				}

				@SuppressWarnings("unchecked")
				public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes, int pos) {
					appendSinceLastStop(pos);
					if (tag == HTML.Tag.DIR) { /** DIR is prevously replaced SCRIPT tag to **/
						insideScript = true;
					} else if (tag == HTML.Tag.IMG) {
						images.add(attributes.getAttribute(HTML.Attribute.SRC));
					}
				}

			};

			Reader reader = new StringReader(html);
			new ParserDelegator().parse(reader, callback, false);
			callback.handleComment(null, html.length());

			res.setHtmlContent(contentHTML.toString());
			return res;

		} catch (IOException e) {
			throw new RuntimeException(e.toString()); // this should never heppen
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy