tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mindbri...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/org/apache/tapestry/util/text MarkupCharacterTranslator.java DefaultCharacterTranslatorSource.java MLCharacterTranslator.java
Date Fri, 11 Jun 2004 19:51:17 GMT
mindbridge    2004/06/11 12:51:17

  Modified:    framework/src/org/apache/tapestry/util/text
                        DefaultCharacterTranslatorSource.java
  Added:       framework/src/org/apache/tapestry/util/text
                        MarkupCharacterTranslator.java
  Removed:     framework/src/org/apache/tapestry/util/text
                        MLCharacterTranslator.java
  Log:
  Updating new class name
  
  Revision  Changes    Path
  1.2       +3 -3      jakarta-tapestry/framework/src/org/apache/tapestry/util/text/DefaultCharacterTranslatorSource.java
  
  Index: DefaultCharacterTranslatorSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/util/text/DefaultCharacterTranslatorSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultCharacterTranslatorSource.java	11 Jun 2004 19:45:40 -0000	1.1
  +++ DefaultCharacterTranslatorSource.java	11 Jun 2004 19:51:17 -0000	1.2
  @@ -29,8 +29,8 @@
    */
   public class DefaultCharacterTranslatorSource implements ICharacterTranslatorSource
   {
  -	private static final ICharacterTranslator DEFAULT_TRANSLATOR = new MLCharacterTranslator();
  -	private static final ICharacterTranslator UNICODE_TRANSLATOR = new MLCharacterTranslator(false);
  +	private static final ICharacterTranslator DEFAULT_TRANSLATOR = new MarkupCharacterTranslator();
  +	private static final ICharacterTranslator UNICODE_TRANSLATOR = new MarkupCharacterTranslator(false);
   
   	private final static Map _translators;
   	
  
  
  
  1.1                  jakarta-tapestry/framework/src/org/apache/tapestry/util/text/MarkupCharacterTranslator.java
  
  Index: MarkupCharacterTranslator.java
  ===================================================================
  //Copyright 2004 The Apache Software Foundation
  //
  // 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.apache.tapestry.util.text;
  
  /**
   * An object that encodes a character according to rules of the HTML specification, 
   * so that it will be properly parsed by a browser irrespectively of the character
   * encoding used in the HTML output.
   * 
   * @author mb
   * @version $Id: MarkupCharacterTranslator.java,v 1.1 2004/06/11 19:51:17 mindbridge Exp
$
   * @since 3.1
   */
  public class MarkupCharacterTranslator implements ICharacterTranslator
  {
      private static final String SAFE_CHARACTERS =
          "01234567890"
              + "abcdefghijklmnopqrstuvwxyz"
              + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
              + "\t\n\r !#$%'()*+,-./:;=?@[\\]^_`{|}~";
  
      private static final String[][] ENTITIES = {
      	{ "\"", """ }, 
  		{ "<", "&lt;" },
  		{ ">", "&gt;" },
  		{ "&", "&amp;" }
      };
      
      private static final ICharacterMatcher SAFE_MATCHER = new AsciiCharacterMatcher(SAFE_CHARACTERS);
      private static final ICharacterTranslator ENTITY_TRANSLATOR = new AsciiCharacterTranslator(ENTITIES);
      
      private boolean _encodeNonAscii;
      private ICharacterMatcher _safeMatcher;
      private ICharacterTranslator _entityTranslator;
  	
      public MarkupCharacterTranslator()
      {
      	this(true);
      }
      
      public MarkupCharacterTranslator(boolean encodeNonAscii)
      {
      	this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR);
      }
      
      public MarkupCharacterTranslator(boolean encodeNonAscii, ICharacterMatcher safeMatcher,
ICharacterTranslator entityTranslator)
      {
      	_encodeNonAscii = encodeNonAscii;
      	_safeMatcher = safeMatcher;
      	_entityTranslator = entityTranslator;
      }
  
      public MarkupCharacterTranslator(boolean encodeNonAscii, String safeCharacters, String[][]
entities)
      {
      	_encodeNonAscii = encodeNonAscii;
      	_safeMatcher = new AsciiCharacterMatcher(safeCharacters);
      	_entityTranslator = new AsciiCharacterTranslator(entities);
      }
      
  	/**
  	 * @see org.apache.tapestry.util.text.IMarkupCharacterTranslator#translateAttribute(char)
  	 */
  	public String translate(char ch) {
  		if (ch >= 128 && !_encodeNonAscii)
  			return null;
  		
  		if (_safeMatcher.matches(ch))
  			return null;
  
  		String entity = _entityTranslator.translate(ch);
  		if (entity != null)
  			return entity;
  		
  		// needs to use a NumberFormat here to be fully compliant, 
  		// but this is accepted fine by the browsers
  		return "&#" + (int) ch + ";";
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message