
com.viaoa.jfc.editor.html.OAHTMLParser Maven / Gradle / Ivy
/* Copyright 1999-2015 Vince Via [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 com.viaoa.jfc.editor.html;
/**
Parse and "clean up" HTML code.
Currently used by Editor to convert pasted code from Microsoft Word.
This needs to be set up to convert styles to HTML tags.
*/
public class OAHTMLParser {
HTMLTokenManager tokenManager;
HTMLToken token;
public static final int EOF = 1;
public static final int VARIABLE = 2;
public static final int GT = 3;
public static final int LT = 4;
public static final int EQUAL = 5;
public static final int STRING = 6;
public static final int SQ = 7; // single quote
public static final int DQ = 8; // double quote
public static final int SLASH = 9; // "/>"
public static final int COMMENT = 10;
public OAHTMLParser() {
tokenManager = new HTMLTokenManager();
}
public boolean isMicrosoft(String code) {
return (code != null && code.toLowerCase().indexOf("urn:schemas-microsoft-com") >= 0);
}
/**
Removes all code outside and including html body tags, and leading white space and leading paragraphs
*/
public static String removeBody(String code) {
if (code == null) return null;
String lcode = code.toLowerCase();
int beginPos = 0;
int endPos = code.length();
String tag = "body";
beginPos = lcode.indexOf("<"+tag);
if (beginPos < 0) {
tag = "html";
beginPos = lcode.indexOf("<"+tag);
}
if (beginPos >= 0) {
beginPos = code.indexOf(">", beginPos);
if (beginPos > 0) {
beginPos++;
endPos = lcode.indexOf(""+tag, beginPos);
if (endPos < 0) return code;
}
}
if (beginPos < 0) {
beginPos = 0;
// 20110329 check for ending