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

com.ibm.commons.util.TextUtil Maven / Gradle / Ivy

The newest version!
/*
 * © Copyright IBM Corp. 2012-2013
 * 
 * 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 com.ibm.commons.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;



/**
 * Text utilities.
 * @ibm-api
 */
public final class TextUtil {

    /**
     * Add a string before each row of an existing string.
     * 

* This function may be used by code generators in order to properly indent * code. To be consistent with Reader's behavior, a line is considered to be * terminated by any one of a line feed ('\n'), a carriage return ('\r) or * a carriage return immediately followed by a linefeed. In each case, the end * of line characters are preserved as they are. *

* @ibm-api */ public static String indentString( String s, String indent ) { if( !StringUtil.isEmpty(s) && !StringUtil.isEmpty(indent) ) { FastStringBuffer buf = new FastStringBuffer(); for( int i=0; i * This replace all the escape sequences by the actual characters. *

* @ibm-api */ public static String fromJavaString( String s ) { FastStringBuffer b = new FastStringBuffer(); int len = s.length(); for( int i=0; i * This replace all the special characters by escape sequences. *

* @ibm-api */ public static String toJavaString(String s, boolean addQuotes) { FastStringBuffer b = new FastStringBuffer(); b.appendJavaString(s,addQuotes); return b.toString(); } /** * Converts a XML string with internal entities into a Java string. *

* The entities parsed are *

    *
  • &
    *
  • '
    *
  • >
    *
  • <
    *
  • "
    *
*

* @param s the XML string * @return the converted string * @ibm-api */ public static String fromXMLString( String s ) { if (s == null) { return null; } FastStringBuffer b = null; int pos = 0; int length = s.length(); while(true) { int nextEntity = s.indexOf('&',pos); if( nextEntity<0 ) { nextEntity = length; } // Copy the part of the string if( nextEntity>pos ) { if( b==null ) { b = new FastStringBuffer(); } b.append(s, pos, nextEntity); pos = nextEntity; } if( pos'); pos+=3; } else { if( s.startsWith("lt;",pos) ) { //$NON-NLS-1$ b.append('<'); pos+=3; } else { if( s.startsWith("quot;",pos) ) { //$NON-NLS-1$ b.append('\"'); pos+=5; } else { if( s.startsWith("#",pos) ) { //$NON-NLS-1$ pos++; int val = 0; do { int ch = s.charAt(pos++); if(ch==';') { break; } if(ch>='0' && ch<='9') { val = val*10 + (ch-'0'); } } while(true); b.append((char)val); } else { throw new AbstractRuntimeException( null, "Unknown entity", s ); //$NON-NLS-1$ } } } } } } } else { return b!=null ? b.toString() : s; } } } /** * Converts a Java string to an XML one, with internal entities into a Java string. *

* The entities parsed are *

    *
  • &
    *
  • '
    *
  • >
    *
  • <
    *
  • "
    *
*

* @param s the Java string * @return the converted string * @ibm-api */ public static String toXMLString(String s) { if( s==null ) { return null; } FastStringBuffer b = null; char[] chars = s.toCharArray(); int length = chars.length; for( int i=0; i': case '<': case '\"': { if( b==null ) { b = new FastStringBuffer(); b.append(s, 0, i); } if( c=='&' ) { b.append( "&" ); break; } //$NON-NLS-1$ if( c=='\'' ) { b.append( "'" ); break; } //$NON-NLS-1$ if( c=='>' ) { b.append( ">" ); break; } //$NON-NLS-1$ if( c=='<' ) { b.append( "<" ); break; } //$NON-NLS-1$ if( c=='\"' ) { b.append( """ ); break; } //$NON-NLS-1$ } break; default: { if( b!=null ) { b.append(c); } } } } return b!=null ? b.toString() : s; } /** * Converts a Java string to an XML one, with internal entities into a Java string. * The entities parsed are *

*

    *
  • &
    *
  • '
    *
  • >
    *
  • <
    *
  • "
    *
*

* @param s the Java string * @param ascii ensure that all the character are in the ascii range, else they will be replaced by an entity * @return the converted string * @ibm-api */ public static String toXMLString(String s, boolean ascii) { if( s==null ) { return null; } FastStringBuffer b = new FastStringBuffer(s.length()+256); char[] chars = s.toCharArray(); int length = chars.length; for( int i=0; i': case '<': case '\"': { if( c=='&' ) { b.append( "&" ); break; } //$NON-NLS-1$ if( c=='\'' ) { b.append( "'" ); break; } //$NON-NLS-1$ if( c=='>' ) { b.append( ">" ); break; } //$NON-NLS-1$ if( c=='<' ) { b.append( "<" ); break; } //$NON-NLS-1$ if( c=='\"' ) { b.append( """ ); break; } //$NON-NLS-1$ } break; default: { if( ascii && (c>127) ) { b.append("&#"+Integer.toString((int)c)+";"); } else { b.append(c); } } } } return b.toString(); } // TESTS.... // public static final void main(String[] args) { // try { // BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // while(true) { // System.out.println("Enter XML text (quit to leave):"); // $NON-NLS-1$ // String text = ""; // while(true) { // String s = in.readLine(); // if(s.equals("quit")) { // $NON-NLS-1$ // System.exit(0); // } // if(s.equals("")) { // break; // } // if(text.length()>0) { // text = text + "\n"; // $NON-NLS-1$ // } // text = text + s; // } // text = StringUtil.replace(text, '\n', ' '); // text = StringUtil.replace(text, '\n', '\0'); // String xml = toXMLString(text,false); // System.out.println(""); // System.out.println(xml); // System.out.println(""); // System.out.println(""); // } // } catch(Throwable t) { // t.printStackTrace(); // } // } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy