use of org.sirix.service.xml.serialize.StAXSerializer in project sirix by sirixdb.
the class TextView method serialize.
/**
* Serialize a tree.
*/
private void serialize() {
final NodeReadTrx rtx = mRtx;
// 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("attributes", 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 long nodeKey = rtx.getNodeKey();
try {
switch(rtx.getKind()) {
case DOCUMENT:
case ELEMENT:
mText.setText("");
if (mAxis == null) {
mSerializer = new StAXSerializer(rtx);
} else {
mSerializer = new StAXDiffSerializer(new DiffAxis(IncludeSelf.YES, mDb.getSession().beginNodeReadTrx(mDb.getCompareRevisionNumber()), rtx, mAxis));
}
processStAX(State.INITIAL);
break;
case TEXT:
rtx.moveTo(nodeKey);
mText.setText("");
doc.insertString(doc.getLength(), new String(rtx.getRawValue()), styleText);
break;
case NAMESPACE:
// Move transaction to parent of given namespace node.
rtx.moveToParent();
mText.setText("");
final long nNodeKey = rtx.getNodeKey();
for (int i = 0, namespCount = rtx.getNamespaceCount(); i < namespCount; i++) {
rtx.moveToNamespace(i);
if (rtx.getNodeKey() == nodeKey) {
break;
}
rtx.moveTo(nNodeKey);
}
if (rtx.nameForKey(rtx.getPrefixKey()).length() == 0) {
doc.insertString(doc.getLength(), new StringBuilder().append("xmlns='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
} else {
doc.insertString(doc.getLength(), new StringBuilder().append("xmlns:").append(rtx.nameForKey(rtx.getPrefixKey())).append("='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
}
break;
case ATTRIBUTE:
// Move transaction to parent of given attribute node.
rtx.moveToParent();
mText.setText("");
final long aNodeKey = rtx.getNodeKey();
for (int i = 0, attsCount = rtx.getAttributeCount(); i < attsCount; i++) {
rtx.moveToAttribute(i);
if (rtx.getNodeKey() == nodeKey) {
break;
}
rtx.moveTo(aNodeKey);
}
// Display value.
final String attPrefix = rtx.getName().getPrefix();
final QNm attQName = rtx.getName();
if (attPrefix == null || attPrefix.isEmpty()) {
doc.insertString(doc.getLength(), new StringBuilder().append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
} else {
doc.insertString(doc.getLength(), new StringBuilder().append(attPrefix).append(":").append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
}
break;
default:
throw new IllegalStateException("Node kind not known!");
}
} catch (final SirixException | BadLocationException | XMLStreamException e) {
LOGWRAPPER.error(e.getMessage(), e);
}
}
use of org.sirix.service.xml.serialize.StAXSerializer in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method copy.
/**
* Helper method for copy-operations.
*
* @param rtx the source {@link XdmNodeReadTrx}
* @param insert the insertion strategy
* @throws SirixException if anything fails in sirix
*/
private void copy(final XdmNodeReadTrx trx, final Insert insert) throws SirixException {
assert trx != null;
assert insert != null;
final XdmNodeReadTrx rtx = trx.getResourceManager().beginNodeReadTrx(trx.getRevisionNumber());
assert rtx.getRevisionNumber() == trx.getRevisionNumber();
rtx.moveTo(trx.getNodeKey());
assert rtx.getNodeKey() == trx.getNodeKey();
if (rtx.getKind() == Kind.DOCUMENT) {
rtx.moveToFirstChild();
}
if (!(rtx.isStructuralNode())) {
throw new IllegalStateException("Node to insert must be a structural node (Text, PI, Comment, Document root or Element)!");
}
final Kind kind = rtx.getKind();
switch(kind) {
case TEXT:
final String textValue = rtx.getValue();
switch(insert) {
case ASFIRSTCHILD:
insertTextAsFirstChild(textValue);
break;
case ASLEFTSIBLING:
insertTextAsLeftSibling(textValue);
break;
case ASRIGHTSIBLING:
insertTextAsRightSibling(textValue);
break;
default:
throw new IllegalStateException();
}
break;
case PROCESSING_INSTRUCTION:
switch(insert) {
case ASFIRSTCHILD:
insertPIAsFirstChild(rtx.getName().getLocalName(), rtx.getValue());
break;
case ASLEFTSIBLING:
insertPIAsLeftSibling(rtx.getName().getLocalName(), rtx.getValue());
break;
case ASRIGHTSIBLING:
insertPIAsRightSibling(rtx.getName().getLocalName(), rtx.getValue());
break;
default:
throw new IllegalStateException();
}
break;
case COMMENT:
final String commentValue = rtx.getValue();
switch(insert) {
case ASFIRSTCHILD:
insertCommentAsFirstChild(commentValue);
break;
case ASLEFTSIBLING:
insertCommentAsLeftSibling(commentValue);
break;
case ASRIGHTSIBLING:
insertCommentAsRightSibling(commentValue);
break;
default:
throw new IllegalStateException();
}
break;
default:
new XMLShredder.Builder(this, new StAXSerializer(rtx), insert).build().call();
}
rtx.close();
}
Aggregations