use of org.exist.EXistException in project exist by eXist-db.
the class ElementImpl method replaceChild.
/**
* Replaces the oldNode with the newChild
*
* @param transaction the transaction
* @param newChild to replace oldChild
* @param oldChild to be replace by newChild
* @return The new node (this differs from the {@link org.w3c.dom.Node#replaceChild(Node, Node)} specification)
* @throws DOMException in case of a DOM error
* @see org.w3c.dom.Node#replaceChild(org.w3c.dom.Node, org.w3c.dom.Node)
*/
@Override
public Node replaceChild(final Txn transaction, final Node newChild, final Node oldChild) throws DOMException {
if (!(oldChild instanceof IStoredNode)) {
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
}
final IStoredNode<?> oldNode = (IStoredNode<?>) oldChild;
if (!oldNode.getNodeId().getParentId().equals(nodeId)) {
throw new DOMException(DOMException.NOT_FOUND_ERR, "Node is not a child of this element");
}
final NodePath thisPath = getPath();
IStoredNode<?> previous = (IStoredNode<?>) oldNode.getPreviousSibling();
if (previous == null) {
previous = this;
} else {
previous = getLastNode(previous);
}
final NodePath oldPath = oldNode.getPath();
StreamListener listener = null;
Node newNode = null;
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
final IndexController indexes = broker.getIndexController();
// May help getReindexRoot() to make some useful things
indexes.setDocument(ownerDocument);
final IStoredNode reindexRoot = broker.getIndexController().getReindexRoot(oldNode, oldPath, false);
indexes.setMode(ReindexMode.REMOVE_SOME_NODES);
if (reindexRoot == null) {
listener = indexes.getStreamListener();
} else {
indexes.reindex(transaction, reindexRoot, ReindexMode.REMOVE_SOME_NODES);
}
broker.removeAllNodes(transaction, oldNode, oldPath, listener);
broker.endRemove(transaction);
broker.flush();
indexes.setMode(ReindexMode.STORE);
listener = indexes.getStreamListener();
newNode = appendChild(transaction, oldNode.getNodeId(), new NodeImplRef(previous), thisPath, newChild, listener);
// Reindex if required
broker.storeXMLResource(transaction, getOwnerDocument());
broker.updateNode(transaction, this, false);
indexes.reindex(transaction, reindexRoot, ReindexMode.STORE);
broker.flush();
} catch (final EXistException e) {
LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
}
// returning the newNode is more sensible than returning the oldNode
return newNode;
}
use of org.exist.EXistException in project exist by eXist-db.
the class ElementImpl method setAttribute.
private void setAttribute(final QName attrName, final String value, final Function<QName, Attr> getFn) {
final Attr existingAttr = getFn.apply(attrName);
if (existingAttr != null) {
// update an existing attribute
existingAttr.setValue(value);
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
if (!(existingAttr instanceof IStoredNode)) {
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
}
final IStoredNode<?> existing = (IStoredNode<?>) existingAttr;
if (!existing.getNodeId().isChildOf(nodeId)) {
throw new DOMException(DOMException.NOT_FOUND_ERR, "node " + existing.getNodeId().getParentId() + " is not a child of element " + nodeId);
}
// update old custom indexes
final IndexController indexes = broker.getIndexController();
indexes.reindex(transaction, existing, ReindexMode.STORE);
broker.updateNode(transaction, existing, true);
transaction.commit();
} catch (final EXistException e) {
LOG.error(e);
throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
}
} else {
try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
final AttrImpl attrib = new AttrImpl(attrName, value, broker.getBrokerPool().getSymbols());
appendChild(attrib);
} catch (final EXistException e) {
LOG.error(e);
throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
}
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class Launcher method registerObserver.
private void registerObserver() {
try {
final BrokerPool pool = BrokerPool.getInstance();
final Optional<ExistRepository> repo = pool.getExpathRepo();
if (repo.isPresent()) {
repo.get().addObserver(this);
repo.get().addObserver(utilityPanel);
} else {
System.err.println("EXPath repository is not available.");
}
} catch (final EXistException e) {
System.err.println("Failed to register as observer for package manager events");
e.printStackTrace();
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class Launcher method checkInstalledApps.
private void checkInstalledApps() {
try {
final BrokerPool pool = BrokerPool.getInstance();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence pkgs = xquery.execute(broker, "repo:list()", null);
for (final SequenceIterator i = pkgs.iterate(); i.hasNext(); ) {
final ExistRepository.Notification notification = new ExistRepository.Notification(ExistRepository.Action.INSTALL, i.nextItem().getStringValue());
final Optional<ExistRepository> expathRepo = pool.getExpathRepo();
if (expathRepo.isPresent()) {
update(expathRepo.get(), notification);
utilityPanel.update(expathRepo.get(), notification);
}
expathRepo.orElseThrow(() -> new EXistException("EXPath repository is not available."));
}
}
} catch (final EXistException | XPathException | PermissionDeniedException e) {
System.err.println("Failed to check installed packages: " + e.getMessage());
e.printStackTrace();
}
}
use of org.exist.EXistException in project exist by eXist-db.
the class AbstractExistHttpServlet method doDatabaseStartup.
private void doDatabaseStartup(Configuration configuration) throws ServletException {
if (configuration == null) {
throw new ServletException("Database has not been " + "configured");
}
getLog().info("Configuring eXist instance");
try {
if (!BrokerPool.isConfigured()) {
BrokerPool.configure(1, 5, configuration);
}
} catch (final EXistException | DatabaseConfigurationException e) {
throw new ServletException(e.getMessage(), e);
}
try {
getLog().info("Registering XMLDB driver");
final Class<?> clazz = Class.forName("org.exist.xmldb.DatabaseImpl");
final Database database = (Database) clazz.newInstance();
DatabaseManager.registerDatabase(database);
} catch (final ClassNotFoundException | XMLDBException | IllegalAccessException | InstantiationException e) {
getLog().info("ERROR", e);
}
}
Aggregations