URLRegExpExtractor.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.pdd.core.dynamic;

  21. import java.util.List;

  22. import org.openspcoop2.utils.regexp.RegExpException;
  23. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  24. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  25. import org.slf4j.Logger;

  26. /**
  27.  * URLRegExpExtractor
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class URLRegExpExtractor {

  34.     private Logger log;
  35.    
  36.     private String url;
  37.    
  38.     public URLRegExpExtractor(String url, Logger log) {
  39.         this.url = url;
  40.         this.log = log;
  41.     }
  42.    
  43.     public boolean match(String pattern) throws DynamicException {
  44.         String v = read(pattern);
  45.         return v!=null && !"".equals(v);
  46.     }
  47.    
  48.     public String read(String pattern) throws DynamicException {
  49.         String valore = null;
  50.         try {
  51.             valore = RegularExpressionEngine.getStringMatchPattern(this.url, pattern);
  52.         }
  53.         catch(RegExpNotFoundException e){
  54.             this.log.debug("Estrazione '"+pattern+"' non ha trovato risultati: "+e.getMessage(),e);
  55.         }
  56.         catch(RegExpException e){
  57.             throw new DynamicException(e.getMessage(),e);
  58.         }
  59.         catch(Exception e){
  60.             throw new DynamicException("Estrazione '"+pattern+"' fallita: "+e.getMessage(),e);
  61.         }
  62.         return valore;
  63.     }
  64.    
  65.     public List<String> readList(String pattern) throws DynamicException {
  66.         List<String> valore = null;
  67.         try {
  68.             valore = RegularExpressionEngine.getAllStringMatchPattern(this.url, pattern);
  69.         }
  70.         catch(RegExpNotFoundException e){
  71.             this.log.debug("Estrazione '"+pattern+"' non ha trovato risultati: "+e.getMessage(),e);
  72.         }
  73.         catch(RegExpException e){
  74.             throw new DynamicException(e.getMessage(),e);
  75.         }
  76.         catch(Exception e){
  77.             throw new DynamicException("Estrazione '"+pattern+"' fallita: "+e.getMessage(),e);
  78.         }
  79.         return valore;
  80.     }
  81.    
  82.    
  83.    
  84.     public boolean found(String pattern) throws DynamicException {
  85.         String v = find(pattern);
  86.         return v!=null && !"".equals(v);
  87.     }
  88.    
  89.     public String find(String pattern) throws DynamicException {
  90.         String valore = null;
  91.         try {
  92.             valore = RegularExpressionEngine.getStringFindPattern(this.url, pattern);
  93.         }
  94.         catch(RegExpNotFoundException e){
  95.             this.log.debug("Estrazione '"+pattern+"' non ha trovato risultati: "+e.getMessage(),e);
  96.         }
  97.         catch(RegExpException e){
  98.             throw new DynamicException(e.getMessage(),e);
  99.         }
  100.         catch(Exception e){
  101.             throw new DynamicException("Estrazione '"+pattern+"' fallita: "+e.getMessage(),e);
  102.         }
  103.         return valore;
  104.     }
  105.    
  106.     public List<String> findAll(String pattern) throws DynamicException {
  107.         List<String> valore = null;
  108.         try {
  109.             valore = RegularExpressionEngine.getAllStringFindPattern(this.url, pattern);
  110.         }
  111.         catch(RegExpNotFoundException e){
  112.             this.log.debug("Estrazione '"+pattern+"' non ha trovato risultati: "+e.getMessage(),e);
  113.         }
  114.         catch(RegExpException e){
  115.             throw new DynamicException(e.getMessage(),e);
  116.         }
  117.         catch(Exception e){
  118.             throw new DynamicException("Estrazione '"+pattern+"' fallita: "+e.getMessage(),e);
  119.         }
  120.         return valore;
  121.     }
  122. }