org.vulpe.commons.util.VulpeStringUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.vulpe.commons.util.VulpeStringUtil.java

Source

 /**
  * Vulpe Framework - Quick and Smart ;)
  * Copyright (C) 2011 Active Thread
  *
  * Este programa  software livre; voc pode redistribu-lo e/ou
  * modific-lo sob os termos da Licena Pblica Geral GNU, conforme
  * publicada pela Free Software Foundation; tanto a verso 2 da
  * Licena como (a seu critrio) qualquer verso mais nova.
  *
  * Este programa  distribudo na expectativa de ser til, mas SEM
  * QUALQUER GARANTIA; sem mesmo a garantia implcita de
  * COMERCIALIZAO ou de ADEQUAO A QUALQUER PROPSITO EM
  * PARTICULAR. Consulte a Licena Pblica Geral GNU para obter mais
  * detalhes.
  *
  * Voc deve ter recebido uma cpia da Licena Pblica Geral GNU
  * junto com este programa; se no, escreva para a Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  */
 /**
  * Vulpe Framework - Quick and Smart ;)
  * Copyright (C) 2011 Active Thread
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 package org.vulpe.commons.util;

 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.sql.Blob;
 import java.util.ArrayList;
 import java.util.List;

 import javax.sql.rowset.serial.SerialBlob;

 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

 /**
  * Vulpe String Utility class.
  *
  * @author <a href="mailto:felipe@vulpe.org">Geraldo Felipe</a>
  * @version 1.0
  * @since 1.0
  */
 public class VulpeStringUtil {

     private static VulpeHashMap<Character, String> ACCENT_MAP = new VulpeHashMap<Character, String>();
     private static final VulpeHashMap<Character, String> UTF_CHARS = new VulpeHashMap<Character, String>();
     private static final VulpeHashMap<Character, String> SPECIAL_CHARS = new VulpeHashMap<Character, String>();

     private static final Logger LOG = LoggerFactory.getLogger(VulpeStringUtil.class);

static {
   ACCENT_MAP.put('', "a");
   ACCENT_MAP.put('', "a");
   ACCENT_MAP.put('', "a");
   ACCENT_MAP.put('', "a");
   ACCENT_MAP.put('', "a");

   ACCENT_MAP.put('?', "A");
   ACCENT_MAP.put('', "A");
   ACCENT_MAP.put('', "A");
   ACCENT_MAP.put('', "A");
   ACCENT_MAP.put('', "A");

   ACCENT_MAP.put('', "e");
   ACCENT_MAP.put('', "e");
   ACCENT_MAP.put('', "e");
   ACCENT_MAP.put('', "e");

   ACCENT_MAP.put('', "E");
   ACCENT_MAP.put('', "E");
   ACCENT_MAP.put('', "E");
   ACCENT_MAP.put('', "E");

   ACCENT_MAP.put('', "i");
   ACCENT_MAP.put('', "i");
   ACCENT_MAP.put('', "i");
   ACCENT_MAP.put('', "i");

   ACCENT_MAP.put('?', "I");
   ACCENT_MAP.put('', "I");
   ACCENT_MAP.put('', "I");
   ACCENT_MAP.put('?', "I");

   ACCENT_MAP.put('', "o");
   ACCENT_MAP.put('', "o");
   ACCENT_MAP.put('', "o");
   ACCENT_MAP.put('', "o");
   ACCENT_MAP.put('', "o");

   ACCENT_MAP.put('', "O");
   ACCENT_MAP.put('', "O");
   ACCENT_MAP.put('', "O");
   ACCENT_MAP.put('', "O");
   ACCENT_MAP.put('', "O");

   ACCENT_MAP.put('', "u");
   ACCENT_MAP.put('', "u");
   ACCENT_MAP.put('', "u");
   ACCENT_MAP.put('', "u");

   ACCENT_MAP.put('', "U");
   ACCENT_MAP.put('', "U");
   ACCENT_MAP.put('', "U");
   ACCENT_MAP.put('', "U");

   // (')
   SPECIAL_CHARS.put('', "&aacute;");
   SPECIAL_CHARS.put('', "&eacute;");
   SPECIAL_CHARS.put('', "&iacute;");
   SPECIAL_CHARS.put('', "&oacute;");
   SPECIAL_CHARS.put('', "&uacute;");
   SPECIAL_CHARS.put('?', "&Aacute;");
   SPECIAL_CHARS.put('', "&Eacute;");
   SPECIAL_CHARS.put('?', "&Iacute;");
   SPECIAL_CHARS.put('', "&Oacute;");
   SPECIAL_CHARS.put('', "&Uacute;");
   // (~)
   SPECIAL_CHARS.put('', "&atilde;");
   SPECIAL_CHARS.put('', "&ntilde;");
   SPECIAL_CHARS.put('', "&otilde;");
   SPECIAL_CHARS.put('', "&Atilde;");
   SPECIAL_CHARS.put('', "&Ntilde;");
   SPECIAL_CHARS.put('', "&Otilde;");
   // (^)
   SPECIAL_CHARS.put('', "&acirc;");
   SPECIAL_CHARS.put('', "&ecirc;");
   SPECIAL_CHARS.put('', "&icirc;");
   SPECIAL_CHARS.put('', "&ocirc;");
   SPECIAL_CHARS.put('', "&ucirc;");
   SPECIAL_CHARS.put('', "&Acirc;");
   SPECIAL_CHARS.put('', "&Ecirc;");
   SPECIAL_CHARS.put('', "&Icirc;");
   SPECIAL_CHARS.put('', "&Ocirc;");
   SPECIAL_CHARS.put('', "&Ucirc;");
   // ( )
   SPECIAL_CHARS.put('', "&ccedil;");
   SPECIAL_CHARS.put('', "&Ccedil;");
}

     /**
      * String accent normalize
      *
      * @param term
      * @return
      */
     public static String normalize(final String term) {
         final StringBuilder normalized = new StringBuilder();
         for (int i = 0; i < term.length(); i++) {
             normalized.append(
                     ACCENT_MAP.containsKey(term.charAt(i)) ? ACCENT_MAP.get(term.charAt(i)) : term.charAt(i));
         }
         return normalized.toString();
     }

     /**
      *
      * @param value
      * @return
      */
     public static String encodeUTF(final String value) {
         final StringBuilder encoded = new StringBuilder();
         for (int i = 0; i < value.length(); i++) {
             encoded.append(
                     UTF_CHARS.containsKey(value.charAt(i)) ? UTF_CHARS.get(value.charAt(i)) : value.charAt(i));
         }
         return encoded.toString();
     }

     /**
      *
      * @param value
      * @return
      */
     public static String encodeHTMLSpecials(final String value) {
         final StringBuilder encoded = new StringBuilder();
         for (int i = 0; i < value.length(); i++) {
             encoded.append(SPECIAL_CHARS.containsKey(value.charAt(i)) ? SPECIAL_CHARS.get(value.charAt(i))
                     : value.charAt(i));
         }
         return encoded.toString();
     }

     /**
      *
      * @param value
      * @return
      */
     public static String capitalize(final String value) {
         final String[] parts = value.toLowerCase().split("\\s");
         final StringBuilder cap = new StringBuilder();
         if (parts.length > 1) {
             int count = 0;
             for (String part : parts) {
                 cap.append(upperCaseFirst(part));
                 if (count < parts.length) {
                     cap.append(" ");
                 }
                 ++count;
             }
         } else {
             cap.append(upperCaseFirst(value.toLowerCase()));
         }
         return cap.toString();
     }

     /**
      * Puts first char in upper case.
      *
      * @param value
      * @return
      */
     public static String upperCaseFirst(final String value) {
         return value.substring(0, 1).toUpperCase() + value.substring(1);
     }

     /**
      * Puts first char in lower case.
      *
      * @param value
      * @return
      */
     public static String lowerCaseFirst(final String value) {
         return value.substring(0, 1).toLowerCase() + value.substring(1);
     }

     /**
      * Convert SQL Blob to String.
      *
      * @param blob
      * @return
      */
     public static String blobToString(final Blob blob) {
         String blobString = null;
         try {
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             final byte[] buffer = new byte[1024];
             final InputStream inputStream = blob.getBinaryStream();
             int count = 0;
             while ((count = inputStream.read(buffer)) >= 0) {
                 baos.write(buffer, 0, count);
             }
             inputStream.close();
             final byte[] bytes = baos.toByteArray();
             blobString = new String(bytes);
         } catch (Exception e) {
             LOG.error(e.getMessage());
         }
         return blobString;
     }

     /**
      * Convert SQL Blob to String.
      *
      * @param blob
      * @return
      */
     public static Blob stringToBlob(final String string) {
         Blob blob = null;
         try {
             blob = new SerialBlob(string.getBytes());
         } catch (Exception e) {
             LOG.error(e.getMessage());
         }
         return blob;
     }

     public static int count(final String value, final String token) {
         int count = 0;
         for (int i = 0; i < value.length(); i++) {
             if (value.charAt(i) == token.charAt(0)) {
                 ++count;
             }
         }
         return count;
     }

     public static int count(final String value, final String tokenBegin, final String tokenEnd) {
         String[] valueParts = value.split(tokenBegin);
         int count = 0;
         for (String string : valueParts) {
             if (string.contains(tokenEnd)) {
                 break;
             }
             ++count;
         }
         return count;
     }

     public static String separateWords(final String value) {
         final StringBuilder newValue = new StringBuilder();
         for (int i = 0; i < value.length(); i++) {
             if (i > 0 && StringUtils.isAllUpperCase(value.substring(i, i + 1))) {
                 newValue.append(" ").append(value.charAt(i));
             } else {
                 newValue.append(value.charAt(i));
             }
         }
         return newValue.toString();
     }

     public static String getAttributeName(final String value) {
         String attributeName = value;
         if (StringUtils.isAllUpperCase(value)) {
             attributeName = value.toLowerCase();
         } else {
             attributeName = lowerCaseFirst(value);
         }
         return attributeName;
     }

     public static String encode(final String value, final String encode) {
         String encoded = value;
         try {
             byte[] bytes = value.getBytes(encode);
             encoded = new String(bytes, encode);
         } catch (UnsupportedEncodingException e) {
             LOG.error(e.getMessage());
         }
         return encoded;
     }

     public static String concat(final Object object, final String... properties) {
         final StringBuilder concat = new StringBuilder();
         for (final String property : properties) {
             final Object value = VulpeReflectUtil.getFieldValue(object, property);
             concat.append(String.valueOf(value));
         }
         return concat.toString();
     }

     public static String getValueFromLast(String value, String token) {
         return value.substring(value.lastIndexOf(token) + 1);
     }

     public static List<String> splitHierarchy(String value) {
         final List<String> parts = new ArrayList<String>();
         int firstPos = value.indexOf('[');
         int lastPos = value.lastIndexOf(']');
         String parent = "";
         if (firstPos > 0) {
             parent = value.substring(0, firstPos);
         }
         value = value.substring(firstPos + 1, lastPos);
         final List<Integer> positions = new ArrayList<Integer>();
         for (int i = 0; i < value.length(); i++) {
             if (value.charAt(i) == ',') {
                 positions.add(i);
             }
         }
         if (positions.size() == 1) {
             int pos = positions.get(0);
             String cut = value.substring(0, pos);
             parts.add(parent + "." + cut);
             cut = value.substring(pos + 1);
             parts.add(parent + "." + cut);
         } else {
             int pos2 = 0;
             for (int i = 0; i < positions.size(); i++) {
                 int pos = positions.get(i);
                 String cut = value.substring(pos2, pos);
                 int open = VulpeStringUtil.count(cut, "[");
                 int close = VulpeStringUtil.count(cut, "]");
                 if (cut.contains("[") && cut.contains("]") && open == close) {
                     parts.add(parent + "." + cut);
                 } else if (cut.contains("[") || cut.contains("]")) {
                     if (i == (positions.size() - 1)) {
                         cut = value.substring(pos2);
                         parts.add(parent + "." + cut);
                     }
                     continue;
                 } else {
                     parts.add(parent + "." + cut);
                 }
                 pos2 = pos + 1;
                 if (i == (positions.size() - 1)) {
                     cut = value.substring(pos2);
                     parts.add(parent + "." + cut);
                 }
             }
         }
         return parts;
     }

     public static void main(String[] args) {
         String expressao = "x[teste1[teste1_1,teste1_2[teste1_2_1,teste1_2_2,teste1_2_3[teste1_2_3_1,teste1_2_3_2],teste1_2_4]],teste2,teste3[teste3_1]]";
         for (String string : splitHierarchy(expressao)) {
             System.out.println(string);
         }
     }

 }