Search in sources :

Example 1 with Relation

use of gate.relations.Relation in project gate-core by GateNLP.

the class DocumentStaxUtils method writeRelationSet.

public static void writeRelationSet(RelationSet relations, XMLStreamWriter xsw, String namespaceURI) throws XMLStreamException {
    // versions of GATE
    if (relations == null || relations.size() == 0)
        return;
    xsw.writeComment(" Relation Set for " + relations.getAnnotationSet().getName() + " ");
    newLine(xsw);
    newLine(xsw);
    xsw.writeStartElement(namespaceURI, "RelationSet");
    if (relations.getAnnotationSet().getName() != null) {
        xsw.writeAttribute("Name", relations.getAnnotationSet().getName());
    }
    newLine(xsw);
    for (Relation relation : relations.get()) {
        StringBuilder str = new StringBuilder();
        int[] members = relation.getMembers();
        for (int i = 0; i < members.length; i++) {
            if (i > 0)
                str.append(";");
            str.append(members[i]);
        }
        xsw.writeStartElement(namespaceURI, "Relation");
        xsw.writeAttribute("Id", String.valueOf(relation.getId()));
        xsw.writeAttribute("Type", relation.getType());
        xsw.writeAttribute("Members", str.toString());
        newLine(xsw);
        xsw.writeStartElement(namespaceURI, "UserData");
        if (relation.getUserData() != null) {
            ObjectWrapper userData = new ObjectWrapper(relation.getUserData());
            writeCharactersOrCDATA(xsw, replaceXMLIllegalCharactersInString(userData.toString()));
        }
        xsw.writeEndElement();
        newLine(xsw);
        writeFeatures(relation.getFeatures(), xsw, namespaceURI);
        xsw.writeEndElement();
        newLine(xsw);
    }
    // end RelationSet element
    xsw.writeEndElement();
    newLine(xsw);
}
Also used : Relation(gate.relations.Relation) SimpleRelation(gate.relations.SimpleRelation)

Example 2 with Relation

use of gate.relations.Relation in project gate-core by GateNLP.

the class DocumentStaxUtils method readRelationSet.

public static void readRelationSet(XMLStreamReader xsr, RelationSet relations, Set<Integer> allAnnotIds) throws XMLStreamException {
    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        xsr.require(XMLStreamConstants.START_ELEMENT, null, "Relation");
        String type = xsr.getAttributeValue(null, "Type");
        String idString = xsr.getAttributeValue(null, "Id");
        String memberString = xsr.getAttributeValue(null, "Members");
        if (memberString == null)
            throw new XMLStreamException("A relation must have members");
        if (type == null)
            throw new XMLStreamException("A relation must have a type");
        if (idString == null)
            throw new XMLStreamException("A relation must have an id");
        String[] memberStrings = memberString.split(";");
        int[] members = new int[memberStrings.length];
        for (int i = 0; i < members.length; ++i) {
            members[i] = Integer.parseInt(memberStrings[i]);
        }
        xsr.nextTag();
        xsr.require(XMLStreamConstants.START_ELEMENT, null, "UserData");
        // get the string representation of the user data
        StringBuilder stringRep = new StringBuilder(1024);
        int eventType;
        while ((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) {
            switch(eventType) {
                case XMLStreamConstants.CHARACTERS:
                case XMLStreamConstants.CDATA:
                    stringRep.append(xsr.getTextCharacters(), xsr.getTextStart(), xsr.getTextLength());
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    throw new XMLStreamException("Elements not allowed within " + "user data.", xsr.getLocation());
                default:
            }
        }
        xsr.require(XMLStreamConstants.END_ELEMENT, null, "UserData");
        FeatureMap features = readFeatureMap(xsr);
        Relation r = new SimpleRelation(Integer.parseInt(idString), type, members);
        r.setFeatures(features);
        if (stringRep.length() > 0) {
            ObjectWrapper wrapper = new ObjectWrapper(stringRep.toString());
            r.setUserData(wrapper.getValue());
        }
        relations.add(r);
    }
}
Also used : FeatureMap(gate.FeatureMap) Relation(gate.relations.Relation) SimpleRelation(gate.relations.SimpleRelation) XMLStreamException(javax.xml.stream.XMLStreamException) SimpleRelation(gate.relations.SimpleRelation)

Aggregations

Relation (gate.relations.Relation)2 SimpleRelation (gate.relations.SimpleRelation)2 FeatureMap (gate.FeatureMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1