001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.cyberneko; 018 019import java.io.OutputStream; 020import java.io.UnsupportedEncodingException; 021import java.util.Collections; 022import java.util.HashMap; 023import java.util.Map; 024import org.cyberneko.html.filters.Writer; 025 026public class HtmlWriter 027 extends Writer { 028 029 /** Creates a new instance of HtmlWriter */ 030 public HtmlWriter() { 031 super(); 032 } 033 034 /** Creates a new instance of HtmlWriter */ 035 public HtmlWriter(OutputStream outputStream, String encoding) 036 throws UnsupportedEncodingException { 037 super(outputStream, encoding); 038 } 039 040 /** Creates a new instance of HtmlWriter */ 041 public HtmlWriter(java.io.Writer writer, String encoding) { 042 super(writer, encoding); 043 } 044 045 private static Map<String, String> entityTranslations; 046 047 static { 048 Map<String, String> entityTranslationsMap = new HashMap<>(1); 049 050 entityTranslationsMap.put("apos", "'"); 051 052 entityTranslations = Collections.unmodifiableMap(entityTranslationsMap); 053 } 054 055 /** Print entity. */ 056 @Override 057 protected void printEntity(String entity) { 058 String translatedEntity = entityTranslations.get(entity); 059 060 if(translatedEntity == null) { 061 super.printEntity(entity); 062 } else { 063 fPrinter.print(translatedEntity); 064 fPrinter.flush(); 065 } 066 } 067 068}