int ampersandIndex = s.indexOf(AMPERSAND);
if (ampersandIndex == -1)
{
if (s.length() > 0)
{
fragment = new SourceFragment(s, location);
fragmentList.add(fragment);
}
}
else
{
// This variable will keep track of where we find the semicolon
// that ends the entity. By initializing it to the position
// before the beginning of the string, we can avoid having
// special logic to pick up the part of the string before
// the first entity; it becomes the same logic as picking up
// the part of the string between the first semicolon and the
// second ampersand.
int semicolonIndex = -1;
int start = location.getStart();
int line = location.getLine();
int column = location.getColumn();
while (true)
{
// We've found an ampersand.
// Build a fragment containing the text from the previous
// semicolon (or the beginning) to the this ampersand.
if (ampersandIndex > semicolonIndex + 1)
{
String text = s.substring(semicolonIndex + 1, ampersandIndex);
fragment = new SourceFragment(text, text,
start + semicolonIndex + 1, line, column + semicolonIndex + 1);
fragmentList.add(fragment);
}
// Since we found an ampersand that starts an entity,
// look for a subsequent semicolon that ends it.
// If it doesn't exist, report a problem.
semicolonIndex = s.indexOf(SEMICOLON, ampersandIndex + 1);
if (semicolonIndex == -1)
{
ICompilerProblem problem = new MXMLUnterminatedEntityProblem(location);
problems.add(problem);
break; // we can't do any further processing
}
// Extract and convert the entity between the ampersand and the semicolon.
String physicalText = s.substring(ampersandIndex, semicolonIndex + 1);
String entityName = s.substring(ampersandIndex + 1, semicolonIndex);
int c = convertEntity(entityName, mxmlDialect);
if (c == -1)
{
// If it doesn't convert to a character, create a problem and return null.
ICompilerProblem problem = new MXMLInvalidEntityProblem(location, physicalText);
problems.add(problem);
}
else
{
// If it does convert, add a fragment for the entity.
String logicalText = String.copyValueOf(new char[] { (char)c });
fragment = new SourceFragment(physicalText, logicalText,
start + ampersandIndex, line, column + ampersandIndex);
fragmentList.add(fragment);
}
// Find the next ampersand after the semicolon.
ampersandIndex = s.indexOf(AMPERSAND, semicolonIndex + 1);
// If there isn't one, we're done.
// Add a final fragment for the text after the last semicolon.
if (ampersandIndex == -1)
{
if (semicolonIndex + 1 < s.length())
{
String text = s.substring(semicolonIndex + 1);
fragment = new SourceFragment(text, text,
start + semicolonIndex + 1, line, column + semicolonIndex + 1);
fragmentList.add(fragment);
}
break;
}