Search in sources :

Example 1 with QNm

use of org.brackit.xquery.atomic.QNm 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);
    }
}
Also used : StAXSerializer(org.sirix.service.xml.serialize.StAXSerializer) StyledDocument(javax.swing.text.StyledDocument) DiffAxis(org.sirix.gui.view.DiffAxis) QNm(org.brackit.xquery.atomic.QNm) NodeReadTrx(org.sirix.api.NodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Style(javax.swing.text.Style) SirixException(org.sirix.exception.SirixException) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class TextView method processEndTag.

/**
 * Process end tag.
 *
 * @param pEvent
 *          {@link XMLEvent} reference
 * @param pLevel
 *          level in the tree
 * @param pIndentSpaces
 *          determines how many spaces to indent
 * @param pDoc
 *          {@link StyledDocument} reference
 * @param pStyleElements
 *          {@link Style} reference
 * @throws BadLocationException
 *           if insertion of string fails
 */
private void processEndTag(final XMLEvent pEvent, final int pLevel, final String pIndentSpaces, final StyledDocument pDoc, final Style pStyleElements) throws BadLocationException {
    assert pEvent != null;
    // assert pLevel >= 0;
    assert pIndentSpaces != null;
    assert pDoc != null;
    assert pStyleElements != null;
    final EndElement endTag = pEvent.asEndElement();
    final QName name = endTag.getName();
    LOGWRAPPER.debug("endTag: " + endTag);
    indent(pDoc, pLevel, pIndentSpaces);
    pDoc.insertString(pDoc.getLength(), new StringBuilder().append("</").append(ViewUtilities.qNameToString(new QNm(name.getNamespaceURI(), name.getPrefix(), name.getLocalPart()))).append(">").append(NEWLINE).toString(), pStyleElements);
}
Also used : QNm(org.brackit.xquery.atomic.QNm) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName)

Example 3 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class AbstractTraverseModel method fillAttributes.

/**
 * Fill an attribute list with entries.
 *
 * @param rtx
 *          Sirix {@link NodeReadTrx}
 * @return {@link List} of {@link Attribute}s
 */
protected List<Attribute> fillAttributes(final NodeReadTrx rtx) {
    assert rtx != null;
    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    // attributes in the axis.
    assert rtx.getKind() == Kind.ELEMENT;
    final int attNumber = rtx.getAttributeCount();
    final List<Attribute> attributes = new ArrayList<>(attNumber);
    for (int i = 0; i < attNumber; i++) {
        rtx.moveToAttribute(i);
        final QNm name = rtx.getName();
        attributes.add(eventFactory.createAttribute(new QName(name.getNamespaceURI(), name.getLocalName(), name.getPrefix()), rtx.getValue()));
        rtx.moveToParent();
    }
    return attributes;
}
Also used : QNm(org.brackit.xquery.atomic.QNm) Attribute(javax.xml.stream.events.Attribute) XMLEventFactory(javax.xml.stream.XMLEventFactory) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList)

Example 4 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class AbstractShredder method processStartTag.

@Override
public void processStartTag(final QNm elementName) throws SirixException {
    final QNm name = checkNotNull(elementName);
    long key = -1;
    switch(mInsertLocation) {
        case ASFIRSTCHILD:
            if (mParents.peek() == Fixed.NULL_NODE_KEY.getStandardProperty()) {
                key = mWtx.insertElementAsFirstChild(name).getNodeKey();
            } else {
                key = mWtx.insertElementAsRightSibling(name).getNodeKey();
            }
            break;
        case ASRIGHTSIBLING:
            if (mWtx.getKind() == Kind.DOCUMENT || mWtx.getParentKey() == Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
                throw new IllegalStateException("Subtree can not be inserted as sibling of document root or the root-element!");
            }
            key = mWtx.insertElementAsRightSibling(name).getNodeKey();
            mInsertLocation = Insert.ASFIRSTCHILD;
            break;
        case ASLEFTSIBLING:
            if (mWtx.getKind() == Kind.DOCUMENT || mWtx.getParentKey() == Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
                throw new IllegalStateException("Subtree can not be inserted as sibling of document root or the root-element!");
            }
            key = mWtx.insertElementAsLeftSibling(name).getNodeKey();
            mInsertLocation = Insert.ASFIRSTCHILD;
            break;
    }
    mParents.pop();
    mParents.push(key);
    mParents.push(Fixed.NULL_NODE_KEY.getStandardProperty());
}
Also used : QNm(org.brackit.xquery.atomic.QNm)

Example 5 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class XMLShredder method addNewElement.

/**
 * Add a new element node.
 *
 * @param pLeftSiblingKeyStack stack used to determine if the new element has to be inserted as a
 *        right sibling or as a new child (in the latter case is NULL on top of the stack)
 * @param event the current event from the StAX parser
 * @return the modified stack
 * @throws SirixException if adding {@link ElementNode} fails
 */
private void addNewElement(final StartElement event) throws SirixException {
    assert event != null;
    final QName qName = event.getName();
    final QNm name = new QNm(qName.getNamespaceURI(), qName.getPrefix(), qName.getLocalPart());
    processStartTag(name);
    // Parse namespaces.
    for (final Iterator<?> it = event.getNamespaces(); it.hasNext(); ) {
        final Namespace namespace = (Namespace) it.next();
        mWtx.insertNamespace(new QNm(namespace.getNamespaceURI(), namespace.getPrefix(), ""));
        mWtx.moveToParent();
    }
    // Parse attributes.
    for (final Iterator<?> it = event.getAttributes(); it.hasNext(); ) {
        final Attribute attribute = (Attribute) it.next();
        final QName attName = attribute.getName();
        mWtx.insertAttribute(new QNm(attName.getNamespaceURI(), attName.getPrefix(), attName.getLocalPart()), attribute.getValue());
        mWtx.moveToParent();
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Namespace(javax.xml.stream.events.Namespace)

Aggregations

QNm (org.brackit.xquery.atomic.QNm)89 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)20 Test (org.junit.Test)18 QueryException (org.brackit.xquery.QueryException)15 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)15 DBNode (org.sirix.xquery.node.DBNode)12 QName (javax.xml.namespace.QName)11 PathSummaryReader (org.sirix.index.path.summary.PathSummaryReader)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Axis (org.sirix.api.Axis)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 SirixException (org.sirix.exception.SirixException)7 Attribute (javax.xml.stream.events.Attribute)5 Namespace (javax.xml.stream.events.Namespace)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixIOException (org.sirix.exception.SirixIOException)5 Ignore (org.junit.Ignore)4 SirixUsageException (org.sirix.exception.SirixUsageException)4