insert HTML block dynamically : HTML Output « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » HTML Output 




insert HTML block dynamically
 
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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. 
 *  
 */



/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 
 @author <a href="mailto:[email protected]">Dungeon Project</a>
 */
public class Main {


  /**
   * This method is used to insert HTML block dynamically
   
   @param source
   *            the HTML code to be processes
   @param replaceNl
   *            if true '\n' will be replaced by &lt;br>
   @param replaceTag
   *            if true '<' will be replaced by &lt; and '>' will be replaced
   *            by &gt;
   @param replaceQuote
   *            if true '\"' will be replaced by &quot;
   @return the formated html block
   */
  public static final String formatHtmlString source, boolean replaceNl, boolean replaceTag,
      boolean replaceQuote )
  {
      StringBuffer buf = new StringBuffer();
      int len = source.length();

      for int ii = 0; ii < len; ii++ )
      {
          char ch = source.charAtii );
          
          switch ch )
          {
              case '\"':
                  if replaceQuote )
                  {
                      buf.append"&quot;" );
                  }
                  else
                  {
                      buf.appendch );
                  }
                  break;

              case '<':
                  if replaceTag )
                  {
                      buf.append"&lt;" );
                  }
                  else
                  {
                      buf.appendch );
                  }
                  break;

              case '>':
                  if replaceTag )
                  {
                      buf.append"&gt;" );
                  }
                  else
                  {
                      buf.appendch );
                  }
                  break;

              case '\n':
                  if replaceNl )
                  {
                      if replaceTag )
                      {
                          buf.append"&lt;br&gt;" );
                      }
                      else
                      {
                          buf.append"<br>" );
                      }
                  }
                  else
                  {
                      buf.appendch );
                  }
                  break;

              case '\r':
                  break;

              case '&':
                  buf.append"&amp;" );
                  break;

              default:
                  buf.appendch );
                  break;
          }
      }

      return buf.toString();
  }


}

   
  














Related examples in the same category
1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Prints a conversion table of miles per gallon to kilometers per liter
4.Servlet: Print Table
5.Html utilities
6.Html Parse Servlet
7.Escape and unescape string
8.Escapes newlines, tabs, backslashes, and quotes in the specified string
9.Web Calendar
10.HTML Helper
11.Escape HTML
12.Convert HTML to text
13.Text To HTML
14.Unescape HTML
15.Java object representations of the HTML table structure
16.Entity Decoder
17.Format a color to HTML RGB color format (e.g. #FF0000 for Color.red)
18.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
19.Encode special characters and do formatting for HTML output
20.HTML color names
21.Utility methods for dealing with HTML
22.Filter the specified message string for characters that are sensitive in HTML
23.A collection of all character entites defined in the HTML4 standard.
24.Decode an HTML color string like '#F567BA;' into a Color
25.Normalize Post Data
26.Get HTML Color String from Java Color object
27.HTML Decoder
28.HTML Parser
29.HTML color and Java Color
30.HTML form Utilites
31.Html Dimensions
32.break Lines with HTML
33.Convert an integer to an HTML RGB value
34.Convert to HTML string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.