Search in sources :

Example 1 with Text

use of nu.xom.Text in project catma by forTEXT.

the class XmlMarkupCollectionSerializationHandler method scanElements.

private void scanElements(StringBuilder contentBuilder, Element element, Stack<String> elementStack, TagManager tagManager, TagLibrary tagLibrary, Map<String, String> namespacePrefixToTagsetIdMap, AnnotationCollection userMarkupCollection, String docId, int docLength) throws Exception {
    int start = contentBuilder.length();
    StringBuilder pathBuilder = new StringBuilder();
    for (int j = 0; j < elementStack.size(); j++) {
        pathBuilder.append("/" + elementStack.get(j));
    }
    String parentPath = pathBuilder.toString();
    elementStack.push(element.getLocalName());
    String path = parentPath + "/" + elementStack.peek();
    String tagName = element.getLocalName();
    String prefix = element.getNamespacePrefix();
    String tagsetId = namespacePrefixToTagsetIdMap.get(prefix);
    if (tagsetId == null) {
        tagsetId = KnownTagsetDefinitionName.DEFAULT_INTRINSIC_XML.asTagsetId();
    }
    TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
    String tagId = idGenerator.generate();
    TagDefinition tagDefinition = tagset.getTagDefinitionsByName(tagName).findFirst().orElse(null);
    String pathPropertyDefId = null;
    if (tagDefinition == null) {
        tagDefinition = new TagDefinition(tagId, elementStack.peek(), // no parent, hierarchy is collected in annotation property
        null, tagsetId);
        tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_displaycolor.name()), PropertyDefinition.SystemPropertyName.catma_displaycolor.name(), Collections.singletonList(ColorConverter.toRGBIntAsString(ColorConverter.randomHex()))));
        tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_markupauthor.name()), PropertyDefinition.SystemPropertyName.catma_markupauthor.name(), Collections.singletonList(author)));
        pathPropertyDefId = idGenerator.generate();
        PropertyDefinition pathDef = new PropertyDefinition(pathPropertyDefId, "path", Collections.emptyList());
        tagDefinition.addUserDefinedPropertyDefinition(pathDef);
        tagManager.addTagDefinition(tagset, tagDefinition);
    } else {
        pathPropertyDefId = tagDefinition.getPropertyDefinition("path").getUuid();
    }
    for (int idx = 0; idx < element.getChildCount(); idx++) {
        Node curChild = element.getChild(idx);
        if (curChild instanceof Text) {
            xmlContentHandler.addTextContent(contentBuilder, element, curChild.getValue());
        } else if (curChild instanceof Element) {
            // descent
            scanElements(contentBuilder, (Element) curChild, elementStack, tagManager, tagLibrary, namespacePrefixToTagsetIdMap, userMarkupCollection, docId, docLength);
        }
    }
    if (element.getChildCount() != 0) {
        xmlContentHandler.addBreak(contentBuilder, element);
    }
    int end = contentBuilder.length();
    Range range = new Range(start, end);
    if (range.isSinglePoint()) {
        int newStart = range.getStartPoint();
        if (newStart > 0) {
            newStart = newStart - 1;
        }
        int newEnd = range.getEndPoint();
        if (newEnd < docLength - 1) {
            newEnd = newEnd + 1;
        }
        range = new Range(newStart, newEnd);
    }
    TagInstance tagInstance = new TagInstance(idGenerator.generate(), tagDefinition.getUuid(), author, ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
    for (int i = 0; i < element.getAttributeCount(); i++) {
        PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(element.getAttribute(i).getQualifiedName());
        if (propertyDefinition == null) {
            propertyDefinition = new PropertyDefinition(idGenerator.generate(), element.getAttribute(i).getQualifiedName(), Collections.singleton(element.getAttribute(i).getValue()));
            tagManager.addUserDefinedPropertyDefinition(tagDefinition, propertyDefinition);
        } else if (!propertyDefinition.getPossibleValueList().contains(element.getAttribute(i).getValue())) {
            List<String> newValueList = new ArrayList<>();
            newValueList.addAll(propertyDefinition.getPossibleValueList());
            newValueList.add(element.getAttribute(i).getValue());
            propertyDefinition.setPossibleValueList(newValueList);
        }
        Property property = new Property(propertyDefinition.getUuid(), Collections.singleton(element.getAttribute(i).getValue()));
        tagInstance.addUserDefinedProperty(property);
    }
    Property pathProperty = new Property(pathPropertyDefId, Collections.singletonList(path));
    tagInstance.addUserDefinedProperty(pathProperty);
    TagReference tagReference = new TagReference(tagInstance, docId, range, userMarkupCollection.getId());
    userMarkupCollection.addTagReference(tagReference);
    elementStack.pop();
}
Also used : TagDefinition(de.catma.tag.TagDefinition) Node(nu.xom.Node) Element(nu.xom.Element) Text(nu.xom.Text) Range(de.catma.document.Range) PropertyDefinition(de.catma.tag.PropertyDefinition) TagsetDefinition(de.catma.tag.TagsetDefinition) TagInstance(de.catma.tag.TagInstance) ArrayList(java.util.ArrayList) List(java.util.List) TagReference(de.catma.document.annotation.TagReference) Property(de.catma.tag.Property)

Example 2 with Text

use of nu.xom.Text in project meico by cemfi.

the class Author method parseData.

/**
 * parse the author element and set the according class variables
 * @param xml
 * @throws Exception
 */
@Override
protected void parseData(Element xml) throws Exception {
    if (xml == null)
        throw new Exception("Cannot generate Author object. XML Element is null.");
    this.setXml(xml);
    if ((xml.getChildCount() == 0) || !(xml.getChild(0) instanceof Text)) {
        // if no text node is given in the xml source
        // generate a placeholder text node
        this.name = new Text("");
        // add it to the xml
        xml.appendChild(this.name);
    } else
        this.name = (Text) xml.getChild(0);
    this.number = Helper.getAttribute("number", xml);
    this.id = Helper.getAttribute("id", xml);
}
Also used : Text(nu.xom.Text)

Example 3 with Text

use of nu.xom.Text in project htmlparser by validator.

the class XOMTreeBuilder method appendCharacters.

@Override
protected void appendCharacters(Element parent, String text) throws SAXException {
    try {
        int childCount = parent.getChildCount();
        Node lastChild;
        if (childCount != 0 && ((lastChild = parent.getChild(childCount - 1)) instanceof Text)) {
            Text lastAsText = (Text) lastChild;
            lastAsText.setValue(lastAsText.getValue() + text);
            return;
        }
        parent.appendChild(nodeFactory.makeText(text));
    } catch (XMLException e) {
        fatal(e);
    }
}
Also used : XMLException(nu.xom.XMLException) ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Text(nu.xom.Text)

Example 4 with Text

use of nu.xom.Text in project htmlparser by validator.

the class XOMTreeBuilder method insertFosterParentedCharacters.

@Override
protected void insertFosterParentedCharacters(String text, Element table, Element stackParent) throws SAXException {
    try {
        Node parent = table.getParent();
        if (parent != null) {
            // always an element if not null
            Element parentAsElt = (Element) parent;
            int tableIndex = indexOfTable(table, parentAsElt);
            Node prevSibling;
            if (tableIndex != 0 && ((prevSibling = parentAsElt.getChild(tableIndex - 1)) instanceof Text)) {
                Text prevAsText = (Text) prevSibling;
                prevAsText.setValue(prevAsText.getValue() + text);
                return;
            }
            parentAsElt.insertChild(nodeFactory.makeText(text), tableIndex);
            cachedTableIndex++;
            return;
        }
        int childCount = stackParent.getChildCount();
        Node lastChild;
        if (childCount != 0 && ((lastChild = stackParent.getChild(childCount - 1)) instanceof Text)) {
            Text lastAsText = (Text) lastChild;
            lastAsText.setValue(lastAsText.getValue() + text);
            return;
        }
        stackParent.appendChild(nodeFactory.makeText(text));
    } catch (XMLException e) {
        fatal(e);
    }
}
Also used : XMLException(nu.xom.XMLException) ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Element(nu.xom.Element) Text(nu.xom.Text)

Example 5 with Text

use of nu.xom.Text in project teiid by teiid.

the class BinaryXMLCodec method readText.

// try to avoid XML reverification by caching repetitive Texts
private Text readText(ArrayByteList src, int type) {
    int i = readSymbol(src, 4, type);
    Text text;
    if (i < textCache.length) {
        text = textCache[i];
        if (text != null)
            return new Text(text);
    }
    text = new Text(symbols[i]);
    if (i < textCache.length)
        textCache[i] = text;
    return text;
}
Also used : Text(nu.xom.Text)

Aggregations

Text (nu.xom.Text)10 Element (nu.xom.Element)6 Node (nu.xom.Node)3 ArrayList (java.util.ArrayList)2 Attribute (nu.xom.Attribute)2 ParentNode (nu.xom.ParentNode)2 XMLException (nu.xom.XMLException)2 Range (de.catma.document.Range)1 TagReference (de.catma.document.annotation.TagReference)1 Property (de.catma.tag.Property)1 PropertyDefinition (de.catma.tag.PropertyDefinition)1 TagDefinition (de.catma.tag.TagDefinition)1 TagInstance (de.catma.tag.TagInstance)1 TagsetDefinition (de.catma.tag.TagsetDefinition)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Document (nu.xom.Document)1