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

The following examples show how to use org.apache.commons.text.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: PathsHelper.java    From azure-cosmosdb-java with MIT License 6 votes vote down vote up
public static PathInfo parseNameSegments(String resourceUrl, String[] segments) {
    if (segments == null || segments.length < 1) {
        return null;
    }

    if (segments.length % 2 == 0) {
        // even number, assume it is individual resource
        if (isResourceType(segments[segments.length - 2])) {
            return new PathInfo(false, segments[segments.length - 2],
                    StringEscapeUtils.unescapeJava(StringUtils.strip(resourceUrl, Paths.ROOT)), true);
        }
    } else {
        // odd number, assume it is feed request
        if (isResourceType(segments[segments.length - 1])) {
            return new PathInfo(true, segments[segments.length - 1],
                    StringEscapeUtils.unescapeJava(StringUtils.strip(
                            resourceUrl.substring(0,
                                    StringUtils.removeEnd(resourceUrl, Paths.ROOT).lastIndexOf(Paths.ROOT)),
                            Paths.ROOT)),
                    true);
        }
    }

    return null;
}
 
Example 2
Source File: ThreadSearhDomain.java    From domain_hunter with MIT License 5 votes vote down vote up
public static Set<String> grepDomain(String httpResponse) {
	Set<String> domains = new HashSet<>();
	//"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$"
	final String DOMAIN_NAME_PATTERN = "([A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}";
	int counter =0;
	while (httpResponse.contains("&#x") && counter<3) {// &#x html编码的特征
		httpResponse = StringEscapeUtils.unescapeHtml4(httpResponse);
		counter++;
	}
	
	counter = 0;
	while (httpResponse.contains("%") && counter<3) {// %对应的URL编码
		httpResponse = URLDecoder.decode(httpResponse);
		counter++;
	}
	
	counter = 0;
	while (httpResponse.contains("\\u00") && counter<3) {//unicode解码
		httpResponse = StringEscapeUtils.unescapeJava(httpResponse);
		counter++;
	}
	
	Pattern pDomainNameOnly = Pattern.compile(DOMAIN_NAME_PATTERN);
	Matcher matcher = pDomainNameOnly.matcher(httpResponse);
	while (matcher.find()) {//多次查找
		domains.add(matcher.group());
	}
	return domains;
}
 
Example 3
Source File: StoryProvider.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public List<MTGStory> next() throws IOException {
	String url = baseURI + "/" + local.getLanguage() + "/section-articles-see-more-ajax?l=" + "en"+ "&sort=DESC&f=13961&offset=" + (offset++);
	List<MTGStory> list = new ArrayList<>();
	JsonElement el = URLTools.extractJson(url);
	JsonArray arr = el.getAsJsonObject().get("data").getAsJsonArray();

	for (int i = 0; i < arr.size(); i++) {
		JsonElement e = arr.get(i);
		String finale = StringEscapeUtils.unescapeJava(e.toString());
		Document d = Jsoup.parse(finale);
		
			MTGStory story = new MTGStory();
			story.setTitle(d.select("div.title h3").html());
			story.setAuthor(StringEscapeUtils.unescapeHtml3(d.select("span.author").html()));
			story.setDescription(StringEscapeUtils.unescapeHtml3(d.select("div.description").html()));
			story.setUrl(new URL(baseURI + d.select("a").first().attr("href")));
			story.setDate(d.select("span.date").text());
		try {
			String bgImage = d.select("div.image").attr("style");
			story.setIcon(loadPics(new URL(bgImage.substring(bgImage.indexOf("url(") + 5, bgImage.indexOf("');")))));
			
		} catch (Exception e2) {
			logger.error("Error loading story ", e2);
		}
		list.add(story);
	}

	return list;
}
 
Example 4
Source File: CSVProperties.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
private static String unescapeJava(String str) {
  // StringEscapeUtils removes the single escape character
  if (str == "\\") {
    return str;
  }
  return StringEscapeUtils.unescapeJava(str);
}
 
Example 5
Source File: DafCSVCleansing.java    From daf-kylo with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    ComponentLog logger = getLogger();

    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    String separatorString = context.getProperty(SEPARATOR_CHAR).evaluateAttributeExpressions(flowFile).getValue();
    String quoteString = context.getProperty(QUOTE_CHAR).evaluateAttributeExpressions(flowFile).getValue();
    String escapeString = context.getProperty(ESCAPE_CHAR).evaluateAttributeExpressions(flowFile).getValue();

    if (StringUtils.startsWith(separatorString, "\\")) {
        separatorString = StringEscapeUtils.unescapeJava(separatorString);
    }

    if (StringUtils.startsWith(quoteString, "\\")) {
        quoteString = StringEscapeUtils.unescapeJava(quoteString);
    }

    if (StringUtils.startsWith(escapeString, "\\")) {
        escapeString = StringEscapeUtils.unescapeJava(escapeString);
    }

    char separatorChar = separatorString.charAt(0);
    char quoteChar = quoteString.charAt(0);
    char escapeChar = escapeString.charAt(0);

    CSVParser parser = new CSVParserBuilder().withSeparator(separatorChar).withQuoteChar(quoteChar).withEscapeChar(escapeChar).build();
    StopWatch stopWatch = new StopWatch(true);

    try {
        flowFile = session.write(flowFile, (in, out) -> {
            CSVReader reader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(in))).withCSVParser(parser).build();
            CSVWriter writer = new CSVWriter(new BufferedWriter(new OutputStreamWriter(out)), separatorChar, quoteChar, escapeChar, CSVWriter.DEFAULT_LINE_END);

            reader.forEach(row -> {
                String[] cleanLine = new String[row.length];

                for (int i = 0; i < row.length; i++) {
                    cleanLine[i] = StringUtils.replaceAll(row[i], "\r\n|\n|\r", " ");
                }
                writer.writeNext(cleanLine);
            });

            writer.close();
        });
    } catch (Exception ex) {
        logger.error("Error CSV processing", ex);
        logger.info("Transferred {} to 'failure'", new Object[]{flowFile});
        session.getProvenanceReporter().modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    logger.info("Transferred {} to 'success'", new Object[]{flowFile});
    session.getProvenanceReporter().modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
    session.transfer(flowFile, REL_SUCCESS);
}
 
Example 6
Source File: Helper.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
public static String replaceUnicode(@NonNull final String s) {
    return StringEscapeUtils.unescapeJava(s);
}
 
Example 7
Source File: CockroachdbDialect.java    From sqlg with MIT License 4 votes vote down vote up
@Override
public Object convertArray(PropertyType propertyType, Array array) throws SQLException {
    switch (propertyType.ordinal()) {
        case BOOLEAN_ARRAY_ORDINAL:
            return array.getArray();
        case boolean_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectArrayToBooleanPrimitiveArray((Object[]) array.getArray());
        case SHORT_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfIntegersArrayToShortArray((Object[]) array.getArray());
        case short_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfIntegersArrayToShortPrimitiveArray((Object[]) array.getArray());
        case INTEGER_ARRAY_ORDINAL:
            return array.getArray();
        case int_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfIntegersArrayToIntegerPrimitiveArray((Object[]) array.getArray());
        case LONG_ARRAY_ORDINAL:
            return array.getArray();
        case long_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfLongsArrayToLongPrimitiveArray((Object[]) array.getArray());
        case DOUBLE_ARRAY_ORDINAL:
            return array.getArray();
        case double_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfDoublesArrayToDoublePrimitiveArray((Object[]) array.getArray());
        case FLOAT_ARRAY_ORDINAL:
            return array.getArray();
        case float_ARRAY_ORDINAL:
            return SqlgUtil.convertObjectOfFloatsArrayToFloatPrimitiveArray((Object[]) array.getArray());
        case STRING_ARRAY_ORDINAL:
            return array.getArray();
        case LOCALDATETIME_ARRAY_ORDINAL:
            Timestamp[] timestamps = (Timestamp[]) array.getArray();
            return SqlgUtil.copyToLocalDateTime(timestamps, new LocalDateTime[timestamps.length]);
        case LOCALDATE_ARRAY_ORDINAL:
            Date[] dates = (Date[]) array.getArray();
            return SqlgUtil.copyToLocalDate(dates, new LocalDate[dates.length]);
        case LOCALTIME_ARRAY_ORDINAL:
            Time[] times = (Time[]) array.getArray();
            return SqlgUtil.copyToLocalTime(times, new LocalTime[times.length]);
        case JSON_ARRAY_ORDINAL:
            String arrayAsString = array.toString();
            //remove the wrapping curly brackets
            arrayAsString = arrayAsString.substring(1);
            arrayAsString = arrayAsString.substring(0, arrayAsString.length() - 1);
            arrayAsString = StringEscapeUtils.unescapeJava(arrayAsString);
            //remove the wrapping qoutes
            arrayAsString = arrayAsString.substring(1);
            arrayAsString = arrayAsString.substring(0, arrayAsString.length() - 1);
            String[] jsons = arrayAsString.split("\",\"");
            JsonNode[] jsonNodes = new JsonNode[jsons.length];
            ObjectMapper objectMapper = new ObjectMapper();
            int count = 0;
            for (String json : jsons) {
                try {
                    JsonNode jsonNode = objectMapper.readTree(json);
                    jsonNodes[count++] = jsonNode;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return jsonNodes;
        default:
            throw new IllegalStateException("Unhandled property type " + propertyType.name());
    }
}
 
Example 8
Source File: AppenderTools.java    From TNT4J with Apache License 2.0 4 votes vote down vote up
/**
 * Parse a given message into a map of key/value pairs. Tags are identified by '#key=value .. #keyN=value' sequence.
 * String values should be enclosed in single quotes.
 *
 * @param tags
 *            a set of name/value pairs
 * @param msg
 *            string message to be parsed
 * @param delm
 *            tag eye catcher
 * @return a map of parsed name/value pairs from a given string message.
 */
public static Map<String, String> parseEventMessage(Map<String, String> tags, String msg, char delm) {
	int curPos = 0;
	while (curPos < msg.length()) {
		if (msg.charAt(curPos) != delm) {
			curPos++;
			continue;
		}

		int start = ++curPos;
		boolean inValue = false;
		boolean quotedValue = false;
		while (curPos < msg.length()) {
			char c = msg.charAt(curPos);
			if (c == '=') {
				inValue = true;
			} else if (c == '\'' && inValue) {
				// the double quote we just read was not escaped, so include it in value
				if (quotedValue) {
					// found closing quote
					curPos++;
					break;
				} else {
					quotedValue = true;
				}
			} else if (Character.isWhitespace(c) && !quotedValue) {
				break;
			}
			curPos++;
		}

		if (curPos > start) {
			String[] curTag = msg.substring(start, curPos).split("=");
			String name = curTag[0].trim().replace("'", "");
			String value = (curTag.length > 1 ? curTag[1] : "");
			if (value.startsWith("'") && value.endsWith("'")) {
				value = StringEscapeUtils.unescapeJava(value.substring(1, value.length() - 1));
			}
			tags.put(name, value);
		}
	}
	return tags;
}
 
Example 9
Source File: ConvertCSVToAvro.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static String unescapeString(String input) {
    if (input.length() > 1) {
        input = StringEscapeUtils.unescapeJava(input);
    }
    return input;
}
 
Example 10
Source File: InferAvroSchema.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static String unescapeString(String input) {
    if (input.length() > 1) {
        input = StringEscapeUtils.unescapeJava(input);
    }
    return input;
}
 
Example 11
Source File: CSVUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
public static String unescapeJava(String input) {
    if (input != null && input.length() > 1) {
        input = StringEscapeUtils.unescapeJava(input);
    }
    return input;
}
 
Example 12
Source File: StringUtility.java    From jstarcraft-core with Apache License 2.0 2 votes vote down vote up
/**
 * 对字符串执行Java解密
 * 
 * @param string
 * @return
 */
public static final String unescapeJava(String string) {
    return StringEscapeUtils.unescapeJava(string);
}
 
Example 13
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Performs unescaping on the given property name.
 *
 * @param name the property name
 * @return the unescaped property name
 * @since 2.4
 */
protected String unescapePropertyName(final String name)
{
    return StringEscapeUtils.unescapeJava(name);
}
 
Example 14
Source File: StringUtils.java    From Rel with Apache License 2.0 votes vote down vote up
/** Unquote the given string and replace escape sequences by the
    original characters.
 
@param s the string to be unquoted.
@return an unquoted string. */
public static String unquote(String s) {
	return StringEscapeUtils.unescapeJava(s);
}