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

org.jfcutils.files.conversions.EscapeCommonXML Maven / Gradle / Ivy

Go to download

This is a Java API with utilities to work with files, strings and HTTP connections

The newest version!
package org.jfcutils.files.conversions;

import java.util.HashMap;
import java.util.Map;

/**
 * This class supplies some methods to unescape special common chars according XML specifications
 * Then the content will need a CDATA section
 *
 */
public class EscapeCommonXML {
	
	//singleton
	private static EscapeCommonXML instance;

	private Map basic_char;
	
	/**
	 * Build the map to undecode HTML characters. Then, the text needs a CDATA section
	 */
	public EscapeCommonXML(){
		basic_char = new HashMap();
		basic_char.put(""","\"");
		basic_char.put("&","&");
		basic_char.put("<","<");
		basic_char.put(">",">");
		basic_char.put("'","'");
		basic_char.put("α","α");
		basic_char.put("–","–");
		basic_char.put("γ","γ");
		basic_char.put("δ","δ");
		basic_char.put("μ","μ");
	}
	
	public static synchronized EscapeCommonXML getInstance(){
		if(instance==null)
			instance = new EscapeCommonXML();
		return instance;	
	}
	
	/**
	 * Unescape XML characters
	 * @param text the string to unescape
	 * @return the unescaped string
	 */
	public String unescapeXML(String text){
		for(String key: basic_char.keySet()){
			text = text.replaceAll(key, basic_char.get(key));
		}
		return text;
	}
	
	/**
	 * Remove common HTML tags in a string
	 * @param text the source string
	 * @return a string that replaces HTML common tags with nothing
	 */
	public String removeCommonHTMLTags(String text){
		text = text.replaceAll("

", ""); text = text.replaceAll("

", ""); text = text.replaceAll("

", ""); text = text.replaceAll("

", ""); text = text.replaceAll("

", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("", ""); text = text.replaceAll("
", ""); text = text.replaceAll("
", ""); text = text.replaceAll("

", ""); text = text.replaceAll("
", ""); text = text.replaceAll("\n", ""); text = text.replaceAll("\r", ""); text = text.replaceAll("
    ", ""); text = text.replaceAll("
      ", ""); text = text.replaceAll("
    ", ""); text = text.replaceAll("
", ""); text = text.replaceAll("
  • ", ""); text = text.replaceAll("
  • ", ""); text = text.replaceAll("
  • ", ""); text = text.replaceAll("", ""); return text; } /** * Remove common HTML tags in a string and unescape entities * @param text the source string * @return the source string without common HTML tags and entities (the XML can need CDATA section) */ public String removeHTMLTagsAndUnescape(String text){ text = this.removeCommonHTMLTags(text); return this.unescapeXML(text); } public static void main(String[] args){ String source = "

    Experiments on use of an agar-gel method for recovery of migrating Ascaris suum larvae from the liver and lungs of pigs were conducted to obtain fast standardized methods. Subsamples of blended tissues of pig liver and lungs were mixed with agar to a final concentration of 1% agar and the larvae allowed to migrate out of the agar-gel into 0.9% NaCl at 38°C. The results showed that within 3 h more than 88% of the recoverable larvae migrated out of the liver agar-gel and more than 83% of the obtained larvae migrated out of the lung agar-gel. The larvae were subsequently available in a very clean suspension which reduced the sample counting time. Blending the liver for 60 sec in a commercial blender showed significantly higher larvae recovery than blending for 30 sec. Addition of gentamycin to reduce bacterial growth during incubation, glucose to increase larval motility during migration or ice to increase sedimentation of migrated larvae did not influence larvae recovery significantly.

    ]]>"; System.out.println(EscapeCommonXML.getInstance().removeCommonHTMLTags(source)); } }




    © 2015 - 2025 Weber Informatics LLC | Privacy Policy