_content = _content.replaceAll("\\n", " ");
_content = _content.replaceAll("\\t", " ");
//_content = _content.trim();
_cursorRelative = _cursorAbsolute - _documentRegion.getOffset(); //- filteredCharsBeforeCursor;
} catch (BadLocationException e) {
throw new ParseException("Unable to extract content of region.", _documentRegion.getOffset());
}
if (_content.startsWith("</tml:")) {
// stop tag - parse tagname only
_tagName = _content.substring(_content.indexOf("</tml:") + "</tml:".length(), _content.indexOf(">"));
_tagName = _tagName.trim();
} else if (_content.startsWith(TML_REGION_PREFIX)){
// perform start tag parsing
// parse tagname
String tagContent = _content.substring(_content.indexOf(":") + 1);
Set<String> tagNameStopWords = new HashSet<String>();
tagNameStopWords.add(" ");
tagNameStopWords.add("/>");
tagNameStopWords.add(">");
tagNameStopWords.add("\"");
tagNameStopWords.add("<");
//tagNameStopWords.add("\n");
int tagnameEndPos = searchForward(tagNameStopWords, tagContent);
if (tagnameEndPos == -1) {
tagnameEndPos = _cursorRelative - TML_REGION_PREFIX.length();
}
_tagName = tagContent.substring(0, tagnameEndPos).trim();
// parse attributes
int attributesStart = TML_REGION_PREFIX.length() + _tagName.length();
int attributesEnd = _content.length();
if (_content.endsWith("/>")) {
attributesEnd = attributesEnd - 2;
} else if (_content.endsWith(">")) {
attributesEnd = attributesEnd - 1;
}
String attributes = _content.substring(attributesStart, attributesEnd);
attributes = attributes.replaceAll("\n", "");
attributes = attributes.replaceAll("\r", "");
if (attributes.contains("<tml:")) {
// attribute section might contain <tml: start sequence if current tag partion is not closed yet and current tag is followed by another tml tag
// remove this
attributes = attributes.substring(0, attributes.indexOf("<tml:"));
}
attributes = attributes.trim();
if (attributes.length() > 0) {
attributes = WGUtils.strReplace(attributes, "\\\"", "$IG_APO$", true);
attributes = WGUtils.strReplace(attributes, "\"\"", "\"$IG_EMPTY$\"", true);
String[] attributeTokens = attributes.split("\"*\"");
for (int i=0; i < attributeTokens.length; i += 2) {
String key = attributeTokens[i];
boolean valueless = false;
if (!key.contains("=")) {
valueless = true;
}
key = key.replaceAll("=", "");
String value = null;
if (key.contains(" ")) {
String[] keys = key.split(" ");
key = keys[keys.length -1];
key = key.trim();
// all other strings in key array are attributes without = and value - add to attribute list
for (int j=0; j < keys.length - 1; j++) {
String sValueless = keys[j].trim();
if (sValueless.length() > 0) {
_valuelessAttributes.add(sValueless.toLowerCase());
}
}
}
if ((i + 1) < attributeTokens.length) {
value = attributeTokens[i + 1];
}
if (!valueless) {
value = WGUtils.strReplace(value, "$IG_APO$", "\\\"", true);
value = WGUtils.strReplace(value, "$IG_EMPTY$","", true);
_attributes.put(key.toLowerCase(), value);
} else {
_valuelessAttributes.add(key.toLowerCase());
}
}
}
}
} catch (ParseException e) {
throw e;
} catch (Exception e) {
throw new ParseException("Unknown parsing error '" + e.getMessage() + "'", -1);
}
}