下面列出了org.apache.commons.lang3.StringEscapeUtils#escapeXml ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void writeEditorialDeclaration(SebisStringBuilder sb) {
if (trpDoc.getEdDeclList()==null || trpDoc.getEdDeclList().isEmpty())
return;
sb.incIndent();
sb.addLine("<encodingDesc>");
sb.incIndent();
sb.addLine("<editorialDecl>");
sb.incIndent();
for (EdFeature f : trpDoc.getEdDeclList()) {
if (f.getSelectedOption()!=null) {
String str = f.getTitle()+" ("+f.getDescription()+"): "+f.getSelectedOption().getText();
String escapedstr = StringEscapeUtils.escapeXml(str);
sb.addLine("<p>"+escapedstr+"</p>");
}
}
sb.decIndent();
sb.addLine("</editorialDecl>");
sb.decIndent();
sb.addLine("</encodingDesc>");
sb.decIndent();
}
/**
* Escapes the given shape text for XML and adjusts offset/length values of given tags accordingly
*/
String escapeShapeText(String text, List<CustomTag> tags) {
String escapedText="";
for (int i=0; i<text.length(); ++i) {
char c = text.charAt(i);
String escaped = StringEscapeUtils.escapeXml(""+c);
int indexOfChar = escapedText.length();
escapedText += escaped;
correctTagOffsetsForEscapedString(indexOfChar, escaped, tags);
// // adapt tag indices for the additional characters that may have been inserted due to escape
// if (escaped.length() > 1) {
// logger.trace("escaped = "+escaped+" length = "+escaped.length());
// correctTagOffsetsForTagInsertion(insertOffset, escaped.length(), tags);
// }
}
return escapedText;
}
/**
* Escapes the given shape text for XML and adjusts offset/length values of given tags accordingly
*/
public static String escapeShapeText(String text, List<CssSyntaxTag> tags) {
String escapedText="";
for (int i=0; i<text.length(); ++i) {
char c = text.charAt(i);
String escaped = StringEscapeUtils.escapeXml(""+c);
int indexOfChar = escapedText.length();
escapedText += escaped;
correctTagOffsetsForEscapedString(indexOfChar, escaped, tags);
}
return escapedText;
}
String createTagEnd(CustomTag t) {
String te="";
if (t instanceof TextStyleTag) {
te = "</hi>";
}
else if (t instanceof AbbrevTag) {
te = "</abbr></choice>";
}
else if (t instanceof PersonTag) {
te = "</persName>";
}
else if (t instanceof PlaceTag) {
te = "</placeName>";
}
else if (t instanceof OrganizationTag) {
te = "</orgName>";
}
else if (t instanceof SpeechTag) {
te = "</sp>";
}
else if (t instanceof GapTag) {
te = "";
}
//no comment element in TEI - we use note instead
else if (t instanceof CommentTag){
CommentTag ct = (CommentTag) t;
te = "<note>";
if (!StringUtils.isEmpty(ct.getComment())) {
te += StringEscapeUtils.escapeXml(ct.getComment());
}
te += "</note>";
}
else {
te = "</"+t.getTagName()+">";
}
return te;
}
/**
* Experimental - will attempt to escape all text within xml elements
* @param xml full xml
* @return escaped xml string
*/
public static String escapeAllXml(String xml)
{
// Match the pattern <something>text</something>
Pattern xmlCleanerPattern = Pattern.compile("(<[^/<>]*>)([^<>]*)(</[^<>]*>)");
StringBuilder xmlStringBuilder = new StringBuilder();
Matcher matcher = xmlCleanerPattern.matcher(xml);
int lastEnd = 0;
while (matcher.find())
{
// Include any non-matching text between this result and the previous result
if (matcher.start() > lastEnd) {
xmlStringBuilder.append(xml.substring(lastEnd, matcher.start()));
}
lastEnd = matcher.end();
// Sanitise the characters inside the tags and append the sanitised version
String cleanText = StringEscapeUtils.escapeXml(matcher.group(2));
xmlStringBuilder.append(matcher.group(1)).append(cleanText).append(matcher.group(3));
}
// Include any leftover text after the last result
xmlStringBuilder.append(xml.substring(lastEnd));
return xmlStringBuilder.toString();
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
public static String generateSummary(String content) {
String summary = StringEscapeUtils.escapeXml(content);
summary = StringUtils.substring(summary, 0, 50);
return summary;
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
@Test
public void escape_xml_with_apache_commons () {
String escapedXML = StringEscapeUtils.escapeXml(XML_TO_ESCAPE);
assertEquals(ESCAPED_XML, escapedXML);
}
@SuppressWarnings("deprecation")
public static String envelopeStringIntoHtml(String text) {
return "<html><body>" + StringEscapeUtils.escapeXml(text) + "</body></html>";
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 转码.
*/
@SuppressWarnings("deprecation")
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
private String escape(String input) {
return StringEscapeUtils.escapeXml(input);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
public void setMessageText(ApiVersion apiVersion, String text) {
//Lets make sure we comply with XML
text = StringEscapeUtils.escapeXml(text);
if (apiVersion != null && !apiVersion.equals(ApiVersion.V2)) {
setMessage(MLTypes.START_PML + text + MLTypes.END_PML);
} else {
setMessageType("MESSAGEML");
setMessage(MLTypes.START_ML + text + MLTypes.END_ML);
}
}
/**
* Escapes and formats text
* @param text unformatted text
* @param inParagraph true if inside a paragraph tag
* @return
*/
public static String formatEscapeHTML(String text, boolean inParagraph) {
String retval = StringEscapeUtils.escapeXml(text);
return addHtmlFormatting(retval, inParagraph);
}