EscapeSQLConfiguration.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.sql;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. /**
  24.  * EscapeSQLConfiguration
  25.  *
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class EscapeSQLConfiguration {

  32.     // Alcuni caratteri possono essere 'escaped' tramite un comune carattere di escape (con in aggiunta una eventuale clausola di escape)
  33.     // Altri devono essere 'escaped' tramite un carattere di escape di default senza la clausola di escape
  34.    
  35.     private List<EscapeWithOtherEscapeChar> escapeWithOtherEscapeChar = new ArrayList<>();
  36.     private List<Character> escapeChar = new ArrayList<>();
  37.     private char escape;
  38.     private boolean useEscapeClausole = false;
  39.    
  40.    
  41.     public char getEscape() {
  42.         return this.escape;
  43.     }
  44.     public void setEscape(char escape) {
  45.         this.escape = escape;
  46.     }
  47.    
  48.     public boolean isUseEscapeClausole() {
  49.         return this.useEscapeClausole;
  50.     }
  51.     public void setUseEscapeClausole(boolean useEscapeClausole) {
  52.         this.useEscapeClausole = useEscapeClausole;
  53.     }
  54.    
  55.     public void addCharacter(char c){
  56.         this.escapeChar.add(c);
  57.     }
  58.     public void addCharacterWithOtherEscapeChar(char c,char escape){
  59.         EscapeWithOtherEscapeChar e = new EscapeWithOtherEscapeChar();
  60.         e.setCharacter(c);
  61.         e.setEscape(escape);
  62.         this.escapeWithOtherEscapeChar.add(e);
  63.     }
  64.    
  65.     public boolean isDefaultEscape(char c){
  66.         if(this.escapeChar!=null && !this.escapeChar.isEmpty()){
  67.             for (Character check : this.escapeChar) {
  68.                 if(check.charValue() == c){
  69.                     return true;
  70.                 }
  71.             }
  72.         }
  73.         return false;
  74.     }
  75.     public boolean isOtherEscape(char c){
  76.         if(this.escapeWithOtherEscapeChar!=null && !this.escapeWithOtherEscapeChar.isEmpty()){
  77.             for (EscapeWithOtherEscapeChar check : this.escapeWithOtherEscapeChar) {
  78.                 if(check.getCharacter() == c){
  79.                     return true;
  80.                 }
  81.             }
  82.         }
  83.         return false;
  84.     }
  85.     public char getOtherEscapeCharacter(char c) throws SQLQueryObjectException{
  86.         if(this.escapeWithOtherEscapeChar!=null && !this.escapeWithOtherEscapeChar.isEmpty()){
  87.             for (EscapeWithOtherEscapeChar check : this.escapeWithOtherEscapeChar) {
  88.                 if(check.getCharacter() == c){
  89.                     return check.getEscape();
  90.                 }
  91.             }
  92.         }
  93.         throw new SQLQueryObjectException("Not exists escape char for character ["+c+"]");
  94.     }
  95. }

  96. class EscapeWithOtherEscapeChar {
  97.    
  98.     private char character;
  99.     private char escape;
  100.    
  101.     public char getCharacter() {
  102.         return this.character;
  103.     }
  104.     public void setCharacter(char character) {
  105.         this.character = character;
  106.     }
  107.     public char getEscape() {
  108.         return this.escape;
  109.     }
  110.     public void setEscape(char escape) {
  111.         this.escape = escape;
  112.     }
  113.    
  114. }