Java Code Examples for org.apache.commons.lang.StringEscapeUtils#unescapeJava()

The following examples show how to use org.apache.commons.lang.StringEscapeUtils#unescapeJava() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextTupleCreator.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public void setup(JsonNode json) throws IOException
{
    if (json.has("params"))
    {
        JsonNode params = json.get("params");
        if (params.has("separator"))
        {
            String str = params.get("separator").getTextValue();
            str = StringEscapeUtils.unescapeJava(str);
            byte[] bytes = str.getBytes("UTF-8");
            separator = new String(bytes);
        }
    }

    schema = new BlockSchema(json.get("schema"));
    typeArray = new DataType[schema.getNumColumns()];
    for (int i = 0; i < schema.getNumColumns(); i++)
        typeArray[i] = schema.getType(i);

    tuple = TupleFactory.getInstance().newTuple(schema.getNumColumns());
}
 
Example 2
Source File: TextDataParserFactory.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private DataParser createParser(String id, OverrunReader reader, long offset) throws DataParserException {
  Utils.checkState(reader.getPos() == 0, Utils.formatL("reader must be in position '0', it is at '{}'",
    reader.getPos()));
  try {

    return new TextCharDataParser(
        getSettings().getContext(),
        id,
        getSettings().<Boolean>getConfig(MULTI_LINE_KEY),
        getSettings().<Boolean>getConfig(USE_CUSTOM_DELIMITER_KEY),
        StringEscapeUtils.unescapeJava(getSettings().<String>getConfig(CUSTOM_DELIMITER_KEY)),
        getSettings().<Boolean>getConfig(INCLUDE_CUSTOM_DELIMITER_IN_TEXT_KEY),
        reader,
        offset,
        getSettings().getMaxRecordLen(),
        TEXT_FIELD_NAME,
        TRUNCATED_FIELD_NAME,
        stringBuilderPool
    );
  } catch (IOException ex) {
    throw new DataParserException(Errors.TEXT_PARSER_00, id, offset, ex.toString(), ex);
  }
}
 
Example 3
Source File: CSVFile.java    From incubator-tajo with Apache License 2.0 6 votes vote down vote up
public CSVScanner(Configuration conf, final Schema schema, final TableMeta meta, final FileFragment fragment)
    throws IOException {
  super(conf, schema, meta, fragment);
  factory = new CompressionCodecFactory(conf);
  codec = factory.getCodec(fragment.getPath());
  if (codec == null || codec instanceof SplittableCompressionCodec) {
    splittable = true;
  }

  //Delimiter
  String delim  = meta.getOption(CatalogConstants.CSVFILE_DELIMITER, CatalogConstants.CSVFILE_DELIMITER_DEFAULT);
  this.delimiter = StringEscapeUtils.unescapeJava(delim).charAt(0);

  String nullCharacters = StringEscapeUtils.unescapeJava(meta.getOption(CatalogConstants.CSVFILE_NULL));
  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes();
  }
}
 
Example 4
Source File: Concat_ws.java    From tajo with Apache License 2.0 6 votes vote down vote up
@Override
public Datum eval(Tuple params) {
  if (params.isBlankOrNull(0)) {
    return NullDatum.get();
  }

  String separator = StringEscapeUtils.unescapeJava(params.getText(0));

  StringBuilder result = new StringBuilder();

  int paramSize = params.size();
  for(int i = 1; i < paramSize; i++) {
    if (params.isBlankOrNull(i)) {
      continue;
    }
    if (result.length() > 0) {
      result.append(separator);
    }
    result.append(params.getText(i));
  }
  return DatumFactory.createText(result.toString());
}
 
Example 5
Source File: RegexFormatter.java    From ReScue with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Remove the front and the end \'"
 * @param reg
 * @return
 */
public static String nakeRegex(String reg) {
	if (reg != null && reg.length() > 1) {
		if (reg.charAt(reg.length() - 1) == '\n') reg = reg.substring(0, reg.length() - 1); // Delete the end \n
		// u'\\n'
		// First char is u		and		length > 3	and			first char equals to last char		  and	the first char is " or '
		if (reg.charAt(0) == 'u' && reg.length() > 3 && (reg.charAt(1) == reg.charAt(reg.length() - 1) && (reg.charAt(1) == '"' || reg.charAt(1) == '\''))) {
			reg = reg.substring(2, reg.length() - 1);
			if (!(Pattern.compile("(^\\\\[^\\\\])|([^\\\\]\\\\[^\\\\])").matcher(reg).find()) && reg.contains("\\\\")) {
				reg = StringEscapeUtils.unescapeJava(reg);
			}
		// There are split char at the front and the end
		} else if (reg.charAt(0) == reg.charAt(reg.length() - 1)) {
			if (reg.charAt(0) == '/') {
				reg = reg.substring(1, reg.length() - 1);
			} else if (reg.charAt(0) == '"' || reg.charAt(0) == '\'') {
				reg = reg.substring(1, reg.length() - 1);
				if (!(Pattern.compile("(^\\\\[^\\\\])|([^\\\\]\\\\[^\\\\])").matcher(reg).find()) && reg.contains("\\\\")) {
					reg = StringEscapeUtils.unescapeJava(reg);
				}
			}
		}
	}
	return reg;
}
 
Example 6
Source File: PscpLiveService.java    From Alice-LiveMan with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public URI getLiveVideoInfoUrl(ChannelInfo channelInfo) throws Exception {
    String channelUrl = channelInfo.getChannelUrl();
    String channelHtml = HttpRequestUtil.downloadUrl(new URI(channelUrl), channelInfo.getChannelUrl(), Collections.emptyMap(), StandardCharsets.UTF_8);
    Matcher dataStoreMatcher = dataStorePattern.matcher(channelHtml);
    if (dataStoreMatcher.find()) {
        String dataStore = StringEscapeUtils.unescapeJava(dataStoreMatcher.group(0));
        JSONObject dataStoreObj = JSON.parseObject(dataStore);
        JSONObject broadcasts = dataStoreObj.getJSONObject("BroadcastCache").getJSONObject("broadcasts");
        for (String videoId : broadcasts.keySet()) {
            JSONObject broadcastObj = broadcasts.getJSONObject(videoId);
            String state = broadcastObj.getJSONObject("broadcast").getString("state");
            if ("RUNNING".equals(state)) {
                return new URI(LIVE_VIDEO_URL + videoId);
            }
        }
    } else {
        throw new RuntimeException("没有找到DataStore[" + channelUrl + "]");
    }
    return null;
}
 
Example 7
Source File: CommonTestConfigUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the name of the test containing this element, or null if it can't be calculated.
 */
@Nullable
public String findTestName(@Nullable PsiElement elt) {
  if (elt == null) return null;

  final DartCallExpression call = findEnclosingTestCall(elt, getTestsFromOutline(elt.getContainingFile()));
  if (call == null) return null;

  final DartStringLiteralExpression lit = DartSyntax.getArgument(call, 0, DartStringLiteralExpression.class);
  if (lit == null) return null;

  final String name = DartSyntax.unquote(lit);
  if (name == null) return null;

  return StringEscapeUtils.unescapeJava(name);
}
 
Example 8
Source File: StringAgg.java    From tajo with Apache License 2.0 6 votes vote down vote up
@Override
public void eval(FunctionContext ctx, Tuple params) {
  ConcatContext concatCtx = (ConcatContext) ctx;

  String concatData = null;
  if (!params.isBlankOrNull(0)) {
    concatData = params.getText(0);
  }

  if (concatCtx.concatData.length() > 0) {
    concatCtx.concatData.append(concatCtx.delimiter);
  } else {
    // When first time, set the delimiter.
    concatCtx.delimiter = StringEscapeUtils.unescapeJava(params.getText(1));
  }
  concatCtx.concatData.append(concatData);
}
 
Example 9
Source File: TextLineSerDe.java    From tajo with Apache License 2.0 5 votes vote down vote up
public static byte[] getNullCharsAsBytes(TableMeta meta, String key, String defaultVal) {
  byte [] nullChars;

  String nullCharacters = StringEscapeUtils.unescapeJava(meta.getProperty(key, defaultVal));
  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes(Bytes.UTF8_CHARSET);
  }

  return nullChars;
}
 
Example 10
Source File: TextLineSerDe.java    From tajo with Apache License 2.0 5 votes vote down vote up
public static byte [] getNullCharsAsBytes(TableMeta meta) {
  byte [] nullChars;

  String nullCharacters = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.TEXT_NULL,
      NullDatum.DEFAULT_TEXT));
  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes(Bytes.UTF8_CHARSET);
  }

  return nullChars;
}
 
Example 11
Source File: EntityQualifierUtils.java    From Eagle with Apache License 2.0 5 votes vote down vote up
private static String unescape(String str){
	int start=0,end = str.length();
	if(str.startsWith("\"")) start = start +1;
	if(str.endsWith("\"")) end = end -1;
	str = str.substring(start,end);
	return StringEscapeUtils.unescapeJava(str);
}
 
Example 12
Source File: UnicodeCodec.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public String decode(@NonNull String text) {
    setMax(1);
    setConfident(0);
    try {
        String result = StringEscapeUtils.unescapeJava(text);
        incConfident();
        return result;
    } catch (Exception e) {
        return text;
    }
}
 
Example 13
Source File: OAuthInterceptorService.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
 * Method that adds all parameters needed to make the request with the provider
 *
 * @param uri The {@link UriComponentsBuilder} to set query params
 * @param providerParams List of the {@link ProviderParam}
 * @return The {@link Http} result
 */
private HttpEntity createHttpEntity(UriComponentsBuilder uri, List<ProviderParam> providerParams) {

    HttpHeaders headers = new HttpHeaders();
    String body = "";

    Map<String, Object> paramsBody = new HashMap<>();
    MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();

    for (ProviderParam param : providerParams) {

        if (Objects.isNull(param.getValue()) || param.getValue().isEmpty()) {
            param.setValue(context.getRequest().getHeader(param.getName()));
        }

        switch (param.getLocation().toUpperCase()) {
            case "HEADER":
                headers.add(param.getName(), param.getValue());
                break;
            case "BODY":
                paramsBody.put(param.getName(), param.getValue());
                break;
            default:
                queryParams.put(param.getName(), Collections.singletonList(param.getValue()));
                break;
        }
    }

    if (paramsBody.size() > 0) {
        try {
            body = StringEscapeUtils.unescapeJava(mapper().writeValueAsString(paramsBody));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
        }
    }

    uri.queryParams(queryParams);
    return new HttpEntity<>(body, headers);
}
 
Example 14
Source File: JSONConfiguration.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
@Override
public String saveToString() {

    // if (!options().prettyPrint()) {
    // }
    final Object value = SerializationHelper.serialize(getValues(false));
    final String dump = StringEscapeUtils.unescapeJava(outputGson.toJson(value));

    if (dump.equals(BLANK_CONFIG)) {
        return "";
    }

    return dump;
}
 
Example 15
Source File: CSVFile.java    From incubator-tajo with Apache License 2.0 5 votes vote down vote up
public CSVAppender(Configuration conf, final Schema schema, final TableMeta meta, final Path path) throws IOException {
  super(conf, schema, meta, path);
  this.fs = path.getFileSystem(conf);
  this.meta = meta;
  this.schema = schema;
  this.delimiter = StringEscapeUtils.unescapeJava(this.meta.getOption(CatalogConstants.CSVFILE_DELIMITER,
      CatalogConstants.CSVFILE_DELIMITER_DEFAULT)).charAt(0);
  this.columnNum = schema.size();
  String nullCharacters = StringEscapeUtils.unescapeJava(this.meta.getOption(CatalogConstants.CSVFILE_NULL));
  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes();
  }
}
 
Example 16
Source File: Replacement.java    From maven-replacer-plugin with MIT License 4 votes vote down vote up
private String unescape(String text) {
	return StringEscapeUtils.unescapeJava(text);
}
 
Example 17
Source File: JwtVerifier.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
public JwtToken getVerifiedJwtToken(String encodedJwt) throws BadCredentialsException {
	try {
		JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(encodedJwt);
		JwtToken jwt = jwtConsumer.getJwtToken();

		String escapedKid = jwt.getJwsHeaders().getKeyId();
		String kid = escapedKid;
		if (!Strings.isNullOrEmpty(kid) && !kid.isEmpty()) {
			kid = StringEscapeUtils.unescapeJava(escapedKid);
			if (escapedKid != kid) {
				log.info("Escaped Key ID from JWT Token");
			}
		}
		JsonWebKey key = keyProvider.getKey(kid);
		
		// Algorithm is not mandatory for the key material, so we set it to the same as the JWT
		if (key.getAlgorithm() == null && key.getPublicKeyUse() == PublicKeyUse.SIGN && key.getKeyType() == KeyType.RSA)
		{
			key.setAlgorithm(jwt.getJwsHeaders().getAlgorithm());
		}
		
		JwsSignatureVerifier signatureVerifier = getInitializedSignatureVerifier(key, jwt);


		boolean signatureValid = jwtConsumer.verifySignatureWith(signatureVerifier);

		if (!signatureValid && Strings.isNullOrEmpty(kid)) {
			key = keyProvider.getKeyAfterRefresh(null);
			signatureVerifier = getInitializedSignatureVerifier(key, jwt);
			signatureValid = jwtConsumer.verifySignatureWith(signatureVerifier);
		}

		if (!signatureValid) {
			throw new BadCredentialsException("Invalid JWT signature");
		}

		validateClaims(jwt);

		return jwt;
	} catch (JwtException e) {
		throw new BadCredentialsException(e.getMessage(), e);
	}
}
 
Example 18
Source File: SequenceFileAppender.java    From tajo with Apache License 2.0 4 votes vote down vote up
@Override
public void init() throws IOException {
  os = new NonSyncByteArrayOutputStream(BUFFER_SIZE);

  this.fs = path.getFileSystem(conf);

  // Set value of non-deprecated key for backward compatibility.
  if (!meta.containsProperty(StorageConstants.TEXT_DELIMITER)
    && meta.containsProperty(StorageConstants.SEQUENCEFILE_DELIMITER)) {
    this.delimiter = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.SEQUENCEFILE_DELIMITER,
      StorageConstants.DEFAULT_FIELD_DELIMITER)).charAt(0);
  } else {
    this.delimiter = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.TEXT_DELIMITER,
      StorageConstants.DEFAULT_FIELD_DELIMITER)).charAt(0);
  }

  String nullCharacters;
  if (!meta.containsProperty(StorageConstants.TEXT_NULL)
    && meta.containsProperty(StorageConstants.SEQUENCEFILE_NULL)) {
    nullCharacters = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.SEQUENCEFILE_NULL,
      NullDatum.DEFAULT_TEXT));
  } else {
    nullCharacters = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.TEXT_NULL,
      NullDatum.DEFAULT_TEXT));
  }

  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes();
  }

  this.columnNum = schema.size();

  if(this.meta.containsProperty(StorageConstants.COMPRESSION_CODEC)) {
    String codecName = this.meta.getProperty(StorageConstants.COMPRESSION_CODEC);
    codecFactory = new CompressionCodecFactory(conf);
    codec = codecFactory.getCodecByClassName(codecName);
  } else {
    if (fs.exists(path)) {
      throw new AlreadyExistsStorageException(path);
    }
  }

  try {
    String serdeClass = this.meta.getProperty(StorageConstants.SEQUENCEFILE_SERDE,
        TextSerializerDeserializer.class.getName());
    serde = (SerializerDeserializer) Class.forName(serdeClass).newInstance();
    serde.init(schema);
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
    throw new IOException(e);
  }

  Class<? extends Writable>  keyClass, valueClass;
  if (serde instanceof BinarySerializerDeserializer) {
    keyClass = BytesWritable.class;
    EMPTY_KEY = new BytesWritable();
    valueClass = BytesWritable.class;
  } else {
    keyClass = LongWritable.class;
    EMPTY_KEY = new LongWritable();
    valueClass = Text.class;
  }

  String type = this.meta.getProperty(StorageConstants.COMPRESSION_TYPE, CompressionType.NONE.name());
  if (type.equals(CompressionType.BLOCK.name())) {
    writer = SequenceFile.createWriter(fs, conf, path, keyClass, valueClass, CompressionType.BLOCK, codec);
  } else if (type.equals(CompressionType.RECORD.name())) {
    writer = SequenceFile.createWriter(fs, conf, path, keyClass, valueClass, CompressionType.RECORD, codec);
  } else {
    writer = SequenceFile.createWriter(fs, conf, path, keyClass, valueClass, CompressionType.NONE, codec);
  }

  if (tableStatsEnabled) {
    this.stats = new TableStatistics(this.schema, columnStatsEnabled);
  }

  super.init();
}
 
Example 19
Source File: UDPBinding.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public void updated(Dictionary config) throws ConfigurationException {

    super.updated(config);

    if (config == null) {
        return;
    }

    String timeOutString = Objects.toString(config.get("buffersize"), null);
    if (isNotBlank(timeOutString)) {
        timeOut = Integer.parseInt(timeOutString);
    } else {
        logger.info("The maximum timeout for blocking write operations will be set to the default value of {}",
                timeOut);
    }

    String blockingString = Objects.toString(config.get("retryinterval"), null);
    if (isNotBlank(blockingString)) {
        blocking = Boolean.parseBoolean(blockingString);
    } else {
        logger.info("The blocking nature of read/write operations will be set to the default value of {}",
                blocking);
    }

    String preambleString = Objects.toString(config.get("preamble"), null);
    if (isNotBlank(preambleString)) {
        preAmble = StringEscapeUtils.unescapeJava(preambleString);
    } else {
        logger.info("The preamble for all write operations will be set to the default value of {}", preAmble);
    }

    String postambleString = Objects.toString(config.get("postamble"), null);
    if (isNotBlank(postambleString)) {
        postAmble = StringEscapeUtils.unescapeJava(postambleString);
    } else {
        logger.info("The postamble for all write operations will be set to the default value of {}", postAmble);
    }

    String updateWithResponseString = Objects.toString(config.get("updatewithresponse"), null);
    if (isNotBlank(updateWithResponseString)) {
        updateWithResponse = Boolean.parseBoolean(updateWithResponseString);
    } else {
        logger.info("Updating states with returned values will be set to the default value of {}",
                updateWithResponse);
    }

    String charsetString = Objects.toString(config.get("charset"), null);
    if (isNotBlank(charsetString)) {
        charset = charsetString;
    } else {
        logger.info("The character set will be set to the default value of {}", charset);
    }
}
 
Example 20
Source File: RCFile.java    From tajo with Apache License 2.0 4 votes vote down vote up
@Override
public void init() throws IOException {
  sync = new byte[SYNC_HASH_SIZE];
  syncCheck = new byte[SYNC_HASH_SIZE];

  more = startOffset < endOffset;
  rowId = new LongWritable();
  readBytes = 0;

  String nullCharacters = StringEscapeUtils.unescapeJava(meta.getProperty(StorageConstants.RCFILE_NULL,
      NullDatum.DEFAULT_TEXT));
  if (StringUtils.isEmpty(nullCharacters)) {
    nullChars = NullDatum.get().asTextBytes();
  } else {
    nullChars = nullCharacters.getBytes();
  }

  // projection
  if (targets == null) {
    targets = schema.toArray();
  }

  outTuple = new VTuple(targets.length);

  targetColumnIndexes = new int[targets.length];
  for (int i = 0; i < targets.length; i++) {
    targetColumnIndexes[i] = schema.getColumnId(targets[i].getQualifiedName());
  }
  Arrays.sort(targetColumnIndexes);

  FileSystem fs = fragment.getPath().getFileSystem(conf);
  end = fs.getFileStatus(fragment.getPath()).getLen();
  in = openFile(fs, fragment.getPath(), 4096);
  if (LOG.isDebugEnabled()) {
    LOG.debug("RCFile open:" + fragment.getPath() + "," + start + "," + (endOffset - startOffset) +
        "," + fs.getFileStatus(fragment.getPath()).getLen());
  }
  //init RCFILE Header
  boolean succeed = false;
  try {
    if (start > 0) {
      seek(0);
      initHeader();
    } else {
      initHeader();
    }
    succeed = true;
  } finally {
    if (!succeed) {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          if (LOG != null && LOG.isDebugEnabled()) {
            LOG.debug("Exception in closing " + in, e);
          }
        }
      }
    }
  }

  columnNumber = Integer.parseInt(metadata.get(new Text(COLUMN_NUMBER_METADATA_STR)).toString());
  selectedColumns = new SelectedColumn[targetColumnIndexes.length];
  colValLenBufferReadIn = new NonSyncDataInputBuffer[targetColumnIndexes.length];
  boolean[] skippedColIDs = new boolean[columnNumber];
  Arrays.fill(skippedColIDs, true);
  super.init();

  for (int i = 0; i < targetColumnIndexes.length; i++) {
    int tid = targetColumnIndexes[i];
    SelectedColumn col = new SelectedColumn();
    col.colIndex = tid;
    if (tid < columnNumber) {
      skippedColIDs[tid] = false;
      col.runLength = 0;
      col.prvLength = -1;
      col.rowReadIndex = 0;
      colValLenBufferReadIn[i] = new NonSyncDataInputBuffer();
    }  else {
      col.isNulled = true;
    }
    selectedColumns[i] = col;
  }

  currentKey = createKeyBuffer();
  currentValue = new ValueBuffer(null, columnNumber, targetColumnIndexes, codec, skippedColIDs);

  if (startOffset > getPosition()) {    // TODO use sync cache
    sync(startOffset); // sync to start
  }
}