use of org.sirix.gui.GUIProp in project sirix by sirixdb.
the class TextView method processStAX.
/**
* Process StAX output.
*
* @param pState
* {@link State} enum, which determines if an initial or update of
* the view occurs.
* @throws XMLStreamException
* if any parsing exception occurs
* @throws BadLocationException
* if inserting strings into the {@link JTextPane} fails
*/
private void processStAX(final State pState) throws XMLStreamException, BadLocationException {
assert pState != null;
final GUIProp prop = new GUIProp();
final int indent = prop.getIndentSpaces();
final StringBuilder spaces = new StringBuilder();
for (int i = 0; i < indent; i++) {
spaces.append(" ");
}
final String indentSpaces = spaces.toString();
// Style document.
final StyledDocument doc = (StyledDocument) mText.getDocument();
final Style styleElements = doc.addStyle("elements", null);
StyleConstants.setForeground(styleElements, ELEMENT_COLOR);
final Style styleNamespaces = doc.addStyle("namespaces", null);
StyleConstants.setForeground(styleNamespaces, NAMESPACE_COLOR);
final Style styleAttributes = doc.addStyle("attributes", null);
StyleConstants.setForeground(styleAttributes, ATTRIBUTE_COLOR);
final Style styleText = doc.addStyle("text", null);
StyleConstants.setForeground(styleText, TEXT_COLOR);
final Style[] styles = new Style[] { styleElements, styleNamespaces, styleAttributes, styleText };
boolean doIndent = true;
assert mSerializer != null;
switch(pState) {
case INITIAL:
// Initialize variables.
final int lineHeight = mText.getFontMetrics(this.getFont()).getHeight();
final int frameHeight = mText.getHeight();
int level = -1;
long height = 0;
while (mSerializer.hasNext() && height < frameHeight) {
final XMLEvent event = mSerializer.nextEvent();
if (mSerializer instanceof StAXDiffSerializer) {
style(((StAXDiffSerializer) mSerializer).getDiff(), styles);
}
switch(event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
doIndent = true;
break;
case XMLStreamConstants.END_DOCUMENT:
if (mTempSerializer != null) {
mSerializer = mTempSerializer;
mTempSerializer = null;
}
break;
case XMLStreamConstants.START_ELEMENT:
level++;
if (mSerializer instanceof StAXDiffSerializer) {
if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
if (doIndent) {
doIndent = false;
} else {
if (!mTempEmptyElement) {
level--;
}
doIndent = true;
}
} else {
doIndent = true;
}
}
indent(doc, level, indentSpaces);
mEmptyElement = processStartTag(event, doc);
if (mSerializer instanceof StAXDiffSerializer && ((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
mTempEmptyElement = mEmptyElement;
} else {
mTempEmptyElement = false;
}
height += lineHeight;
break;
case XMLStreamConstants.END_ELEMENT:
if (mSerializer instanceof StAXDiffSerializer) {
if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
if (doIndent) {
doIndent = false;
} else {
if (!mEmptyElement) {
level++;
}
doIndent = true;
}
} else {
doIndent = true;
}
}
if (mEmptyElement) {
mEmptyElement = false;
} else {
processEndTag(event, level, indentSpaces, doc, styleElements);
}
level--;
break;
case XMLStreamConstants.CHARACTERS:
doIndent = true;
mTempEmptyElement = false;
level++;
indent(doc, level, indentSpaces);
level--;
doc.insertString(doc.getLength(), event.asCharacters().getData() + NEWLINE, styleText);
height += lineHeight;
break;
default:
}
}
mTempLevel = level;
mDimension = mText.getSize();
break;
case UPDATE:
for (int i = 0; i < mLineChanges && mSerializer.hasNext(); i++) {
final XMLEvent event = mSerializer.nextEvent();
if (mSerializer instanceof StAXDiffSerializer) {
style(((StAXDiffSerializer) mSerializer).getDiff(), styles);
}
switch(event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
doIndent = true;
break;
case XMLStreamConstants.END_DOCUMENT:
if (mTempSerializer != null) {
mSerializer = mTempSerializer;
mTempSerializer = null;
}
break;
case XMLStreamConstants.START_ELEMENT:
mTempLevel++;
if (mSerializer instanceof StAXDiffSerializer) {
if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
if (doIndent) {
doIndent = false;
} else {
if (!mTempEmptyElement) {
mTempLevel--;
}
doIndent = true;
}
} else {
doIndent = true;
}
}
mTempEmptyElement = false;
indent(doc, mTempLevel, indentSpaces);
mEmptyElement = processStartTag(event, doc);
if (mSerializer instanceof StAXDiffSerializer && ((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
mTempEmptyElement = mEmptyElement;
} else {
mTempEmptyElement = false;
}
LOGWRAPPER.debug("start elem: " + event.asStartElement().getName().getLocalPart() + " " + mEmptyElement);
break;
case XMLStreamConstants.END_ELEMENT:
if (mSerializer instanceof StAXDiffSerializer) {
if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
if (doIndent) {
doIndent = false;
} else {
if (!mTempEmptyElement) {
mTempLevel++;
}
doIndent = true;
}
} else {
doIndent = true;
}
}
if (mEmptyElement) {
mEmptyElement = false;
} else {
processEndTag(event, mTempLevel, indentSpaces, doc, styleElements);
}
mTempLevel--;
break;
case XMLStreamConstants.CHARACTERS:
doIndent = true;
mTempEmptyElement = false;
mTempLevel++;
indent(doc, mTempLevel, indentSpaces);
mTempLevel--;
doc.insertString(doc.getLength(), event.asCharacters().getData() + NEWLINE, styleText);
LOGWRAPPER.debug("characters: " + event.asCharacters().getData());
break;
default:
}
}
break;
default:
}
}
Aggregations