use of org.sirix.api.NodeReadTrx 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.api.NodeReadTrx in project sirix by sirixdb.
the class DatabaseRepresentation method getLastRevision.
/**
* This method reads the existing database, and offers the last revision id of
* the database
*
* @param resourceName
* The name of the existing database.
* @return The {@link OutputStream} containing the result
* @throws WebApplicationException
* The Exception occurred.
* @throws SirixException
*/
public long getLastRevision(final String resourceName) throws JaxRxException, SirixException {
long lastRevision;
if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
Database database = Databases.openDatabase(mStoragePath);
NodeReadTrx rtx = null;
Session session = null;
try {
session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
lastRevision = session.getMostRecentRevisionNumber();
} catch (final Exception globExcep) {
throw new JaxRxException(globExcep);
} finally {
WorkerHelper.closeRTX(rtx, session, database);
}
} else {
throw new JaxRxException(404, "Resource not found");
}
return lastRevision;
}
use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.
the class NodeIdRepresentation method serializeAT.
/**
* This method serializes requested resource with an access type.
*
* @param resource
* The requested resource
* @param nodeId
* The node id of the requested resource.
* @param revision
* The revision of the requested resource.
* @param doNodeId
* Specifies whether the node id's have to be shown in the result.
* @param output
* The output stream to be written.
* @param wrapResult
* Specifies whether the result has to be wrapped with a result
* element.
* @param accessType
* The {@link IDAccessType} which indicates the access to a special
* node.
*/
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
Session session = null;
Database database = null;
NodeReadTrx rtx = null;
try {
database = Databases.openDatabase(mStoragePath);
session = database.getSession(new SessionConfiguration.Builder(resource).build());
if (revision == null) {
rtx = session.beginNodeReadTrx();
} else {
rtx = session.beginNodeReadTrx(revision);
}
if (rtx.moveTo(nodeId).hasMoved()) {
switch(accessType) {
case FIRSTCHILD:
if (!rtx.moveToFirstChild().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
case LASTCHILD:
if (rtx.moveToFirstChild().hasMoved()) {
long last = rtx.getNodeKey();
while (rtx.moveToRightSibling().hasMoved()) {
last = rtx.getNodeKey();
}
rtx.moveTo(last);
} else {
throw new JaxRxException(404, NOTFOUND);
}
break;
case RIGHTSIBLING:
if (!rtx.moveToRightSibling().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
case LEFTSIBLING:
if (!rtx.moveToLeftSibling().hasMoved())
throw new JaxRxException(404, NOTFOUND);
break;
// nothing to do;
default:
}
if (wrapResult) {
output.write(BEGINRESULT);
final XMLSerializerProperties props = new XMLSerializerProperties();
final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
if (doNodeId) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
output.write(ENDRESULT);
} else {
final XMLSerializerProperties props = new XMLSerializerProperties();
final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), output, props);
if (doNodeId) {
builder.emitRESTful().emitIDs();
}
final XMLSerializer serializer = builder.build();
serializer.call();
}
} else {
throw new JaxRxException(404, NOTFOUND);
}
} catch (final SirixException ttExcep) {
throw new JaxRxException(ttExcep);
} catch (final IOException ioExcep) {
throw new JaxRxException(ioExcep);
} catch (final Exception globExcep) {
if (globExcep instanceof JaxRxException) {
// types
throw (JaxRxException) globExcep;
} else {
throw new JaxRxException(globExcep);
}
} finally {
try {
WorkerHelper.closeRTX(rtx, session, database);
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
} else {
throw new JaxRxException(404, "Resource does not exist");
}
}
use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.
the class DocumentWrapper method selectID.
@Override
public NodeInfo selectID(final String ID, final boolean getParent) {
try {
final NodeReadTrx rtx = mSession.beginNodeReadTrx();
final Axis axis = new DescendantAxis(rtx, IncludeSelf.YES);
while (axis.hasNext()) {
if (rtx.getKind() == Kind.ELEMENT) {
final int attCount = rtx.getAttributeCount();
if (attCount > 0) {
final long nodeKey = rtx.getNodeKey();
for (int index = 0; index < attCount; index++) {
rtx.moveToAttribute(index);
if ("xml:id".equalsIgnoreCase(rtx.getName().getLocalName()) && ID.equals(rtx.getValue())) {
if (getParent) {
rtx.moveToParent();
}
return new NodeWrapper(this, rtx.getNodeKey());
}
rtx.moveTo(nodeKey);
}
}
}
axis.next();
}
rtx.close();
} catch (final SirixException e) {
LOGWRAPPER.error(e.getMessage(), e);
}
return null;
}
use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.
the class NodeWrapper method getParent.
@Override
public NodeInfo getParent() {
try {
NodeInfo parent = null;
final NodeReadTrx rtx = createRtxAndMove();
if (rtx.hasParent()) {
// Parent transaction.
parent = new NodeWrapper(mDocWrapper, rtx.getParentKey());
}
rtx.close();
return parent;
} catch (final SirixException e) {
LOGGER.error(e.getMessage(), e);
return null;
}
}
Aggregations