本文整理汇总了Java中java.text.StringCharacterIterator.setIndex方法的典型用法代码示例。如果您正苦于以下问题:Java StringCharacterIterator.setIndex方法的具体用法?Java StringCharacterIterator.setIndex怎么用?Java StringCharacterIterator.setIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.StringCharacterIterator
的用法示例。
在下文中一共展示了StringCharacterIterator.setIndex方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: escapeCharFromXML
import java.text.StringCharacterIterator; //导入方法依赖的package包/类
/**
* Escapes characters for text appearing as XML data by HTML tags.
*
* <P>The following characters are replaced with corresponding character entities :
* <table border='1' cellpadding='3' cellspacing='0'>
* <tr><th> Character </th><th> Encoding </th></tr>
* <tr><td> < </td><td> &lt; </td></tr>
* <tr><td> > </td><td> &gt; </td></tr>
* <tr><td> & </td><td> &amp; </td></tr>
* <tr><td> " </td><td> &quot;</td></tr>
* <tr><td> ' </td><td> &#039;</td></tr>
* </table>
*
* <P>Note that JSTL's {@code <c:out>} escapes the exact same set of
* characters as this method. <span class='highlight'>That is, {@code <c:out>}
* is good for escaping to produce valid XML, but not for producing safe HTML.</span>
* see: http://www.javapractices.com/topic/TopicAction.do?Id=96
*/
public static String escapeCharFromXML(String text)
{
if (null == text) {
System.out.println("Error in StringUtil.escapeCharFromXML(), argument is null.");
return NULL_STRING;
}
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(text);
char character = iterator.current();
while (character != StringCharacterIterator.DONE ){
if (character == '&') {
// skip double (error) parsing: < -> &lt;
int save = iterator.getIndex();
String ampersand_tag = isAmpersandTag(text, save);
if(ampersand_tag.length() > 0) {
result.append(ampersand_tag);
iterator.setIndex(save + ampersand_tag.length() - 1);
} else {
result.append("&");
}
} else if (' ' != character && XMLTag.existsGlyph(character)) {
result.append(XMLTag.getHTML(character));
}
else {
//the char is not a special one
//add it to the result as is
result.append(character);
}
character = iterator.next();
}
return result.toString();
}
示例2: isolateInnerPartString
import java.text.StringCharacterIterator; //导入方法依赖的package包/类
/**
* This method will isolate an inner part, if the SCI is
* currently positioned on the opening token of that inner
* part. <br />
* It also allows for nested inner parts!
*
* @param aSCI StringCharacterIterator from which to read the
* inner formula.
* @param aOpener char with the opening token for the inner part.
* @param aCloser char with the closing token for the inner part.
* @param aKeepTokens boolean that indicates whether the tokens
* should be included in the return String.
* @return String with the inner formula.
*/
private String isolateInnerPartString(StringCharacterIterator aSCI, char aOpener, char aCloser, boolean aKeepTokens) {
// The String which we'll return.
String innerFormula = "";
// We will count tokens...
int tokenCount = 1;
// Current character is opening token and can and will be ignored.
char next = aSCI.next();
// This position is also the starting position for our String to be.
int startPosition = aSCI.getIndex();
// This one is derived from the logic within the loop below.
int endPosition = -1;
// Now to count tokens. Opening token adds 1 to the counter,
// closing token subtracts 1. If the counter reaches zero, we've
// found the end of our innerFormula.
while (tokenCount > 0) {
if (next == aOpener) {
// Opening token, add one to counter.
tokenCount++;
} else if (next == aCloser) {
// Closing token, subtract one from counter.
tokenCount--;
}
// Advance one character.
next = aSCI.next();
}
// Okay, end found. We'll have to retrieve it's position, 'though.
// It's position is NOT the current, since that is BEYOND the last
// closing token. It is not one before that, since that would be
// the closing token itself.
// We need the position 2 before the current.
// BTW: this int is to reset to the current position when we're done.
int imPosition = aSCI.getIndex();
endPosition = imPosition - 2;
// Construct the inner formula from the characters starting at
// 'startPosition' and ending with 'endPosition'. Note that both are
// inclusive.
for (int i = startPosition; i <= endPosition; i++) {
// Set the index for the char to retrieve.
aSCI.setIndex(i);
// Get the current char and append it to the String.
innerFormula += Character.valueOf(aSCI.current());
}
// Reset the index on the SCI to the correct endposition (which is
// just after the last closing bracket, btw).
// We've stored that position in 'imPosition'.
aSCI.setIndex(imPosition);
// All done.
return ((aKeepTokens ? Character.valueOf(aOpener) : "")
+ innerFormula + (aKeepTokens ? Character.toString(aCloser) : ""));
}
示例3: checkAIMLPattern
import java.text.StringCharacterIterator; //导入方法依赖的package包/类
public static void checkAIMLPattern(String s, boolean flag)
throws NotAnAIMLPatternException {
StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
boolean flag1 = true;
int j = 1;
for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
int i;
if (!Character.isLetterOrDigit(c)) {
i = 32;
if (Character.isWhitespace(c)) {
i |= 0x10;
if (c == ' ')
i |= 8;
else
throw new NotAnAIMLPatternException("The only allowed whitespace is a space ( ).", s);
}
if (c == '*' || c == '_') {
i |= 2;
if (j != 1 && (j == 4 || (j & 2) == 2))
throw new NotAnAIMLPatternException("A wildcard cannot be preceded by a wildcard, a letter or a digit.", s);
}
if (c == '<') {
int k = stringcharacteriterator.getIndex();
if (s.regionMatches(false, k, "<bot name=\"", 0, 11)) {
stringcharacteriterator.setIndex(k + 11);
for (c = stringcharacteriterator.next(); c != '\uFFFF' && c != '"' && (Character.isLetterOrDigit(c) || c == ' ' || c == '_'); c = stringcharacteriterator.next())
;
k = stringcharacteriterator.getIndex();
if (!s.regionMatches(false, k, "\"/>", 0, 3))
throw new NotAnAIMLPatternException("Invalid or malformed <bot/> element.", s);
stringcharacteriterator.setIndex(k + 3);
} else {
throw new NotAnAIMLPatternException("Invalid or malformed inner element.", s);
}
}
} else {
i = 4;
if (!flag && Character.toUpperCase(c) != c)
throw new NotAnAIMLPatternException("Characters with case mappings must be uppercase.", s);
if (j != 1 && (j & 2) == 2)
throw new NotAnAIMLPatternException("A letter or digit may not be preceded by a wildcard.", s);
}
j = i;
}
}