/*- * ================================================================================ * ECOMP Portal SDK * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property * ================================================================================ * 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.openecomp.portalsdk.analytics.util; import java.io.*; import org.openecomp.portalsdk.analytics.RaptorObject; public class HtmlStripper extends RaptorObject// this function can be accessed publicly { String filename; final char TXT_END_TAG = '>'; final char TXT_START_TAG = '<'; public String stripSpecialCharacters (String s) { s = nvl(s); StringBuffer sbuf = new StringBuffer(); int c; int prev_char = 0; boolean inText = true; for (int i = 0; i < s.length(); i++) { // while c is not last character c = s.charAt(i); //System.out.println("444 Character " + (char)c + " int " + c); if ((char) c == TXT_END_TAG) { // if char == '>' text following // it and not HTML tag inText = true; } else if ((char) c == TXT_START_TAG) { // if char == '<' tag is // following not text inText = false; } /*else if ((char)c == '\n') { System.out.println("new line " + (char)c + " int " + c); inText = false; }*/ /*else if ((char) c == '&') { // if char == '&' chars following // are not text inText = false; }*/ else if (inText) { // if text write char to "text.txt" //System.out.println("444444 Adding " + (char)c + " int " + c); if (c >= 32 && c <= 122) { if(c==32 || (c>32 && c<=47) || ( c>=58 && c<=64 )) { if (c == 45) { prev_char = 0; sbuf.append((char) 45); } else if(prev_char != 95) { prev_char = 95; sbuf.append((char) 95); } } else { prev_char=0; sbuf.append((char) c); } } } } //System.out.println("\n\nFinished processing: " + s + "\n\n"); //System.out.println("\n\nThe processed String : " + sbuf.toString() + "\n\n"); return sbuf.toString(); } public String stripHtml(String s) { //System.out.println("Starting to process: " + s + "\n\n"); // prints s = nvl(s); StringBuffer sbuf = new StringBuffer(); int c; boolean inText = true; for (int i = 0; i < s.length(); i++) { // while c is not last character c = s.charAt(i); if ((char) c == TXT_END_TAG) { // if char == '>' text following // it and not HTML tag inText = true; } else if ((char) c == TXT_START_TAG) { // if char == '<' tag is // following not text inText = false; } /*else if ((char)c == '\n') { System.out.println("new line " + (char)c + " int " + c); inText = false; }*/ /*else if ((char) c == '&') { // if char == '&' chars following // are not text inText = false; }*/ else if (inText) { // if text write char to "text.txt" if (c >= 32 && c <= 122) { sbuf.append((char) c); } } } //System.out.println("\n\nFinished processing: " + s + "\n\n"); //System.out.println("\n\nThe processed String : " + sbuf.toString() + "\n\n"); int pos = 0 ; while(sbuf.indexOf(" ")!=-1) { pos = sbuf.indexOf(" "); sbuf.replace(pos, pos+6, " "); } return sbuf.toString(); } public String stripCSVHtml (String s) { String s1 = stripHtml(s); //s1 = Utils.replaceInString(s1, "\"", "\"\""); s1 = s1.replaceAll("\"", "\"\""); return s1; } public static void main(String[] args) { String st = " Import: report name hello $ # " ; System.out.println("hello " + new HtmlStripper().stripSpecialCharacters(st)); StringBuffer strBuf = new StringBuffer(""); strBuf.append("\n\n"); strBuf.append(" \n"); strBuf.append("\n"); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append(""); strBuf.append("
Charge To Account ABBZ2
dateMonth 04/30/2008
Generated Date/Time 05/30/2008 06:15:16 PM GMT
Login Id: sundar
"); new HtmlStripper().stripHtml(strBuf.toString()); } }