Search in sources :

Example 1 with TextData

use of eu.etaxonomy.cdm.model.description.TextData in project cdmlib by cybertaxonomy.

the class CsvDemoExport method handleRedlistStatus.

/**
 * @param record
 * @param taxon
 * @param featureCells
 * @param features
 */
private void handleRedlistStatus(CsvDemoRecord record, Taxon taxon, List<List<String>> featureCells, List<Feature> features) {
    Set<TaxonDescription> descriptions = taxon.getDescriptions();
    for (TaxonDescription description : descriptions) {
        for (DescriptionElementBase el : description.getElements()) {
            if (el.isInstanceOf(CategoricalData.class)) {
                CategoricalData categoricalData = CdmBase.deproxy(el, CategoricalData.class);
                for (State state : categoricalData.getStatesOnly()) {
                    Feature stateFeature = categoricalData.getFeature();
                    // find matching feature and put data into according cell
                    for (int i = 0; i < features.size(); i++) {
                        if (features.get(i).equals(stateFeature)) {
                            List<String> cell = featureCells.get(i);
                            cell.add(state.toString());
                        }
                    }
                }
            } else if (el.isInstanceOf(TextData.class)) {
                TextData textData = CdmBase.deproxy(el, TextData.class);
                Feature textFeature = textData.getFeature();
                // find matching feature and put data into according cell
                for (int i = 0; i < features.size(); i++) {
                    if (features.get(i).equals(textFeature)) {
                        List<String> cell = featureCells.get(i);
                        String text = textData.getText(Language.GERMAN());
                        text = text.replaceAll(System.getProperty("line.separator"), "");
                        text = text.replaceAll("                            ", " ");
                        cell.add(text);
                    }
                }
            }
        }
    }
    record.setFeatures(featureCells);
}
Also used : State(eu.etaxonomy.cdm.model.description.State) CategoricalData(eu.etaxonomy.cdm.model.description.CategoricalData) TextData(eu.etaxonomy.cdm.model.description.TextData) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) ArrayList(java.util.ArrayList) List(java.util.List) Feature(eu.etaxonomy.cdm.model.description.Feature) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 2 with TextData

use of eu.etaxonomy.cdm.model.description.TextData in project cdmlib by cybertaxonomy.

the class MarkupFeatureImport method handleHabitat.

protected String handleHabitat(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent) throws XMLStreamException {
    checkNoAttributes(parentEvent);
    Taxon taxon = state.getCurrentTaxon();
    // TODO which ref to take?
    Reference sourceReference = state.getConfig().getSourceReference();
    boolean isTextMode = true;
    String text = "";
    while (reader.hasNext()) {
        XMLEvent next = readNoWhitespace(reader);
        if (isMyEndingElement(next, parentEvent)) {
            Feature feature = getFeature(state, MarkupTransformer.uuidExtractedHabitat, "Extracted Habitat", "An structured habitat that was extracted from a habitat text", "extr. habit.", null);
            TextData habitat = TextData.NewInstance(feature);
            habitat.addPrimaryTaxonomicSource(sourceReference);
            habitat.putText(getDefaultLanguage(state), text);
            TaxonDescription description = getExtractedMarkupMarkedDescription(state, taxon, sourceReference);
            description.addElement(habitat);
            return text;
        } else if (isStartingElement(next, ALTITUDE)) {
            // OLD: text = text.trim() + getTaggedCData(state, reader, next);
            text += handleAltitude(state, reader, next);
        } else if (isStartingElement(next, LIFE_CYCLE_PERIODS)) {
            handleNotYetImplementedElement(next);
        } else if (next.isCharacters()) {
            if (!isTextMode) {
                String message = "String is not in text mode";
                fireWarningEvent(message, next, 6);
            } else {
                text += next.asCharacters().getData();
            }
        } else if (isStartingElement(next, BR)) {
            text += "<br/>";
            isTextMode = false;
        } else if (isEndingElement(next, BR)) {
            isTextMode = true;
        } else if (isStartingElement(next, REFERENCES)) {
            handleNotYetImplementedElement(next);
        } else if (isStartingElement(next, FIGURE_REF)) {
            handleNotYetImplementedElement(next);
        } else {
            String type = next.toString();
            String location = String.valueOf(next.getLocation().getLineNumber());
            System.out.println("MarkupFeature.handleHabitat: Unexpected element in habitat: " + type + ":  " + location);
            handleUnexpectedElement(next);
        }
    }
    throw new IllegalStateException("<Habitat> has no closing tag");
}
Also used : Reference(eu.etaxonomy.cdm.model.reference.Reference) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) Taxon(eu.etaxonomy.cdm.model.taxon.Taxon) TextData(eu.etaxonomy.cdm.model.description.TextData) XMLEvent(javax.xml.stream.events.XMLEvent) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) Feature(eu.etaxonomy.cdm.model.description.Feature)

Example 3 with TextData

use of eu.etaxonomy.cdm.model.description.TextData in project cdmlib by cybertaxonomy.

the class MarkupFeatureImport method handleChar.

/**
 * Handle the char or subchar element. As
 * @param state the import state
 * @param reader
 * @param parentEvent
 * @param parentFeature in case of subchars we need to attache the newly created feature to a parent feature, should be <code>null</code>
 * for top level chars.
 * @return List of TextData. Not a single one as the recursive TextData will also be returned
 * @throws XMLStreamException
 */
private List<TextData> handleChar(MarkupImportState state, XMLEventReader reader, XMLEvent parentEvent, Feature parentFeature, CharOrder myCharOrder) throws XMLStreamException {
    List<TextData> result = new ArrayList<>();
    String classValue = getClassOnlyAttribute(parentEvent);
    Feature feature = makeFeature(classValue, state, parentEvent, parentFeature);
    if (parentFeature == null) {
        state.putFeatureToCharSorterList(feature);
    } else {
        FeatureSorterInfo parentInfo = state.getLatestCharFeatureSorterInfo();
        // if (! parentInfo.getUuid().equals(parentFeature.getUuid())){
        // String message = "The parent char feature is not the same as the latest feature. This is the case for char hierarchies with > 2 levels, which is not yet handled by the import";
        // fireWarningEvent(message, parentEvent, 6);
        // }else{
        state.getLatestCharFeatureSorterInfo().addSubFeature(new FeatureSorterInfo(feature));
    // }
    }
    TextData textData = TextData.NewInstance(feature);
    textData.addPrimaryTaxonomicSource(state.getConfig().getSourceReference());
    result.add(textData);
    AnnotationType annType = getAnnotationType(state, MarkupTransformer.uuidOriginalOrder, "Original order", "Order in original treatment", null, AnnotationType.TECHNICAL().getVocabulary());
    textData.addAnnotation(Annotation.NewInstance(myCharOrder.orderString(), annType, Language.ENGLISH()));
    boolean isTextMode = true;
    String text = "";
    while (reader.hasNext()) {
        XMLEvent next = readNoWhitespace(reader);
        if (isMyEndingElement(next, parentEvent)) {
            text = text.trim();
            textData.putText(getDefaultLanguage(state), text);
            return result;
        } else if (isStartingElement(next, FIGURE_REF)) {
            // TODO
            handleNotYetImplementedElement(next);
        } else if (isStartingElement(next, FOOTNOTE_REF)) {
            // TODO
            handleNotYetImplementedElement(next);
        } else if (isStartingElement(next, BR)) {
            text += "<br/>";
            isTextMode = false;
        } else if (isEndingElement(next, BR)) {
            isTextMode = true;
        } else if (isHtml(next)) {
            text += getXmlTag(next);
        } else if (next.isStartElement()) {
            if (isStartingElement(next, ANNOTATION)) {
                // TODO test handleSimpleAnnotation
                handleNotYetImplementedElement(next);
            } else if (isStartingElement(next, ITALICS)) {
                handleNotYetImplementedElement(next);
            } else if (isStartingElement(next, BOLD)) {
                handleNotYetImplementedElement(next);
            } else if (isStartingElement(next, FIGURE)) {
                handleFigure(state, reader, next, specimenImport, nomenclatureImport);
            } else if (isStartingElement(next, SUB_CHAR)) {
                List<TextData> subTextData = handleChar(state, reader, next, feature, myCharOrder.nextChild());
                result.addAll(subTextData);
            } else if (isStartingElement(next, FOOTNOTE)) {
                FootnoteDataHolder footnote = handleFootnote(state, reader, next, specimenImport, nomenclatureImport);
                if (footnote.isRef()) {
                    String message = "Ref footnote not implemented here";
                    fireWarningEvent(message, next, 4);
                } else {
                    registerGivenFootnote(state, footnote);
                }
            } else {
                handleUnexpectedStartElement(next.asStartElement());
            }
        } else if (next.isCharacters()) {
            if (!isTextMode) {
                String message = "String is not in text mode";
                fireWarningEvent(message, next, 6);
            } else {
                text += next.asCharacters().getData();
            }
        } else {
            handleUnexpectedEndElement(next.asEndElement());
        }
    }
    throw new IllegalStateException("RefPart has no closing tag");
}
Also used : TextData(eu.etaxonomy.cdm.model.description.TextData) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) Feature(eu.etaxonomy.cdm.model.description.Feature) AnnotationType(eu.etaxonomy.cdm.model.common.AnnotationType)

Example 4 with TextData

use of eu.etaxonomy.cdm.model.description.TextData in project cdmlib by cybertaxonomy.

the class MarkupFeatureImport method makeFeatureString.

/**
 * @param state
 * @param reader
 * @param feature
 * @param taxonDescription
 * @param lastDescriptionElement
 * @param distributionList
 * @param next
 * @return
 * @throws XMLStreamException
 * @throws
 */
private DescriptionElementBase makeFeatureString(MarkupImportState state, XMLEventReader reader, Feature feature, TaxonDescription taxonDescription, DescriptionElementBase lastDescriptionElement, XMLEvent next, Boolean isFreetext) throws XMLStreamException {
    // for specimen only
    if (feature.equals(Feature.SPECIMEN()) || feature.equals(Feature.MATERIALS_EXAMINED()) || feature.getUuid().equals(MarkupTransformer.uuidWoodSpecimens)) {
        List<DescriptionElementBase> specimens = specimenImport.handleMaterialsExamined(state, reader, next, feature, taxonDescription);
        for (DescriptionElementBase specimen : specimens) {
            if (specimen.getInDescription() == null) {
                taxonDescription.addElement(specimen);
            }
            lastDescriptionElement = specimen;
        }
        state.setCurrentCollector(null);
        return lastDescriptionElement;
    } else if (feature.equals(Feature.COMMON_NAME()) && (isFreetext == null || !isFreetext)) {
        List<DescriptionElementBase> commonNames = makeCommonNameString(state, reader, next);
        // NOTE: we do also have the old version makeVernacular, which was called from "others" below
        for (DescriptionElementBase commonName : commonNames) {
            taxonDescription.addElement(commonName);
            lastDescriptionElement = commonName;
        }
        return lastDescriptionElement;
    } else {
        // others
        Map<String, SubheadingResult> subheadingMap = handleString(state, reader, next, feature);
        for (String subheading : subheadingMap.keySet()) {
            Feature subheadingFeature = feature;
            if (StringUtils.isNotBlank(subheading) && subheadingMap.size() > 1) {
                subheadingFeature = makeFeature(subheading, state, next, null);
            }
            if (feature.equals(Feature.COMMON_NAME()) && (isFreetext == null || !isFreetext)) {
            // NOTE: see above
            // List<DescriptionElementBase> commonNames = makeVernacular(state, subheading, subheadingMap.get(subheading));
            // for (DescriptionElementBase commonName : commonNames){
            // taxonDescription.addElement(commonName);
            // lastDescriptionElement = commonName;
            // }
            } else {
                TextData textData = TextData.NewInstance(subheadingFeature);
                SubheadingResult subHeadingResult = subheadingMap.get(subheading);
                LanguageString languageString = textData.putText(getDefaultLanguage(state), subHeadingResult.text);
                if (isNotEmptyCollection(subHeadingResult.references.getReferences())) {
                    for (LabeledReference reference : subHeadingResult.references.getReferences()) {
                        textData.addPrimaryTaxonomicSource(reference.ref, reference.detail);
                    }
                    textData.addImportSource(null, null, state.getConfig().getSourceReference(), null);
                } else {
                    textData.addPrimaryTaxonomicSource(state.getConfig().getSourceReference());
                }
                // intext references
                for (IntextReference intext : subHeadingResult.inlineReferences) {
                    languageString.addIntextReference(intext);
                }
                taxonDescription.addElement(textData);
                lastDescriptionElement = textData;
            // TODO how to handle figures when these data are split in
            // subheadings
            }
        }
        return lastDescriptionElement;
    }
}
Also used : IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) TextData(eu.etaxonomy.cdm.model.description.TextData) ArrayList(java.util.ArrayList) List(java.util.List) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString) Map(java.util.Map) Feature(eu.etaxonomy.cdm.model.description.Feature) DescriptionElementBase(eu.etaxonomy.cdm.model.description.DescriptionElementBase)

Example 5 with TextData

use of eu.etaxonomy.cdm.model.description.TextData in project cdmlib by cybertaxonomy.

the class MarkupFeatureImport method makeFeatureWriter.

/**
 * @param state
 * @param reader
 * @param feature
 * @param taxon
 * @param next
 * @throws XMLStreamException
 */
private void makeFeatureWriter(MarkupImportState state, XMLEventReader reader, Feature feature, Taxon taxon, XMLEvent next) throws XMLStreamException {
    WriterDataHolder writer = handleWriter(state, reader, next);
    if (isNotBlank(writer.writer)) {
        // TODO
        Reference ref = state.getConfig().getSourceReference();
        TaxonDescription description = getDefaultTaxonDescription(taxon, false, true, ref);
        TextData featurePlaceholder = docImport.getFeaturePlaceholder(state, description, feature, true);
        featurePlaceholder.addAnnotation(writer.annotation);
        registerFootnotes(state, featurePlaceholder, writer.footnotes);
    } else {
        String message = "Writer element is empty";
        fireWarningEvent(message, next, 4);
    }
}
Also used : Reference(eu.etaxonomy.cdm.model.reference.Reference) IntextReference(eu.etaxonomy.cdm.model.common.IntextReference) TextData(eu.etaxonomy.cdm.model.description.TextData) TaxonDescription(eu.etaxonomy.cdm.model.description.TaxonDescription) LanguageString(eu.etaxonomy.cdm.model.common.LanguageString)

Aggregations

TextData (eu.etaxonomy.cdm.model.description.TextData)98 TaxonDescription (eu.etaxonomy.cdm.model.description.TaxonDescription)40 LanguageString (eu.etaxonomy.cdm.model.common.LanguageString)31 Test (org.junit.Test)29 Language (eu.etaxonomy.cdm.model.common.Language)28 Taxon (eu.etaxonomy.cdm.model.taxon.Taxon)28 Feature (eu.etaxonomy.cdm.model.description.Feature)27 DescriptionElementBase (eu.etaxonomy.cdm.model.description.DescriptionElementBase)26 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)20 SpecimenDescription (eu.etaxonomy.cdm.model.description.SpecimenDescription)19 Reference (eu.etaxonomy.cdm.model.reference.Reference)18 CommonTaxonName (eu.etaxonomy.cdm.model.description.CommonTaxonName)13 Media (eu.etaxonomy.cdm.model.media.Media)13 ArrayList (java.util.ArrayList)12 DataSet (org.unitils.dbunit.annotation.DataSet)11 TaxonName (eu.etaxonomy.cdm.model.name.TaxonName)10 UUID (java.util.UUID)10 CategoricalData (eu.etaxonomy.cdm.model.description.CategoricalData)9 DescriptionElementSource (eu.etaxonomy.cdm.model.description.DescriptionElementSource)9 TaxonNameDescription (eu.etaxonomy.cdm.model.description.TaxonNameDescription)8