use of org.sirix.exception.SirixException in project sirix by sirixdb.
the class RESTResponseHelper method buildResponseOfDomLR.
/**
* This method builds the overview for the resources and collection we offer
* in our implementation.
*
* @param storagePath
* path to the storage
* @param availableRes
* a list of available resources or collections
* @return the streaming output for the HTTP response body
*/
public static StreamingOutput buildResponseOfDomLR(final File storagePath, final List<String> availableRes) {
final StreamingOutput sOutput = new StreamingOutput() {
@Override
public void write(final OutputStream output) throws IOException, WebApplicationException {
Document document;
try {
document = createSurroundingXMLResp();
final Element resElement = RESTResponseHelper.createResultElement(document);
List<Element> collections;
try {
collections = RESTResponseHelper.createCollectionElementDBs(storagePath, availableRes, document);
} catch (final SirixException exce) {
throw new WebApplicationException(exce);
}
for (final Element resource : collections) {
resElement.appendChild(resource);
}
document.appendChild(resElement);
final DOMSource domSource = new DOMSource(document);
final StreamResult streamResult = new StreamResult(output);
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(domSource, streamResult);
} catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
throw new WebApplicationException(e);
}
}
};
return sOutput;
}
use of org.sirix.exception.SirixException 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.exception.SirixException in project sirix by sirixdb.
the class TextView method refreshUpdate.
@Override
public void refreshUpdate(final Optional<VisualItemAxis> pAxis) {
checkNotNull(pAxis);
dispose();
mAxis = null;
final Dimension mainFrame = mGUI.getSize();
mText.setSize(new Dimension(mainFrame.width - 16, mainFrame.height - mGUI.getJMenuBar().getHeight() - 37));
if (pAxis.isPresent()) {
mAxis = pAxis.get();
}
mDb = checkNotNull(mGUI).getReadDB();
try {
if (mRtx != null) {
mRtx.close();
}
mRtx = mDb.getSession().beginNodeReadTrx(mDb.getRevisionNumber());
mRtx.moveTo(mDb.getNodeKey());
mText.setText("");
if (mDimension != null) {
mText.setSize(mDimension);
}
mTempValue = 0;
mLineChanges = 0;
serialize();
mText.setCaretPosition(0);
repaint();
} catch (final SirixException e) {
LOGWRAPPER.error(e.getMessage(), e);
}
addAdjustmentListener();
}
use of org.sirix.exception.SirixException 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.exception.SirixException in project sirix by sirixdb.
the class DatabaseRepresentation method getResource.
/**
* This method is responsible to deliver the whole database. Additional
* parameters can be set (wrap, revision, output) which change the response
* view.
*
* @param resourceName
* The name of the requested database.
* @param queryParams
* The optional query parameters.
* @return The XML database resource, depending on the query parameters.
* @throws JaxRxException
* The exception occurred.
*/
public StreamingOutput getResource(final String resourceName, final Map<QueryParameter, String> queryParams) throws JaxRxException {
final StreamingOutput streamingOutput = new StreamingOutput() {
@Override
public void write(final OutputStream output) throws IOException, JaxRxException {
final String revision = queryParams.get(QueryParameter.REVISION);
final String wrap = queryParams.get(QueryParameter.WRAP);
final String nodeId = queryParams.get(QueryParameter.OUTPUT);
final boolean wrapResult = (wrap == null) ? false : wrap.equalsIgnoreCase(YESSTRING);
final boolean nodeid = (nodeId == null) ? false : nodeId.equalsIgnoreCase(YESSTRING);
try {
if (revision == null) {
serialize(resourceName, null, nodeid, output, wrapResult);
} else {
// pattern which have to match against the input
final Pattern pattern = Pattern.compile("[0-9]+[-]{1}[1-9]+");
final Matcher matcher = pattern.matcher(revision);
if (matcher.matches()) {
getModificHistory(resourceName, revision, nodeid, output, wrapResult);
} else {
serialize(resourceName, Integer.valueOf(revision), nodeid, output, wrapResult);
}
}
} catch (final NumberFormatException exce) {
throw new JaxRxException(400, exce.getMessage());
} catch (final SirixException exce) {
throw new JaxRxException(exce);
}
}
};
return streamingOutput;
}
Aggregations