Example usage for java.util.regex Matcher appendReplacement

List of usage examples for java.util.regex Matcher appendReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher appendReplacement.

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:org.forgerock.openidm.repo.orientdb.impl.query.TokenHandler.java

/**
 * Replaces a query string with tokens of format ${token-name} with the values from the
 * passed in map, where the token-name must be the key in the map
 * /*from  w ww  . j a  v a2 s  .  c  om*/
 * @param queryString the query with tokens
 * @param params the parameters to replace the tokens. Values can be String or List.
 * @return the query with all tokens replace with their found values
 * @throws BadRequestException if token in the query is not in the passed parameters
 */
String replaceTokensWithValues(String queryString, Map<String, String> params) throws BadRequestException {
    java.util.regex.Matcher matcher = tokenPattern.matcher(queryString);
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        String fullTokenKey = matcher.group(1);
        String tokenKey = fullTokenKey;
        String tokenPrefix = null;
        String[] tokenKeyParts = tokenKey.split(":", 2);
        // if prefix found
        if (tokenKeyParts.length == 2) {
            tokenPrefix = tokenKeyParts[0];
            tokenKey = tokenKeyParts[1];
        }
        if (!params.containsKey(tokenKey)) {
            // fail with an exception if token not found
            throw new BadRequestException("Missing entry in params passed to query for token " + tokenKey);
        } else {
            Object replacement = params.get(tokenKey);

            if (PREFIX_LIST.equals(tokenPrefix)) {
                // escape quotes, quote each element, and split on ,
                replacement = Arrays.asList(
                        ("'" + replacement.toString().replaceAll("'", "\\\\'").replaceAll(",", "','") + "'")
                                .split(","));
            }

            if (replacement instanceof List) {
                StringBuffer commaSeparated = new StringBuffer();
                boolean first = true;
                for (Object entry : ((List) replacement)) {
                    if (!first) {
                        commaSeparated.append(",");
                    } else {
                        first = false;
                    }
                    commaSeparated.append(entry.toString());
                }
                replacement = commaSeparated.toString();
            }

            if (replacement == null) {
                replacement = "";
            }

            // Optional control of representation via prefix
            if (tokenPrefix != null) {
                if (tokenPrefix.equals(PREFIX_UNQUOTED)) {
                    // Leave replacement unquoted
                } else if (tokenPrefix.equals(PREFIX_DOTNOTATION)) {
                    // Convert Json Pointer to OrientDB dot notation
                    replacement = JSON_POINTER_TO_DOT_NOTATION.apply(replacement.toString());
                }
            } else {
                // Default is single quoted string replacement (escaping single quotes in replacement)
                replacement = "'" + replacement.toString().replaceAll("'", "\\\\'") + "'";
            }

            matcher.appendReplacement(buffer, "");
            buffer.append(replacement);
        }
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:de.mpg.mpdl.inge.transformation.transformations.commonPublicationFormats.Bibtex.java

/**
 * @param bibtex/*  ww  w . ja  v  a2  s.  c o  m*/
 * @return eSciDoc-publication item XML representation of this BibTeX entry
 * @throws RuntimeException
 */
public String getBibtex(String bibtex) throws RuntimeException {
    // Remove Math '$' from the whole BibTex-String
    Pattern mathPattern = Pattern.compile("(?sm)\\$(\\\\.*?)(?<!\\\\)\\$");
    Matcher mathMatcher = mathPattern.matcher(bibtex);
    StringBuffer sb = new StringBuffer();
    while (mathMatcher.find()) {
        mathMatcher.appendReplacement(sb, "$1");
    }
    mathMatcher.appendTail(sb);
    bibtex = sb.toString();
    BibtexParser parser = new BibtexParser(true);
    BibtexFile file = new BibtexFile();
    try {
        parser.parse(file, new StringReader(bibtex));
    } catch (Exception e) {
        this.logger.error("Error parsing BibTex record.");
        throw new RuntimeException(e);
    }
    PubItemVO itemVO = new PubItemVO();
    MdsPublicationVO mds = new MdsPublicationVO();
    itemVO.setMetadata(mds);
    List entries = file.getEntries();
    boolean entryFound = false;
    if (entries == null || entries.size() == 0) {
        this.logger.warn("No entry found in BibTex record.");
        throw new RuntimeException();
    }
    for (Object object : entries) {
        if (object instanceof BibtexEntry) {
            if (entryFound) {
                this.logger.error("Multiple entries in BibTex record.");
                throw new RuntimeException();
            }
            entryFound = true;
            BibtexEntry entry = (BibtexEntry) object;
            // genre
            BibTexUtil.Genre bibGenre;
            try {
                bibGenre = BibTexUtil.Genre.valueOf(entry.getEntryType());
            } catch (IllegalArgumentException iae) {
                bibGenre = BibTexUtil.Genre.misc;
                this.logger.warn("Unrecognized genre: " + entry.getEntryType());
            }
            MdsPublicationVO.Genre itemGenre = BibTexUtil.getGenreMapping().get(bibGenre);
            mds.setGenre(itemGenre);
            SourceVO sourceVO = new SourceVO();
            SourceVO secondSourceVO = new SourceVO();
            Map fields = entry.getFields();
            // Mapping of BibTeX Standard Entries
            // title
            if (fields.get("title") != null) {
                if (fields.get("chapter") != null) {
                    mds.setTitle(BibTexUtil.stripBraces(
                            BibTexUtil.bibtexDecode(fields.get("chapter").toString()), false) + " - "
                            + BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("title").toString()),
                                    false));
                } else {
                    mds.setTitle(BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("title").toString()),
                            false));
                }
            }
            // booktitle
            if (fields.get("booktitle") != null) {
                if (bibGenre == BibTexUtil.Genre.book) {
                    mds.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("booktitle").toString()), false));
                } else if (bibGenre == BibTexUtil.Genre.conference || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.inproceedings) {
                    sourceVO.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("booktitle").toString()), false));
                    if (bibGenre == BibTexUtil.Genre.conference || bibGenre == BibTexUtil.Genre.inproceedings) {
                        sourceVO.setGenre(Genre.PROCEEDINGS);
                    } else if (bibGenre == BibTexUtil.Genre.inbook
                            || bibGenre == BibTexUtil.Genre.incollection) {
                        sourceVO.setGenre(Genre.BOOK);
                    }
                }
            }
            // fjournal, journal
            if (fields.get("fjournal") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.unpublished) {
                    sourceVO.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("fjournal").toString()), false));
                    sourceVO.setGenre(SourceVO.Genre.JOURNAL);
                    if (fields.get("journal") != null) {
                        sourceVO.getAlternativeTitles().add(new AlternativeTitleVO(BibTexUtil.stripBraces(
                                BibTexUtil.bibtexDecode(fields.get("journal").toString()), false)));
                    }
                }
            } else if (fields.get("journal") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.unpublished
                        || bibGenre == BibTexUtil.Genre.inproceedings) {
                    sourceVO.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("journal").toString()), false));
                    sourceVO.setGenre(SourceVO.Genre.JOURNAL);
                }
            }
            // number
            if (fields.get("number") != null && bibGenre != BibTexUtil.Genre.techreport) {
                sourceVO.setIssue(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("number").toString()), false));
            } else if (fields.get("number") != null && bibGenre == BibTexUtil.Genre.techreport) {
                {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.REPORT_NR, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("number").toString()), false)));
                }
            }
            // pages
            if (fields.get("pages") != null) {
                if (bibGenre == BibTexUtil.Genre.book || bibGenre == BibTexUtil.Genre.proceedings) {
                    mds.setTotalNumberOfPages(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("pages").toString()), false));
                } else {
                    BibTexUtil.fillSourcePages(BibTexUtil.stripBraces(
                            BibTexUtil.bibtexDecode(fields.get("pages").toString()), false), sourceVO);
                    if (bibGenre == BibTexUtil.Genre.inproceedings
                            && (fields.get("booktitle") == null || fields.get("booktitle").toString() == "")
                            && (fields.get("event_name") != null
                                    && fields.get("event_name").toString() != "")) {
                        sourceVO.setTitle(BibTexUtil.stripBraces(fields.get("event_name").toString(), false));
                        sourceVO.setGenre(Genre.PROCEEDINGS);
                    }
                }
            }
            // Publishing info
            PublishingInfoVO publishingInfoVO = new PublishingInfoVO();
            mds.setPublishingInfo(publishingInfoVO);
            // address
            if (fields.get("address") != null) {
                if (!(bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.conference
                        || bibGenre == BibTexUtil.Genre.incollection)
                        && (sourceVO.getTitle() == null || sourceVO.getTitle() == null)) {
                    publishingInfoVO.setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("address").toString()), false));
                } else {
                    if (sourceVO.getPublishingInfo() == null) {
                        PublishingInfoVO sourcePublishingInfoVO = new PublishingInfoVO();
                        sourceVO.setPublishingInfo(sourcePublishingInfoVO);
                    }
                    sourceVO.getPublishingInfo().setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("address").toString()), false));
                }
            }
            // edition
            if (fields.get("edition") != null) {
                publishingInfoVO.setEdition(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("edition").toString()), false));
            }
            // publisher
            if (!(bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                    || bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.conference
                    || bibGenre == BibTexUtil.Genre.incollection)
                    && (sourceVO.getTitle() == null || sourceVO.getTitle() == null)) {
                if (fields.get("publisher") != null) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("publisher").toString()), false));
                } else if (fields.get("school") != null && (bibGenre == BibTexUtil.Genre.mastersthesis
                        || bibGenre == BibTexUtil.Genre.phdthesis || bibGenre == BibTexUtil.Genre.techreport)) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("school").toString()), false));
                } else if (fields.get("institution") != null) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("institution").toString()), false));
                } else if (fields.get("publisher") == null && fields.get("school") == null
                        && fields.get("institution") == null && fields.get("address") != null) {
                    publishingInfoVO.setPublisher("ANY PUBLISHER");
                }
            } else {
                if (sourceVO.getPublishingInfo() == null) {
                    PublishingInfoVO sourcePublishingInfoVO = new PublishingInfoVO();
                    sourceVO.setPublishingInfo(sourcePublishingInfoVO);
                }
                if (fields.get("publisher") != null) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("publisher").toString()), false));
                } else if (fields.get("school") != null && (bibGenre == BibTexUtil.Genre.mastersthesis
                        || bibGenre == BibTexUtil.Genre.phdthesis || bibGenre == BibTexUtil.Genre.techreport)) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("school").toString()), false));
                } else if (fields.get("institution") != null) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("institution").toString()), false));
                } else if (fields.get("publisher") == null && fields.get("school") == null
                        && fields.get("institution") == null && fields.get("address") != null) {
                    sourceVO.getPublishingInfo().setPublisher("ANY PUBLISHER");
                }
            }
            // series
            if (fields.get("series") != null) {
                if (bibGenre == BibTexUtil.Genre.book || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.techreport) {
                    sourceVO.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("series").toString()), false));
                    sourceVO.setGenre(SourceVO.Genre.SERIES);
                } else if (bibGenre == BibTexUtil.Genre.inbook || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.inproceedings
                        || bibGenre == BibTexUtil.Genre.conference) {
                    secondSourceVO.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("series").toString()), false));
                    secondSourceVO.setGenre(SourceVO.Genre.SERIES);
                }
            }
            // type --> degree
            if (fields.get("type") != null && bibGenre == BibTexUtil.Genre.mastersthesis) {
                if (fields.get("type").toString().toLowerCase().contains("master")
                        || fields.get("type").toString().toLowerCase().contains("m.a.")
                        || fields.get("type").toString().toLowerCase().contains("m.s.")
                        || fields.get("type").toString().toLowerCase().contains("m.sc.")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.MASTER);
                } else if (fields.get("type").toString().toLowerCase().contains("bachelor")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.BACHELOR);
                } else if (fields.get("type").toString().toLowerCase().contains("magister")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.MAGISTER);
                } else if (fields.get("type").toString().toLowerCase().contains("diplom")) // covers also
                                                                                           // the english
                                                                                           // version
                                                                                           // (diploma)
                {
                    mds.setDegree(MdsPublicationVO.DegreeType.DIPLOMA);
                } else if (fields.get("type").toString().toLowerCase().contains("statsexamen")
                        || fields.get("type").toString().toLowerCase().contains("state examination")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.DIPLOMA);
                }
            } else if (fields.get("type") != null && bibGenre == BibTexUtil.Genre.phdthesis) {
                if (fields.get("type").toString().toLowerCase().contains("phd")
                        || fields.get("type").toString().toLowerCase().contains("dissertation")
                        || fields.get("type").toString().toLowerCase().contains("doktor")
                        || fields.get("type").toString().toLowerCase().contains("doctor")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.PHD);
                } else if (fields.get("type").toString().toLowerCase().contains("habilitation")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.HABILITATION);
                }
            }
            // volume
            if (fields.get("volume") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.book) {
                    sourceVO.setVolume(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                } else if (bibGenre == BibTexUtil.Genre.inbook || bibGenre == BibTexUtil.Genre.inproceedings
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()) {
                        sourceVO.getSources().get(0).setVolume(BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                    } else {
                        sourceVO.setVolume(BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                    }
                }
            }
            // event infos
            if (bibGenre != null && (bibGenre.equals(BibTexUtil.Genre.inproceedings)
                    || bibGenre.equals(BibTexUtil.Genre.proceedings)
                    || bibGenre.equals(BibTexUtil.Genre.conference) || bibGenre.equals(BibTexUtil.Genre.poster)
                    || bibGenre.equals(BibTexUtil.Genre.talk))) {
                EventVO event = new EventVO();
                boolean eventNotEmpty = false;
                // event location
                if (fields.get("location") != null) {
                    event.setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("location").toString()), false));
                    eventNotEmpty = true;
                }
                // event place
                else if (fields.get("event_place") != null) {
                    event.setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("event_place").toString()), false));
                    eventNotEmpty = true;
                }
                // event name/title
                if (fields.get("event_name") != null) {
                    event.setTitle(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("event_name").toString()), false));
                    eventNotEmpty = true;
                }
                // event will be set only it's not empty
                if (eventNotEmpty == true) {
                    if (event.getTitle() == null) {
                        event.setTitle("");
                    }
                    mds.setEvent(event);
                }
            }
            // year, month
            String dateString = null;
            if (fields.get("year") != null) {
                dateString = BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("year").toString()),
                        false);
                if (fields.get("month") != null) {
                    String month = BibTexUtil.parseMonth(fields.get("month").toString());
                    dateString += "-" + month;
                }
                if (bibGenre == BibTexUtil.Genre.unpublished) {
                    mds.setDateCreated(dateString);
                } else {
                    mds.setDatePublishedInPrint(dateString);
                }
            }
            String affiliation = null;
            String affiliationAddress = null;
            // affiliation
            if (fields.get("affiliation") != null) {
                affiliation = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("affiliation").toString()), false);
            }
            // affiliationaddress
            if (fields.get("affiliationaddress") != null) {
                affiliationAddress = BibTexUtil.stripBraces(
                        BibTexUtil.bibtexDecode(fields.get("affiliationaddress").toString()), false);
            }
            // author
            boolean noConeAuthorFound = true;
            if (fields.get("author") != null) {
                if (fields.get("author") instanceof BibtexPersonList) {
                    BibtexPersonList authors = (BibtexPersonList) fields.get("author");
                    for (Object author : authors.getList()) {
                        if (author instanceof BibtexPerson) {
                            addCreator(mds, (BibtexPerson) author, CreatorVO.CreatorRole.AUTHOR, affiliation,
                                    affiliationAddress);
                        } else {
                            this.logger.warn("Entry in BibtexPersonList not a BibtexPerson: [" + author
                                    + "] in [" + author + "]");
                        }
                    }
                } else if (fields.get("author") instanceof BibtexPerson) {
                    BibtexPerson author = (BibtexPerson) fields.get("author");
                    addCreator(mds, (BibtexPerson) author, CreatorVO.CreatorRole.AUTHOR, affiliation,
                            affiliationAddress);
                } else if (fields.get("author") instanceof BibtexString) {
                    AuthorDecoder decoder;
                    try {
                        String authorString = BibTexUtil.bibtexDecode(fields.get("author").toString(), false);
                        List<CreatorVO> teams = new ArrayList<CreatorVO>();
                        if (authorString.contains("Team")) {
                            // set pattern for finding Teams (leaded or followed by [and|,|;|{|}|^|$])
                            Pattern pattern = Pattern.compile(
                                    "(?<=(and|,|;|\\{|^))([\\w|\\s]*?Team[\\w|\\s]*?)(?=(and|,|;|\\}|$))",
                                    Pattern.DOTALL);
                            Matcher matcher = pattern.matcher(authorString);
                            String matchedGroup;
                            while (matcher.find()) {
                                matchedGroup = matcher.group();
                                // remove matchedGroup (and prefix/suffix) from authorString
                                if (authorString.startsWith(matchedGroup)) {
                                    authorString = authorString.replaceAll(matchedGroup + "(and|,|;|\\})", "");
                                } else {
                                    authorString = authorString.replaceAll("(and|,|;|\\{)" + matchedGroup, "");
                                }
                                // set matchedGroup as Organisation Author
                                OrganizationVO team = new OrganizationVO();
                                team.setName(matchedGroup.trim());
                                CreatorVO creatorVO = new CreatorVO(team, CreatorVO.CreatorRole.AUTHOR);
                                teams.add(creatorVO);
                            }
                        }
                        decoder = new AuthorDecoder(authorString, false);
                        if (decoder.getBestFormat() != null) {
                            List<Author> authors = decoder.getAuthorListList().get(0);
                            for (Author author : authors) {
                                PersonVO personVO = new PersonVO();
                                personVO.setFamilyName(author.getSurname());
                                if (author.getGivenName() != null) {
                                    personVO.setGivenName(author.getGivenName());
                                } else {
                                    personVO.setGivenName(author.getInitial());
                                }
                                /*
                                 * Case for MPI-KYB (Biological Cybernetics) with CoNE identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (author.getTags().get("identifier") != null)) {
                                    String query = author.getTags().get("identifier");
                                    int affiliationsCount = Integer
                                            .parseInt(author.getTags().get("affiliationsCount"));
                                    if (affiliationsCount > 0
                                            || configuration.get("OrganizationalUnit") != null) {
                                        for (int ouCount = 0; ouCount < (affiliationsCount > 0
                                                ? affiliationsCount
                                                : 1); ouCount++) // 1
                                                                                                                                            // is
                                                                                                                                            // for
                                                                                                                                            // the
                                                                                                                                            // case
                                                                                                                                            // configuration.get("OrganizationalUnit")
                                                                                                                                            // !=
                                                                                                                                            // null
                                        {
                                            String organizationalUnit = (author.getTags().get(
                                                    "affiliation" + new Integer(ouCount).toString()) != null
                                                            ? author.getTags()
                                                                    .get("affiliation"
                                                                            + new Integer(ouCount).toString())
                                                            : (configuration.get("OrganizationalUnit") != null
                                                                    ? configuration.get("OrganizationalUnit")
                                                                    : ""));
                                            Node coneEntries = null;
                                            if (query.equals(author.getTags().get("identifier"))) {
                                                coneEntries = Util.queryConeExactWithIdentifier("persons",
                                                        query, organizationalUnit);
                                                // for MPIKYB due to OUs which do not occur in CoNE
                                                if (coneEntries.getFirstChild().getFirstChild() == null) {
                                                    logger.error("No Person with Identifier ("
                                                            + author.getTags().get("identifier") + ") and OU ("
                                                            + organizationalUnit
                                                            + ") found in CoNE for Publication \""
                                                            + fields.get("title") + "\"");
                                                }
                                            } else {
                                                coneEntries = Util.queryConeExact("persons", query,
                                                        organizationalUnit);
                                            }
                                            Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                            if (coneNode != null) {
                                                Node currentNode = coneNode.getFirstChild();
                                                boolean first = true;
                                                while (currentNode != null) {
                                                    if (currentNode.getNodeType() == Node.ELEMENT_NODE
                                                            && first) {
                                                        first = false;
                                                        noConeAuthorFound = false;
                                                        Node coneEntry = currentNode;
                                                        String coneId = coneEntry.getAttributes()
                                                                .getNamedItem("rdf:about").getNodeValue();
                                                        personVO.setIdentifier(
                                                                new IdentifierVO(IdType.CONE, coneId));
                                                        for (int i = 0; i < coneEntry.getChildNodes()
                                                                .getLength(); i++) {
                                                            Node posNode = coneEntry.getChildNodes().item(i);
                                                            if ("escidoc:position"
                                                                    .equals(posNode.getNodeName())) {
                                                                String from = null;
                                                                String until = null;
                                                                String name = null;
                                                                String id = null;
                                                                Node node = posNode.getFirstChild()
                                                                        .getFirstChild();
                                                                while (node != null) {
                                                                    if ("eprints:affiliatedInstitution"
                                                                            .equals(node.getNodeName())) {
                                                                        name = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:start-date"
                                                                            .equals(node.getNodeName())) {
                                                                        from = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:end-date"
                                                                            .equals(node.getNodeName())) {
                                                                        until = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("dc:identifier"
                                                                            .equals(node.getNodeName())) {
                                                                        id = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    }
                                                                    node = node.getNextSibling();
                                                                }
                                                                if (smaller(from, dateString)
                                                                        && smaller(dateString, until)) {
                                                                    OrganizationVO org = new OrganizationVO();
                                                                    org.setName(name);
                                                                    org.setIdentifier(id);
                                                                    personVO.getOrganizations().add(org);
                                                                }
                                                            }
                                                        }
                                                    } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                        throw new RuntimeException(
                                                                "Ambigous CoNE entries for " + query);
                                                    }
                                                    currentNode = currentNode.getNextSibling();
                                                }
                                            } else {
                                                throw new RuntimeException("Missing CoNE entry for " + query);
                                            }
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-Microstructure Physics with affiliation identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("affiliation id in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (author.getTags().get("identifier") != null)) {
                                    String identifier = author.getTags().get("identifier");
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    if (!("extern".equals(identifier))) {
                                        Node coneEntries = null;
                                        coneEntries = Util.queryConeExact("persons", query,
                                                (configuration.get("OrganizationalUnit") != null
                                                        ? configuration.get("OrganizationalUnit")
                                                        : ""));
                                        Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                        if (coneNode != null) {
                                            Node currentNode = coneNode.getFirstChild();
                                            boolean first = true;
                                            while (currentNode != null) {
                                                if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                    first = false;
                                                    noConeAuthorFound = false;
                                                    Node coneEntry = currentNode;
                                                    String coneId = coneEntry.getAttributes()
                                                            .getNamedItem("rdf:about").getNodeValue();
                                                    personVO.setIdentifier(
                                                            new IdentifierVO(IdType.CONE, coneId));
                                                    if (identifier != null && !("".equals(identifier))) {
                                                        try {
                                                            String ouSubTitle = identifier.substring(0,
                                                                    identifier.indexOf(","));
                                                            Document document = Util.queryFramework(
                                                                    "/oum/organizational-units?query="
                                                                            + URLEncoder.encode("\"/title\"=\""
                                                                                    + ouSubTitle + "\"",
                                                                                    "UTF-8"));
                                                            NodeList ouList = document.getElementsByTagNameNS(
                                                                    "http://www.escidoc.de/schemas/organizationalunit/0.8",
                                                                    "organizational-unit");
                                                            Element ou = (Element) ouList.item(0);
                                                            String href = ou.getAttribute("xlink:href");
                                                            String ouId = href
                                                                    .substring(href.lastIndexOf("/") + 1);
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(identifier);
                                                            org.setIdentifier(ouId);
                                                            personVO.getOrganizations().add(org);
                                                        } catch (Exception e) {
                                                            logger.error("Error getting OUs", e);
                                                            throw new RuntimeException(
                                                                    "Error getting Organizational Unit for "
                                                                            + identifier);
                                                        }
                                                    }
                                                } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                    throw new RuntimeException(
                                                            "Ambigous CoNE entries for " + query);
                                                }
                                                currentNode = currentNode.getNextSibling();
                                            }
                                        } else {
                                            throw new RuntimeException("Missing CoNE entry for " + query);
                                        }
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("empty brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors"))
                                                && (author.getTags().get("brackets") != null))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeAuthorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(name);
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    } else {
                                        throw new RuntimeException("Missing CoNE entry for " + query);
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("no".equals(configuration.get("CurlyBracketsForCoNEAuthors")))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeAuthorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(name);
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    }
                                }
                                if (affiliation != null) {
                                    OrganizationVO organization = new OrganizationVO();
                                    organization.setIdentifier(PropertyReader
                                            .getProperty("escidoc.pubman.external.organisation.id"));
                                    organization.setName(affiliation);
                                    organization.setAddress(affiliationAddress);
                                    personVO.getOrganizations().add(organization);
                                }
                                CreatorVO creatorVO = new CreatorVO(personVO, CreatorVO.CreatorRole.AUTHOR);
                                mds.getCreators().add(creatorVO);
                            }
                        }
                        if (!teams.isEmpty()) {
                            mds.getCreators().addAll(teams);
                        }
                    } catch (Exception e) {
                        this.logger.error("An error occured while getting field 'author'.", e);
                        throw new RuntimeException(e);
                    }
                }
            }
            // editor
            boolean noConeEditorFound = false;
            if (fields.get("editor") != null) {
                this.logger.debug("fields.get(\"editor\"): " + fields.get("editor").getClass());
                if (fields.get("editor") instanceof BibtexPersonList) {
                    BibtexPersonList editors = (BibtexPersonList) fields.get("editor");
                    for (Object editor : editors.getList()) {
                        if (editor instanceof BibtexPerson) {
                            addCreator(mds, (BibtexPerson) editor, CreatorVO.CreatorRole.EDITOR, affiliation,
                                    affiliationAddress);
                        } else {
                            this.logger.warn("Entry in BibtexPersonList not a BibtexPerson: [" + editor
                                    + "] in [" + editors + "]");
                        }
                    }
                } else if (fields.get("editor") instanceof BibtexPerson) {
                    BibtexPerson editor = (BibtexPerson) fields.get("editor");
                    addCreator(mds, (BibtexPerson) editor, CreatorVO.CreatorRole.EDITOR, affiliation,
                            affiliationAddress);
                } else if (fields.get("editor") instanceof BibtexString) {
                    AuthorDecoder decoder;
                    try {
                        String editorString = BibTexUtil.bibtexDecode(fields.get("editor").toString(), false);
                        List<CreatorVO> teams = new ArrayList<CreatorVO>();
                        if (editorString.contains("Team")) {
                            // set pattern for finding Teams (leaded or followed by [and|,|;|{|}|^|$])
                            Pattern pattern = Pattern.compile(
                                    "(?<=(and|,|;|\\{|^))([\\w|\\s]*?Team[\\w|\\s]*?)(?=(and|,|;|\\}|$))",
                                    Pattern.DOTALL);
                            Matcher matcher = pattern.matcher(editorString);
                            String matchedGroup;
                            while (matcher.find()) {
                                matchedGroup = matcher.group();
                                // remove matchedGroup (and prefix/suffix) from authorString
                                if (editorString.startsWith(matchedGroup)) {
                                    editorString = editorString.replaceAll(matchedGroup + "(and|,|;|\\})", "");
                                } else {
                                    editorString = editorString.replaceAll("(and|,|;|\\{)" + matchedGroup, "");
                                }
                                // set matchedGroup as Organisation Author
                                OrganizationVO team = new OrganizationVO();
                                team.setName(matchedGroup.trim());
                                CreatorVO creatorVO = new CreatorVO(team, CreatorVO.CreatorRole.EDITOR);
                                teams.add(creatorVO);
                            }
                        }
                        decoder = new AuthorDecoder(editorString, false);
                        if (decoder.getBestFormat() != null) {
                            List<Author> editors = decoder.getAuthorListList().get(0);
                            for (Author editor : editors) {
                                PersonVO personVO = new PersonVO();
                                personVO.setFamilyName(editor.getSurname());
                                if (editor.getGivenName() != null) {
                                    personVO.setGivenName(editor.getGivenName());
                                } else {
                                    personVO.setGivenName(editor.getInitial());
                                }
                                /*
                                 * Case for MPI-KYB (Biological Cybernetics) with CoNE identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (editor.getTags().get("identifier") != null)) {
                                    String query = editor.getTags().get("identifier");
                                    int affiliationsCount = Integer
                                            .parseInt(editor.getTags().get("affiliationsCount"));
                                    if (affiliationsCount > 0
                                            || configuration.get("OrganizationalUnit") != null) {
                                        for (int ouCount = 0; ouCount < (affiliationsCount > 0
                                                ? affiliationsCount
                                                : 1); ouCount++) // 1
                                                                                                                                            // is
                                                                                                                                            // for
                                                                                                                                            // the
                                                                                                                                            // case
                                                                                                                                            // configuration.get("OrganizationalUnit")
                                                                                                                                            // !=
                                                                                                                                            // null
                                        {
                                            String organizationalUnit = (editor.getTags().get(
                                                    "affiliation" + new Integer(ouCount).toString()) != null
                                                            ? editor.getTags()
                                                                    .get("affiliation"
                                                                            + new Integer(ouCount).toString())
                                                            : (configuration.get("OrganizationalUnit") != null
                                                                    ? configuration.get("OrganizationalUnit")
                                                                    : ""));
                                            Node coneEntries = null;
                                            if (query.equals(editor.getTags().get("identifier"))) {
                                                coneEntries = Util.queryConeExactWithIdentifier("persons",
                                                        query, organizationalUnit);
                                                // for MPIKYB due to OUs which do not occur in CoNE
                                                if (coneEntries.getFirstChild().getFirstChild() == null) {
                                                    logger.error("No Person with Identifier ("
                                                            + editor.getTags().get("identifier") + ") and OU ("
                                                            + organizationalUnit
                                                            + ") found in CoNE for Publication \""
                                                            + fields.get("title") + "\"");
                                                }
                                            } else {
                                                coneEntries = Util.queryConeExact("persons", query,
                                                        organizationalUnit);
                                            }
                                            Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                            if (coneNode != null) {
                                                Node currentNode = coneNode.getFirstChild();
                                                boolean first = true;
                                                while (currentNode != null) {
                                                    if (currentNode.getNodeType() == Node.ELEMENT_NODE
                                                            && first) {
                                                        first = false;
                                                        noConeEditorFound = false;
                                                        Node coneEntry = currentNode;
                                                        String coneId = coneEntry.getAttributes()
                                                                .getNamedItem("rdf:about").getNodeValue();
                                                        personVO.setIdentifier(
                                                                new IdentifierVO(IdType.CONE, coneId));
                                                        for (int i = 0; i < coneEntry.getChildNodes()
                                                                .getLength(); i++) {
                                                            Node posNode = coneEntry.getChildNodes().item(i);
                                                            if ("escidoc:position"
                                                                    .equals(posNode.getNodeName())) {
                                                                String from = null;
                                                                String until = null;
                                                                String name = null;
                                                                String id = null;
                                                                Node node = posNode.getFirstChild()
                                                                        .getFirstChild();
                                                                while (node != null) {
                                                                    if ("eprints:affiliatedInstitution"
                                                                            .equals(node.getNodeName())) {
                                                                        name = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:start-date"
                                                                            .equals(node.getNodeName())) {
                                                                        from = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:end-date"
                                                                            .equals(node.getNodeName())) {
                                                                        until = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("dc:identifier"
                                                                            .equals(node.getNodeName())) {
                                                                        id = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    }
                                                                    node = node.getNextSibling();
                                                                }
                                                                if (smaller(from, dateString)
                                                                        && smaller(dateString, until)) {
                                                                    OrganizationVO org = new OrganizationVO();
                                                                    org.setName(name);
                                                                    org.setIdentifier(id);
                                                                    personVO.getOrganizations().add(org);
                                                                }
                                                            }
                                                        }
                                                    } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                        throw new RuntimeException(
                                                                "Ambigous CoNE entries for " + query);
                                                    }
                                                    currentNode = currentNode.getNextSibling();
                                                }
                                            } else {
                                                throw new RuntimeException("Missing CoNE entry for " + query);
                                            }
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-Microstructure Physics with affiliation identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("affiliation id in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (editor.getTags().get("identifier") != null)) {
                                    String identifier = editor.getTags().get("identifier");
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    if (!("extern".equals(identifier))) {
                                        Node coneEntries = null;
                                        coneEntries = Util.queryConeExact("persons", query,
                                                (configuration.get("OrganizationalUnit") != null
                                                        ? configuration.get("OrganizationalUnit")
                                                        : ""));
                                        Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                        if (coneNode != null) {
                                            Node currentNode = coneNode.getFirstChild();
                                            boolean first = true;
                                            while (currentNode != null) {
                                                if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                    first = false;
                                                    noConeAuthorFound = false;
                                                    Node coneEntry = currentNode;
                                                    String coneId = coneEntry.getAttributes()
                                                            .getNamedItem("rdf:about").getNodeValue();
                                                    personVO.setIdentifier(
                                                            new IdentifierVO(IdType.CONE, coneId));
                                                    if (identifier != null && !("".equals(identifier))) {
                                                        try {
                                                            String ouSubTitle = identifier.substring(0,
                                                                    identifier.indexOf(","));
                                                            Document document = Util.queryFramework(
                                                                    "/oum/organizational-units?query="
                                                                            + URLEncoder.encode("\"/title\"=\""
                                                                                    + ouSubTitle + "\"",
                                                                                    "UTF-8"));
                                                            NodeList ouList = document.getElementsByTagNameNS(
                                                                    "http://www.escidoc.de/schemas/organizationalunit/0.8",
                                                                    "organizational-unit");
                                                            Element ou = (Element) ouList.item(0);
                                                            String href = ou.getAttribute("xlink:href");
                                                            String ouId = href
                                                                    .substring(href.lastIndexOf("/") + 1);
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(identifier);
                                                            org.setIdentifier(ouId);
                                                            personVO.getOrganizations().add(org);
                                                        } catch (Exception e) {
                                                            logger.error("Error getting OUs", e);
                                                            throw new RuntimeException(
                                                                    "Error getting Organizational Unit for "
                                                                            + identifier);
                                                        }
                                                    }
                                                } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                    throw new RuntimeException(
                                                            "Ambigous CoNE entries for " + query);
                                                }
                                                currentNode = currentNode.getNextSibling();
                                            }
                                        } else {
                                            throw new RuntimeException("Missing CoNE entry for " + query);
                                        }
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("empty brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors"))
                                                && (editor.getTags().get("brackets") != null))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeEditorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(name);
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    } else {
                                        throw new RuntimeException("Missing CoNE entry for " + query);
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("no".equals(configuration.get("CurlyBracketsForCoNEAuthors")))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeEditorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(name);
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    }
                                }
                                if (affiliation != null) {
                                    OrganizationVO organization = new OrganizationVO();
                                    organization.setIdentifier(PropertyReader
                                            .getProperty("escidoc.pubman.external.organisation.id"));
                                    organization.setName(affiliation);
                                    organization.setAddress(affiliationAddress);
                                    personVO.getOrganizations().add(organization);
                                }
                                CreatorVO creatorVO = new CreatorVO(personVO, CreatorVO.CreatorRole.EDITOR);
                                if ((bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                                        || bibGenre == BibTexUtil.Genre.inproceedings
                                        || bibGenre == BibTexUtil.Genre.conference
                                        || bibGenre == BibTexUtil.Genre.incollection)
                                        && (sourceVO.getTitle() != null || sourceVO.getTitle() == null)) {
                                    sourceVO.getCreators().add(creatorVO);
                                } else {
                                    mds.getCreators().add(creatorVO);
                                }
                            }
                        }
                        if (!teams.isEmpty()) {
                            mds.getCreators().addAll(teams);
                        }
                    } catch (Exception e) {
                        this.logger.error("An error occured while getting field 'editor'.", e);
                        throw new RuntimeException(e);
                    }
                }
            }
            // No CoNE Author or Editor Found
            if (noConeAuthorFound == true && noConeEditorFound == true && configuration != null
                    && "true".equals(configuration.get("CoNE"))) {
                throw new RuntimeException("No CoNE-Author and no CoNE-Editor was found");
            }
            // If no affiliation is given, set the first author to "external"
            boolean affiliationFound = false;
            for (CreatorVO creator : mds.getCreators()) {
                if (creator.getPerson() != null && creator.getPerson().getOrganizations() != null) {
                    for (OrganizationVO organization : creator.getPerson().getOrganizations()) {
                        if (organization.getIdentifier() != null) {
                            affiliationFound = true;
                            break;
                        }
                    }
                }
            }
            if (!affiliationFound && mds.getCreators().size() > 0) {
                OrganizationVO externalOrganization = new OrganizationVO();
                externalOrganization.setName("External Organizations");
                try {
                    externalOrganization.setIdentifier(
                            PropertyReader.getProperty("escidoc.pubman.external.organisation.id"));
                } catch (Exception e) {
                    throw new RuntimeException("Property escidoc.pubman.external.organisation.id not found", e);
                }
                if (mds.getCreators().get(0).getPerson() != null) {
                    mds.getCreators().get(0).getPerson().getOrganizations().add(externalOrganization);
                }
            }
            // Mapping of "common" (maybe relevant), non standard BibTeX Entries
            // abstract
            if (fields.get("abstract") != null) {
                mds.getAbstracts().add(new AbstractVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("abstract").toString()), false)));
            }
            // contents
            if (fields.get("contents") != null) {
                mds.setTableOfContents(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("contents").toString()), false));
            }
            // isbn
            if (fields.get("isbn") != null) {
                if (bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO != null) {
                        sourceVO.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISBN, BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("isbn").toString()), false)));
                    }
                } else {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISBN, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("isbn").toString()), false)));
                }
            }
            // issn
            if (fields.get("issn") != null) {
                if (bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()) {
                        sourceVO.getSources().get(0).getIdentifiers()
                                .add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil.stripBraces(
                                        BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                    }
                } else if (bibGenre == BibTexUtil.Genre.article) {
                    if (sourceVO != null) {
                        sourceVO.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                    }
                } else {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                }
            }
            // keywords
            if (fields.get("keywords") != null) {
                mds.setFreeKeywords(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("keywords").toString()), false));
            }
            // language
            /*
             * if (fields.get("language") != null) {
             * mds.getLanguages().add(BibTexUtil.stripBraces(BibTexUtil
             * .bibtexDecode(fields.get("language").toString ()), false)); }
             */
            // subtitle
            if (fields.get("subtitle") != null) {
                mds.getAlternativeTitles().add(new AlternativeTitleVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("subtitle").toString()), false)));
            }
            // url is now mapped to locator
            if (fields.get("url") != null) {
                // mds.getIdentifiers().add(
                // new IdentifierVO(
                // IdentifierVO.IdType.URI,
                // BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("url").toString()), false)));
                FileVO locator = new FileVO();
                locator.setContent(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("url").toString()), false));
                locator.setName("Link");
                locator.setStorage(FileVO.Storage.EXTERNAL_URL);
                locator.setVisibility(FileVO.Visibility.PUBLIC);
                locator.setContentCategory(
                        "http://purl.org/escidoc/metadata/ves/content-categories/any-fulltext");
                MdsFileVO metadata = new MdsFileVO();
                metadata.setContentCategory(
                        "http://purl.org/escidoc/metadata/ves/content-categories/any-fulltext");
                metadata.setTitle("Link");
                locator.getMetadataSets().add(metadata);
                itemVO.getFiles().add(locator);
            }
            // web_url as URI-Identifier
            else if (fields.get("web_url") != null) {
                mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.URI, BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("web_url").toString()), false)));
            }
            // Prevent the creation of an empty source
            if (sourceVO.getTitle() != null && sourceVO.getTitle() != null && sourceVO.getTitle() != ""
                    && sourceVO.getGenre() != null) {
                mds.getSources().add(sourceVO);
                // Prevent the creation of an empty second
                if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()
                        && sourceVO.getSources().get(0) != null
                        && sourceVO.getSources().get(0).getTitle() != null
                        && sourceVO.getSources().get(0).getTitle() != null
                        && sourceVO.getSources().get(0).getTitle() != "") {
                    mds.getSources().add(sourceVO.getSources().get(0));
                }
            }
            // Prevent the creation of an empty second source
            if (secondSourceVO.getTitle() != null && secondSourceVO.getTitle() != null
                    && secondSourceVO.getTitle() != "" && secondSourceVO.getGenre() != null) {
                mds.getSources().add(secondSourceVO);
                // Prevent the creation of an empty second
                if (secondSourceVO.getSources() != null && !secondSourceVO.getSources().isEmpty()
                        && secondSourceVO.getSources().get(0) != null
                        && secondSourceVO.getSources().get(0).getTitle() != null
                        && secondSourceVO.getSources().get(0).getTitle() != null
                        && secondSourceVO.getSources().get(0).getTitle() != "") {
                    mds.getSources().add(secondSourceVO.getSources().get(0));
                }
            }
            // New mapping for MPIS
            // DOI
            if (fields.get("doi") != null) {
                mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.DOI,
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("doi").toString()), false)));
            }
            // eid
            if (fields.get("eid") != null) {
                if (mds.getSources().size() == 1) {
                    mds.getSources().get(0).setSequenceNumber(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("eid").toString()), false));
                }
            }
            // rev
            if (fields.get("rev") != null) {
                if ("Peer".equals(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("rev").toString()), false))) {
                    mds.setReviewMethod(ReviewMethod.PEER);
                } else if ("No review".equals(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("rev").toString()), false))) {
                    mds.setReviewMethod(ReviewMethod.NO_REVIEW);
                }
            }
            // MPG-Affil
            if (fields.get("MPG-Affil") != null) {
                if ("Peer".equals(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("MPG-Affil").toString()), false))) {
                    // TODO
                }
            }
            // MPIS Groups
            if (fields.get("group") != null) {
                String[] groups = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("group").toString()), false).split(",");
                for (String group : groups) {
                    group = group.trim();
                    if (!"".equals(group)) {
                        if (groupSet == null) {
                            try {
                                groupSet = loadGroupSet();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                        if (!groupSet.contains(group)) {
                            throw new RuntimeException("Group '" + group + "' not found.");
                        }
                        mds.getSubjects()
                                .add(new SubjectVO(group, null, SubjectClassification.MPIS_GROUPS.toString()));
                    }
                }
            }
            // MPIS Projects
            if (fields.get("project") != null) {
                String[] projects = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("project").toString()), false)
                        .split(",");
                for (String project : projects) {
                    project = project.trim();
                    if (!"".equals(project)) {
                        if (projectSet == null) {
                            try {
                                projectSet = loadProjectSet();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                        if (!projectSet.contains(project)) {
                            throw new RuntimeException("Project '" + project + "' not found.");
                        }
                        mds.getSubjects().add(
                                new SubjectVO(project, null, SubjectClassification.MPIS_PROJECTS.toString()));
                    }
                }
            }
            // Cite Key
            mds.getIdentifiers().add(new IdentifierVO(IdType.BIBTEX_CITEKEY, entry.getEntryKey()));
        } else if (object instanceof BibtexToplevelComment) {
            this.logger.debug("Comment found: " + ((BibtexToplevelComment) object).getContent());
        }
    }
    XmlTransforming xmlTransforming = new XmlTransformingBean();
    try {
        if (entryFound) {
            return xmlTransforming.transformToItem(itemVO);
        } else {
            this.logger.warn("No entry found in BibTex record.");
            throw new RuntimeException();
        }
    } catch (TechnicalException e) {
        this.logger.error("An error ocurred while transforming the item.");
        throw new RuntimeException(e);
    }
}

From source file:fr.paris.lutece.plugins.extend.service.content.ExtendableContentPostProcessor.java

/**
 * {@inheritDoc}//from w  w  w.  j a  v  a 2 s.  c o m
 */
@Override
public String process(HttpServletRequest request, String strContent) {
    String strHtmlContent = strContent;

    // Check if the process is carried out in client or server side
    boolean bClientSide = Boolean.valueOf(AppPropertiesService.getProperty(PROPERTY_CLIENT_SIDE, "false"));

    if (bClientSide) {
        // CLIENT SIDE
        int nPos = strHtmlContent.indexOf(END_BODY);

        if (nPos < 0) {
            AppLogService.error("ExtendableContentPostProcessor Service : no BODY end tag found");

            return strHtmlContent;
        }

        Map<String, Object> model = new HashMap<String, Object>();
        model.put(MARK_BASE_URL, AppPathService.getBaseUrl(request));
        model.put(MARK_REGEX_PATTERN, _strRegexPattern);

        HtmlTemplate template = AppTemplateService.getTemplate(TEMPLATE_CONTENT_POST_PROCESSOR,
                request.getLocale(), model);

        StringBuilder sb = new StringBuilder();
        sb.append(strHtmlContent.substring(0, nPos));
        sb.append(template.getHtml());
        sb.append(strHtmlContent.substring(nPos));
        strHtmlContent = sb.toString();
    } else {
        // SERVER SIDE

        /**
         * Replace all makers @Extender[<idResource>,<resourceType>,<extenderType>,<params>]@ to
         * the correct HTML content of the extender.
         * 1) First parse the content of the markers
         * 2) Get all information (idResource, resourceType, extenderType, params)
         * 3) Get the html content from the given information
         * 4) Replace the markers by the html content
         */

        // 1) First parse the content of the markers
        Matcher match = _regexPattern.matcher(strHtmlContent);
        Matcher parameterMatch = null;
        StringBuffer strResultHTML = new StringBuffer(strHtmlContent.length());
        while (match.find()) {
            String strMarker = match.group();

            // 2) Get all information (idResource, resourceType, extenderType, params)
            ResourceExtenderDTO resourceExtender = _mapper.map(match.group(1));
            boolean bParameteredId = StringUtils.equalsIgnoreCase(resourceExtender.getIdExtendableResource(),
                    EXTEND_PARAMETERED_ID);

            if (bParameteredId) {
                if (parameterMatch == null) {
                    parameterMatch = _extendedParameterRegexPattern.matcher(strHtmlContent);
                } else {
                    parameterMatch.reset();
                }

                while (parameterMatch.find()) {
                    ResourceExtenderDTO realResourceExtender = _mapper.map(parameterMatch.group(1));

                    if (StringUtils.equals(realResourceExtender.getExtendableResourceType(),
                            resourceExtender.getExtendableResourceType())
                            && StringUtils.equals(realResourceExtender.getExtenderType(),
                                    resourceExtender.getExtenderType())) {
                        resourceExtender
                                .setIdExtendableResource(realResourceExtender.getIdExtendableResource());

                        break;
                    }
                }
            }

            String strHtml = StringUtils.EMPTY;

            if (!bParameteredId || !StringUtils.equalsIgnoreCase(resourceExtender.getIdExtendableResource(),
                    EXTEND_PARAMETERED_ID)) {
                // 3) Get the html content from the given information
                if (!StringUtils.equals(resourceExtender.getExtendableResourceType(), Page.RESOURCE_TYPE)
                        || (StringUtils.isBlank(request.getParameter(PARAM_PAGE))
                                && StringUtils.isBlank(request.getParameter(PARAM_PORTLET_ID)))) {
                    strHtml = _extenderService.getContent(resourceExtender.getIdExtendableResource(),
                            resourceExtender.getExtendableResourceType(), resourceExtender.getExtenderType(),
                            resourceExtender.getParameters(), request);
                }
            }

            // 4) Replace the markers by the html content
            match.appendReplacement(strResultHTML, Matcher.quoteReplacement(strHtml));
        }
        match.appendTail(strResultHTML);
        strHtmlContent = strResultHTML.toString();
    }

    if (StringUtils.isNotBlank(_strExtenderParameterRegexPattern)) {
        strHtmlContent = _extendedParameterRegexPattern.matcher(strHtmlContent).replaceAll("");
    }

    return strHtmlContent;
}

From source file:com.android.email.activity.MessageView.java

/**
 * Reload the body from the provider cursor.  This must only be called from the UI thread.
 *
 * @param bodyText text part/*from   w w  w  .j  av  a  2s  . c om*/
 * @param bodyHtml html part
 *
 * TODO deal with html vs text and many other issues
 */
private void reloadUiFromBody(String bodyText, String bodyHtml) {
    String text = null;
    mHtmlTextRaw = null;
    boolean hasImages = false;

    if (bodyHtml == null) {
        text = bodyText;
        /*
         * Convert the plain text to HTML
         */
        StringBuffer sb = new StringBuffer("<html><body>");
        if (text != null) {
            // Escape any inadvertent HTML in the text message
            text = EmailHtmlUtil.escapeCharacterToDisplay(text);
            // Find any embedded URL's and linkify
            Matcher m = Patterns.WEB_URL.matcher(text);
            while (m.find()) {
                int start = m.start();
                /*
                 * WEB_URL_PATTERN may match domain part of email address. To detect
                 * this false match, the character just before the matched string
                 * should not be '@'.
                 */
                if (start == 0 || text.charAt(start - 1) != '@') {
                    String url = m.group();
                    Matcher proto = WEB_URL_PROTOCOL.matcher(url);
                    String link;
                    if (proto.find()) {
                        // This is work around to force URL protocol part be lower case,
                        // because WebView could follow only lower case protocol link.
                        link = proto.group().toLowerCase() + url.substring(proto.end());
                    } else {
                        // Patterns.WEB_URL matches URL without protocol part,
                        // so added default protocol to link.
                        link = "http://" + url;
                    }
                    String href = String.format("<a href=\"%s\">%s</a>", link, url);
                    m.appendReplacement(sb, href);
                } else {
                    m.appendReplacement(sb, "$0");
                }
            }
            m.appendTail(sb);
        }
        sb.append("</body></html>");
        text = sb.toString();
    } else {
        text = bodyHtml;
        mHtmlTextRaw = bodyHtml;
        hasImages = IMG_TAG_START_REGEX.matcher(text).find();
    }

    mShowPicturesSection.setVisibility(hasImages ? View.VISIBLE : View.GONE);
    if (mMessageContentView != null) {
        mMessageContentView.loadDataWithBaseURL("email://", text, "text/html", "utf-8", null);
    }

    // Ask for attachments after body
    mLoadAttachmentsTask = new LoadAttachmentsTask();
    mLoadAttachmentsTask.execute(mMessage.mId);
}

From source file:de.mpg.escidoc.services.transformation.transformations.commonPublicationFormats.Bibtex.java

/**
 * @param bibtex//from  www .  j a v a  2s  .c om
 * @return eSciDoc-publication item XML representation of this BibTeX entry
 * @throws RuntimeException
 */
public String getBibtex(String bibtex) throws RuntimeException {
    // Remove Math '$' from the whole BibTex-String
    Pattern mathPattern = Pattern.compile("(?sm)\\$(\\\\.*?)(?<!\\\\)\\$");
    Matcher mathMatcher = mathPattern.matcher(bibtex);
    StringBuffer sb = new StringBuffer();
    while (mathMatcher.find()) {
        mathMatcher.appendReplacement(sb, "$1");
    }
    mathMatcher.appendTail(sb);
    bibtex = sb.toString();
    BibtexParser parser = new BibtexParser(true);
    BibtexFile file = new BibtexFile();
    try {
        parser.parse(file, new StringReader(bibtex));
    } catch (Exception e) {
        this.logger.error("Error parsing BibTex record.");
        throw new RuntimeException(e);
    }
    PubItemVO itemVO = new PubItemVO();
    MdsPublicationVO mds = new MdsPublicationVO();
    itemVO.setMetadata(mds);
    List entries = file.getEntries();
    boolean entryFound = false;
    if (entries == null || entries.size() == 0) {
        this.logger.warn("No entry found in BibTex record.");
        throw new RuntimeException();
    }
    for (Object object : entries) {
        if (object instanceof BibtexEntry) {
            if (entryFound) {
                this.logger.error("Multiple entries in BibTex record.");
                throw new RuntimeException();
            }
            entryFound = true;
            BibtexEntry entry = (BibtexEntry) object;
            // genre
            BibTexUtil.Genre bibGenre;
            try {
                bibGenre = BibTexUtil.Genre.valueOf(entry.getEntryType());
            } catch (IllegalArgumentException iae) {
                bibGenre = BibTexUtil.Genre.misc;
                this.logger.warn("Unrecognized genre: " + entry.getEntryType());
            }
            MdsPublicationVO.Genre itemGenre = BibTexUtil.getGenreMapping().get(bibGenre);
            mds.setGenre(itemGenre);
            SourceVO sourceVO = new SourceVO(new TextVO());
            SourceVO secondSourceVO = new SourceVO(new TextVO());
            Map fields = entry.getFields();
            // Mapping of BibTeX Standard Entries
            // title
            if (fields.get("title") != null) {
                if (fields.get("chapter") != null) {
                    mds.setTitle(new TextVO(BibTexUtil.stripBraces(
                            BibTexUtil.bibtexDecode(fields.get("chapter").toString()), false) + " - "
                            + BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("title").toString()),
                                    false)));
                } else {
                    mds.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("title").toString()), false)));
                }
            }
            // booktitle
            if (fields.get("booktitle") != null) {
                if (bibGenre == BibTexUtil.Genre.book) {
                    mds.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("booktitle").toString()), false)));
                } else if (bibGenre == BibTexUtil.Genre.conference || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.inproceedings) {
                    sourceVO.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("booktitle").toString()), false)));
                    if (bibGenre == BibTexUtil.Genre.conference || bibGenre == BibTexUtil.Genre.inproceedings) {
                        sourceVO.setGenre(Genre.PROCEEDINGS);
                    } else if (bibGenre == BibTexUtil.Genre.inbook
                            || bibGenre == BibTexUtil.Genre.incollection) {
                        sourceVO.setGenre(Genre.BOOK);
                    }
                }
            }
            // fjournal, journal
            if (fields.get("fjournal") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.unpublished) {
                    sourceVO.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("fjournal").toString()), false)));
                    sourceVO.setGenre(SourceVO.Genre.JOURNAL);
                    if (fields.get("journal") != null) {
                        sourceVO.getAlternativeTitles().add(new TextVO(BibTexUtil.stripBraces(
                                BibTexUtil.bibtexDecode(fields.get("journal").toString()), false)));
                    }
                }
            } else if (fields.get("journal") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.unpublished
                        || bibGenre == BibTexUtil.Genre.inproceedings) {
                    sourceVO.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("journal").toString()), false)));
                    sourceVO.setGenre(SourceVO.Genre.JOURNAL);
                }
            }
            // number
            if (fields.get("number") != null && bibGenre != BibTexUtil.Genre.techreport) {
                sourceVO.setIssue(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("number").toString()), false));
            } else if (fields.get("number") != null && bibGenre == BibTexUtil.Genre.techreport) {
                {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.REPORT_NR, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("number").toString()), false)));
                }
            }
            // pages
            if (fields.get("pages") != null) {
                if (bibGenre == BibTexUtil.Genre.book || bibGenre == BibTexUtil.Genre.proceedings) {
                    mds.setTotalNumberOfPages(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("pages").toString()), false));
                } else {
                    BibTexUtil.fillSourcePages(BibTexUtil.stripBraces(
                            BibTexUtil.bibtexDecode(fields.get("pages").toString()), false), sourceVO);
                    if (bibGenre == BibTexUtil.Genre.inproceedings
                            && (fields.get("booktitle") == null || fields.get("booktitle").toString() == "")
                            && (fields.get("event_name") != null
                                    && fields.get("event_name").toString() != "")) {
                        sourceVO.setTitle(
                                new TextVO(BibTexUtil.stripBraces(fields.get("event_name").toString(), false)));
                        sourceVO.setGenre(Genre.PROCEEDINGS);
                    }
                }
            }
            // Publishing info
            PublishingInfoVO publishingInfoVO = new PublishingInfoVO();
            mds.setPublishingInfo(publishingInfoVO);
            // address
            if (fields.get("address") != null) {
                if (!(bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.conference
                        || bibGenre == BibTexUtil.Genre.incollection)
                        && (sourceVO.getTitle() == null || sourceVO.getTitle().getValue() == null)) {
                    publishingInfoVO.setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("address").toString()), false));
                } else {
                    if (sourceVO.getPublishingInfo() == null) {
                        PublishingInfoVO sourcePublishingInfoVO = new PublishingInfoVO();
                        sourceVO.setPublishingInfo(sourcePublishingInfoVO);
                    }
                    sourceVO.getPublishingInfo().setPlace(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("address").toString()), false));
                }
            }
            // edition
            if (fields.get("edition") != null) {
                publishingInfoVO.setEdition(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("edition").toString()), false));
            }
            // publisher
            if (!(bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                    || bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.conference
                    || bibGenre == BibTexUtil.Genre.incollection)
                    && (sourceVO.getTitle() == null || sourceVO.getTitle().getValue() == null)) {
                if (fields.get("publisher") != null) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("publisher").toString()), false));
                } else if (fields.get("school") != null && (bibGenre == BibTexUtil.Genre.mastersthesis
                        || bibGenre == BibTexUtil.Genre.phdthesis || bibGenre == BibTexUtil.Genre.techreport)) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("school").toString()), false));
                } else if (fields.get("institution") != null) {
                    publishingInfoVO.setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("institution").toString()), false));
                } else if (fields.get("publisher") == null && fields.get("school") == null
                        && fields.get("institution") == null && fields.get("address") != null) {
                    publishingInfoVO.setPublisher("ANY PUBLISHER");
                }
            } else {
                if (sourceVO.getPublishingInfo() == null) {
                    PublishingInfoVO sourcePublishingInfoVO = new PublishingInfoVO();
                    sourceVO.setPublishingInfo(sourcePublishingInfoVO);
                }
                if (fields.get("publisher") != null) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("publisher").toString()), false));
                } else if (fields.get("school") != null && (bibGenre == BibTexUtil.Genre.mastersthesis
                        || bibGenre == BibTexUtil.Genre.phdthesis || bibGenre == BibTexUtil.Genre.techreport)) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("school").toString()), false));
                } else if (fields.get("institution") != null) {
                    sourceVO.getPublishingInfo().setPublisher(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("institution").toString()), false));
                } else if (fields.get("publisher") == null && fields.get("school") == null
                        && fields.get("institution") == null && fields.get("address") != null) {
                    sourceVO.getPublishingInfo().setPublisher("ANY PUBLISHER");
                }
            }
            // series
            if (fields.get("series") != null) {
                if (bibGenre == BibTexUtil.Genre.book || bibGenre == BibTexUtil.Genre.misc
                        || bibGenre == BibTexUtil.Genre.techreport) {
                    sourceVO.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("series").toString()), false)));
                    sourceVO.setGenre(SourceVO.Genre.SERIES);
                } else if (bibGenre == BibTexUtil.Genre.inbook || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.inproceedings
                        || bibGenre == BibTexUtil.Genre.conference) {
                    secondSourceVO.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("series").toString()), false)));
                    secondSourceVO.setGenre(SourceVO.Genre.SERIES);
                }
            }
            // type --> degree
            if (fields.get("type") != null && bibGenre == BibTexUtil.Genre.mastersthesis) {
                if (fields.get("type").toString().toLowerCase().contains("master")
                        || fields.get("type").toString().toLowerCase().contains("m.a.")
                        || fields.get("type").toString().toLowerCase().contains("m.s.")
                        || fields.get("type").toString().toLowerCase().contains("m.sc.")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.MASTER);
                } else if (fields.get("type").toString().toLowerCase().contains("bachelor")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.BACHELOR);
                } else if (fields.get("type").toString().toLowerCase().contains("magister")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.MAGISTER);
                } else if (fields.get("type").toString().toLowerCase().contains("diplom")) // covers also the english
                                                                                           // version (diploma)
                {
                    mds.setDegree(MdsPublicationVO.DegreeType.DIPLOMA);
                } else if (fields.get("type").toString().toLowerCase().contains("statsexamen")
                        || fields.get("type").toString().toLowerCase().contains("state examination")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.DIPLOMA);
                }
            } else if (fields.get("type") != null && bibGenre == BibTexUtil.Genre.phdthesis) {
                if (fields.get("type").toString().toLowerCase().contains("phd")
                        || fields.get("type").toString().toLowerCase().contains("dissertation")
                        || fields.get("type").toString().toLowerCase().contains("doktor")
                        || fields.get("type").toString().toLowerCase().contains("doctor")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.PHD);
                } else if (fields.get("type").toString().toLowerCase().contains("habilitation")) {
                    mds.setDegree(MdsPublicationVO.DegreeType.HABILITATION);
                }
            }
            // volume
            if (fields.get("volume") != null) {
                if (bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.book) {
                    sourceVO.setVolume(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                } else if (bibGenre == BibTexUtil.Genre.inbook || bibGenre == BibTexUtil.Genre.inproceedings
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()) {
                        sourceVO.getSources().get(0).setVolume(BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                    } else {
                        sourceVO.setVolume(BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("volume").toString()), false));
                    }
                }
            }
            // event infos
            if (bibGenre != null && (bibGenre.equals(BibTexUtil.Genre.inproceedings)
                    || bibGenre.equals(BibTexUtil.Genre.proceedings)
                    || bibGenre.equals(BibTexUtil.Genre.conference) || bibGenre.equals(BibTexUtil.Genre.poster)
                    || bibGenre.equals(BibTexUtil.Genre.talk))) {
                EventVO event = new EventVO();
                boolean eventNotEmpty = false;
                // event location
                if (fields.get("location") != null) {
                    event.setPlace(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("location").toString()), false)));
                    eventNotEmpty = true;
                }
                // event place
                else if (fields.get("event_place") != null) {
                    event.setPlace(new TextVO(BibTexUtil.stripBraces(
                            BibTexUtil.bibtexDecode(fields.get("event_place").toString()), false)));
                    eventNotEmpty = true;
                }
                // event name/title
                if (fields.get("event_name") != null) {
                    event.setTitle(new TextVO(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("event_name").toString()), false)));
                    eventNotEmpty = true;
                }
                // event will be set only it's not empty
                if (eventNotEmpty == true) {
                    if (event.getTitle() == null) {
                        event.setTitle(new TextVO());
                    }
                    mds.setEvent(event);
                }
            }
            // year, month
            String dateString = null;
            if (fields.get("year") != null) {
                dateString = BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("year").toString()),
                        false);
                if (fields.get("month") != null) {
                    String month = BibTexUtil.parseMonth(fields.get("month").toString());
                    dateString += "-" + month;
                }
                if (bibGenre == BibTexUtil.Genre.unpublished) {
                    mds.setDateCreated(dateString);
                } else {
                    mds.setDatePublishedInPrint(dateString);
                }
            }
            String affiliation = null;
            String affiliationAddress = null;
            // affiliation
            if (fields.get("affiliation") != null) {
                affiliation = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("affiliation").toString()), false);
            }
            // affiliationaddress
            if (fields.get("affiliationaddress") != null) {
                affiliationAddress = BibTexUtil.stripBraces(
                        BibTexUtil.bibtexDecode(fields.get("affiliationaddress").toString()), false);
            }
            // author
            boolean noConeAuthorFound = true;
            if (fields.get("author") != null) {
                if (fields.get("author") instanceof BibtexPersonList) {
                    BibtexPersonList authors = (BibtexPersonList) fields.get("author");
                    for (Object author : authors.getList()) {
                        if (author instanceof BibtexPerson) {
                            addCreator(mds, (BibtexPerson) author, CreatorVO.CreatorRole.AUTHOR, affiliation,
                                    affiliationAddress);
                        } else {
                            this.logger.warn("Entry in BibtexPersonList not a BibtexPerson: [" + author
                                    + "] in [" + author + "]");
                        }
                    }
                } else if (fields.get("author") instanceof BibtexPerson) {
                    BibtexPerson author = (BibtexPerson) fields.get("author");
                    addCreator(mds, (BibtexPerson) author, CreatorVO.CreatorRole.AUTHOR, affiliation,
                            affiliationAddress);
                } else if (fields.get("author") instanceof BibtexString) {
                    AuthorDecoder decoder;
                    try {
                        String authorString = BibTexUtil.bibtexDecode(fields.get("author").toString(), false);
                        List<CreatorVO> teams = new ArrayList<CreatorVO>();
                        if (authorString.contains("Team")) {
                            // set pattern for finding Teams (leaded or followed by [and|,|;|{|}|^|$])
                            Pattern pattern = Pattern.compile(
                                    "(?<=(and|,|;|\\{|^))([\\w|\\s]*?Team[\\w|\\s]*?)(?=(and|,|;|\\}|$))",
                                    Pattern.DOTALL);
                            Matcher matcher = pattern.matcher(authorString);
                            String matchedGroup;
                            while (matcher.find()) {
                                matchedGroup = matcher.group();
                                // remove matchedGroup (and prefix/suffix) from authorString
                                if (authorString.startsWith(matchedGroup)) {
                                    authorString = authorString.replaceAll(matchedGroup + "(and|,|;|\\})", "");
                                } else {
                                    authorString = authorString.replaceAll("(and|,|;|\\{)" + matchedGroup, "");
                                }
                                // set matchedGroup as Organisation Author
                                OrganizationVO team = new OrganizationVO();
                                team.setName(new TextVO(matchedGroup.trim()));
                                CreatorVO creatorVO = new CreatorVO(team, CreatorVO.CreatorRole.AUTHOR);
                                teams.add(creatorVO);
                            }
                        }
                        decoder = new AuthorDecoder(authorString, false);
                        if (decoder.getBestFormat() != null) {
                            List<Author> authors = decoder.getAuthorListList().get(0);
                            for (Author author : authors) {
                                PersonVO personVO = new PersonVO();
                                personVO.setFamilyName(author.getSurname());
                                if (author.getGivenName() != null) {
                                    personVO.setGivenName(author.getGivenName());
                                } else {
                                    personVO.setGivenName(author.getInitial());
                                }
                                /*
                                 * Case for MPI-KYB (Biological Cybernetics) with CoNE identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (author.getTags().get("identifier") != null)) {
                                    String query = author.getTags().get("identifier");
                                    int affiliationsCount = Integer
                                            .parseInt(author.getTags().get("affiliationsCount"));
                                    if (affiliationsCount > 0
                                            || configuration.get("OrganizationalUnit") != null) {
                                        for (int ouCount = 0; ouCount < (affiliationsCount > 0
                                                ? affiliationsCount
                                                : 1); ouCount++) // 1
                                                                                                                                            // is
                                                                                                                                            // for
                                                                                                                                            // the
                                                                                                                                            // case
                                                                                                                                            // configuration.get("OrganizationalUnit")
                                                                                                                                            // !=
                                                                                                                                            // null
                                        {
                                            String organizationalUnit = (author.getTags().get(
                                                    "affiliation" + new Integer(ouCount).toString()) != null
                                                            ? author.getTags()
                                                                    .get("affiliation"
                                                                            + new Integer(ouCount).toString())
                                                            : (configuration.get("OrganizationalUnit") != null
                                                                    ? configuration.get("OrganizationalUnit")
                                                                    : ""));
                                            Node coneEntries = null;
                                            if (query.equals(author.getTags().get("identifier"))) {
                                                coneEntries = Util.queryConeExactWithIdentifier("persons",
                                                        query, organizationalUnit);
                                                // for MPIKYB due to OUs which do not occur in CoNE
                                                if (coneEntries.getFirstChild().getFirstChild() == null) {
                                                    logger.error("No Person with Identifier ("
                                                            + author.getTags().get("identifier") + ") and OU ("
                                                            + organizationalUnit
                                                            + ") found in CoNE for Publication \""
                                                            + fields.get("title") + "\"");
                                                }
                                            } else {
                                                coneEntries = Util.queryConeExact("persons", query,
                                                        organizationalUnit);
                                            }
                                            Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                            if (coneNode != null) {
                                                Node currentNode = coneNode.getFirstChild();
                                                boolean first = true;
                                                while (currentNode != null) {
                                                    if (currentNode.getNodeType() == Node.ELEMENT_NODE
                                                            && first) {
                                                        first = false;
                                                        noConeAuthorFound = false;
                                                        Node coneEntry = currentNode;
                                                        String coneId = coneEntry.getAttributes()
                                                                .getNamedItem("rdf:about").getNodeValue();
                                                        personVO.setIdentifier(
                                                                new IdentifierVO(IdType.CONE, coneId));
                                                        for (int i = 0; i < coneEntry.getChildNodes()
                                                                .getLength(); i++) {
                                                            Node posNode = coneEntry.getChildNodes().item(i);
                                                            if ("escidoc:position"
                                                                    .equals(posNode.getNodeName())) {
                                                                String from = null;
                                                                String until = null;
                                                                String name = null;
                                                                String id = null;
                                                                Node node = posNode.getFirstChild()
                                                                        .getFirstChild();
                                                                while (node != null) {
                                                                    if ("eprints:affiliatedInstitution"
                                                                            .equals(node.getNodeName())) {
                                                                        name = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:start-date"
                                                                            .equals(node.getNodeName())) {
                                                                        from = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:end-date"
                                                                            .equals(node.getNodeName())) {
                                                                        until = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("dc:identifier"
                                                                            .equals(node.getNodeName())) {
                                                                        id = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    }
                                                                    node = node.getNextSibling();
                                                                }
                                                                if (smaller(from, dateString)
                                                                        && smaller(dateString, until)) {
                                                                    OrganizationVO org = new OrganizationVO();
                                                                    org.setName(new TextVO(name));
                                                                    org.setIdentifier(id);
                                                                    personVO.getOrganizations().add(org);
                                                                }
                                                            }
                                                        }
                                                    } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                        throw new RuntimeException(
                                                                "Ambigous CoNE entries for " + query);
                                                    }
                                                    currentNode = currentNode.getNextSibling();
                                                }
                                            } else {
                                                throw new RuntimeException("Missing CoNE entry for " + query);
                                            }
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-Microstructure Physics with affiliation identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("affiliation id in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (author.getTags().get("identifier") != null)) {
                                    String identifier = author.getTags().get("identifier");
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    if (!("extern".equals(identifier))) {
                                        Node coneEntries = null;
                                        coneEntries = Util.queryConeExact("persons", query,
                                                (configuration.get("OrganizationalUnit") != null
                                                        ? configuration.get("OrganizationalUnit")
                                                        : ""));
                                        Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                        if (coneNode != null) {
                                            Node currentNode = coneNode.getFirstChild();
                                            boolean first = true;
                                            while (currentNode != null) {
                                                if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                    first = false;
                                                    noConeAuthorFound = false;
                                                    Node coneEntry = currentNode;
                                                    String coneId = coneEntry.getAttributes()
                                                            .getNamedItem("rdf:about").getNodeValue();
                                                    personVO.setIdentifier(
                                                            new IdentifierVO(IdType.CONE, coneId));
                                                    if (identifier != null && !("".equals(identifier))) {
                                                        try {
                                                            String ouSubTitle = identifier.substring(0,
                                                                    identifier.indexOf(","));
                                                            Document document = Util.queryFramework(
                                                                    "/oum/organizational-units?query="
                                                                            + URLEncoder.encode("\"/title\"=\""
                                                                                    + ouSubTitle + "\"",
                                                                                    "UTF-8"));
                                                            NodeList ouList = document.getElementsByTagNameNS(
                                                                    "http://www.escidoc.de/schemas/organizationalunit/0.8",
                                                                    "organizational-unit");
                                                            Element ou = (Element) ouList.item(0);
                                                            String href = ou.getAttribute("xlink:href");
                                                            String ouId = href
                                                                    .substring(href.lastIndexOf("/") + 1);
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(identifier));
                                                            org.setIdentifier(ouId);
                                                            personVO.getOrganizations().add(org);
                                                        } catch (Exception e) {
                                                            logger.error("Error getting OUs", e);
                                                            throw new RuntimeException(
                                                                    "Error getting Organizational Unit for "
                                                                            + identifier);
                                                        }
                                                    }
                                                } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                    throw new RuntimeException(
                                                            "Ambigous CoNE entries for " + query);
                                                }
                                                currentNode = currentNode.getNextSibling();
                                            }
                                        } else {
                                            throw new RuntimeException("Missing CoNE entry for " + query);
                                        }
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("empty brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors"))
                                                && (author.getTags().get("brackets") != null))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeAuthorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(name));
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    } else {
                                        throw new RuntimeException("Missing CoNE entry for " + query);
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("no".equals(configuration.get("CurlyBracketsForCoNEAuthors")))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeAuthorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(name));
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-RA (Radio Astronomy) with identifier and affiliation in brackets
                                 * This Case is using NO CoNE!
                                 */
                                if (configuration != null && "false".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (author.getTags().get("identifier") != null)) {
                                    String identifier = author.getTags().get("identifier");
                                    String authoAffiliation = author.getTags().get("affiliation0");
                                    OrganizationVO org = new OrganizationVO();
                                    org.setName(new TextVO(authoAffiliation));
                                    org.setIdentifier(identifier);
                                    personVO.getOrganizations().add(org);
                                }
                                if (affiliation != null) {
                                    OrganizationVO organization = new OrganizationVO();
                                    organization.setIdentifier(PropertyReader
                                            .getProperty("escidoc.pubman.external.organisation.id"));
                                    organization.setName(new TextVO(affiliation));
                                    organization.setAddress(affiliationAddress);
                                    personVO.getOrganizations().add(organization);
                                }
                                CreatorVO creatorVO = new CreatorVO(personVO, CreatorVO.CreatorRole.AUTHOR);
                                mds.getCreators().add(creatorVO);
                            }
                        }
                        if (!teams.isEmpty()) {
                            mds.getCreators().addAll(teams);
                        }
                    } catch (Exception e) {
                        this.logger.error("An error occured while getting field 'author'.", e);
                        throw new RuntimeException(e);
                    }
                }
            }
            // editor
            boolean noConeEditorFound = false;
            if (fields.get("editor") != null) {
                this.logger.debug("fields.get(\"editor\"): " + fields.get("editor").getClass());
                if (fields.get("editor") instanceof BibtexPersonList) {
                    BibtexPersonList editors = (BibtexPersonList) fields.get("editor");
                    for (Object editor : editors.getList()) {
                        if (editor instanceof BibtexPerson) {
                            addCreator(mds, (BibtexPerson) editor, CreatorVO.CreatorRole.EDITOR, affiliation,
                                    affiliationAddress);
                        } else {
                            this.logger.warn("Entry in BibtexPersonList not a BibtexPerson: [" + editor
                                    + "] in [" + editors + "]");
                        }
                    }
                } else if (fields.get("editor") instanceof BibtexPerson) {
                    BibtexPerson editor = (BibtexPerson) fields.get("editor");
                    addCreator(mds, (BibtexPerson) editor, CreatorVO.CreatorRole.EDITOR, affiliation,
                            affiliationAddress);
                } else if (fields.get("editor") instanceof BibtexString) {
                    AuthorDecoder decoder;
                    try {
                        String editorString = BibTexUtil.bibtexDecode(fields.get("editor").toString(), false);
                        List<CreatorVO> teams = new ArrayList<CreatorVO>();
                        if (editorString.contains("Team")) {
                            // set pattern for finding Teams (leaded or followed by [and|,|;|{|}|^|$])
                            Pattern pattern = Pattern.compile(
                                    "(?<=(and|,|;|\\{|^))([\\w|\\s]*?Team[\\w|\\s]*?)(?=(and|,|;|\\}|$))",
                                    Pattern.DOTALL);
                            Matcher matcher = pattern.matcher(editorString);
                            String matchedGroup;
                            while (matcher.find()) {
                                matchedGroup = matcher.group();
                                // remove matchedGroup (and prefix/suffix) from authorString
                                if (editorString.startsWith(matchedGroup)) {
                                    editorString = editorString.replaceAll(matchedGroup + "(and|,|;|\\})", "");
                                } else {
                                    editorString = editorString.replaceAll("(and|,|;|\\{)" + matchedGroup, "");
                                }
                                // set matchedGroup as Organisation Author
                                OrganizationVO team = new OrganizationVO();
                                team.setName(new TextVO(matchedGroup.trim()));
                                CreatorVO creatorVO = new CreatorVO(team, CreatorVO.CreatorRole.EDITOR);
                                teams.add(creatorVO);
                            }
                        }
                        decoder = new AuthorDecoder(editorString, false);
                        if (decoder.getBestFormat() != null) {
                            List<Author> editors = decoder.getAuthorListList().get(0);
                            for (Author editor : editors) {
                                PersonVO personVO = new PersonVO();
                                personVO.setFamilyName(editor.getSurname());
                                if (editor.getGivenName() != null) {
                                    personVO.setGivenName(editor.getGivenName());
                                } else {
                                    personVO.setGivenName(editor.getInitial());
                                }
                                /*
                                 * Case for MPI-KYB (Biological Cybernetics) with CoNE identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (editor.getTags().get("identifier") != null)) {
                                    String query = editor.getTags().get("identifier");
                                    int affiliationsCount = Integer
                                            .parseInt(editor.getTags().get("affiliationsCount"));
                                    if (affiliationsCount > 0
                                            || configuration.get("OrganizationalUnit") != null) {
                                        for (int ouCount = 0; ouCount < (affiliationsCount > 0
                                                ? affiliationsCount
                                                : 1); ouCount++) // 1
                                                                                                                                            // is
                                                                                                                                            // for
                                                                                                                                            // the
                                                                                                                                            // case
                                                                                                                                            // configuration.get("OrganizationalUnit")
                                                                                                                                            // !=
                                                                                                                                            // null
                                        {
                                            String organizationalUnit = (editor.getTags().get(
                                                    "affiliation" + new Integer(ouCount).toString()) != null
                                                            ? editor.getTags()
                                                                    .get("affiliation"
                                                                            + new Integer(ouCount).toString())
                                                            : (configuration.get("OrganizationalUnit") != null
                                                                    ? configuration.get("OrganizationalUnit")
                                                                    : ""));
                                            Node coneEntries = null;
                                            if (query.equals(editor.getTags().get("identifier"))) {
                                                coneEntries = Util.queryConeExactWithIdentifier("persons",
                                                        query, organizationalUnit);
                                                // for MPIKYB due to OUs which do not occur in CoNE
                                                if (coneEntries.getFirstChild().getFirstChild() == null) {
                                                    logger.error("No Person with Identifier ("
                                                            + editor.getTags().get("identifier") + ") and OU ("
                                                            + organizationalUnit
                                                            + ") found in CoNE for Publication \""
                                                            + fields.get("title") + "\"");
                                                }
                                            } else {
                                                coneEntries = Util.queryConeExact("persons", query,
                                                        organizationalUnit);
                                            }
                                            Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                            if (coneNode != null) {
                                                Node currentNode = coneNode.getFirstChild();
                                                boolean first = true;
                                                while (currentNode != null) {
                                                    if (currentNode.getNodeType() == Node.ELEMENT_NODE
                                                            && first) {
                                                        first = false;
                                                        noConeEditorFound = false;
                                                        Node coneEntry = currentNode;
                                                        String coneId = coneEntry.getAttributes()
                                                                .getNamedItem("rdf:about").getNodeValue();
                                                        personVO.setIdentifier(
                                                                new IdentifierVO(IdType.CONE, coneId));
                                                        for (int i = 0; i < coneEntry.getChildNodes()
                                                                .getLength(); i++) {
                                                            Node posNode = coneEntry.getChildNodes().item(i);
                                                            if ("escidoc:position"
                                                                    .equals(posNode.getNodeName())) {
                                                                String from = null;
                                                                String until = null;
                                                                String name = null;
                                                                String id = null;
                                                                Node node = posNode.getFirstChild()
                                                                        .getFirstChild();
                                                                while (node != null) {
                                                                    if ("eprints:affiliatedInstitution"
                                                                            .equals(node.getNodeName())) {
                                                                        name = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:start-date"
                                                                            .equals(node.getNodeName())) {
                                                                        from = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("escidoc:end-date"
                                                                            .equals(node.getNodeName())) {
                                                                        until = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    } else if ("dc:identifier"
                                                                            .equals(node.getNodeName())) {
                                                                        id = node.getFirstChild()
                                                                                .getNodeValue();
                                                                    }
                                                                    node = node.getNextSibling();
                                                                }
                                                                if (smaller(from, dateString)
                                                                        && smaller(dateString, until)) {
                                                                    OrganizationVO org = new OrganizationVO();
                                                                    org.setName(new TextVO(name));
                                                                    org.setIdentifier(id);
                                                                    personVO.getOrganizations().add(org);
                                                                }
                                                            }
                                                        }
                                                    } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                        throw new RuntimeException(
                                                                "Ambigous CoNE entries for " + query);
                                                    }
                                                    currentNode = currentNode.getNextSibling();
                                                }
                                            } else {
                                                throw new RuntimeException("Missing CoNE entry for " + query);
                                            }
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-Microstructure Physics with affiliation identifier in brackets and
                                 * affiliations to adopt from CoNE for each author (also in brackets)
                                 */
                                else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("affiliation id in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (editor.getTags().get("identifier") != null)) {
                                    String identifier = editor.getTags().get("identifier");
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    if (!("extern".equals(identifier))) {
                                        Node coneEntries = null;
                                        coneEntries = Util.queryConeExact("persons", query,
                                                (configuration.get("OrganizationalUnit") != null
                                                        ? configuration.get("OrganizationalUnit")
                                                        : ""));
                                        Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                        if (coneNode != null) {
                                            Node currentNode = coneNode.getFirstChild();
                                            boolean first = true;
                                            while (currentNode != null) {
                                                if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                    first = false;
                                                    noConeAuthorFound = false;
                                                    Node coneEntry = currentNode;
                                                    String coneId = coneEntry.getAttributes()
                                                            .getNamedItem("rdf:about").getNodeValue();
                                                    personVO.setIdentifier(
                                                            new IdentifierVO(IdType.CONE, coneId));
                                                    if (identifier != null && !("".equals(identifier))) {
                                                        try {
                                                            String ouSubTitle = identifier.substring(0,
                                                                    identifier.indexOf(","));
                                                            Document document = Util.queryFramework(
                                                                    "/oum/organizational-units?query="
                                                                            + URLEncoder.encode("\"/title\"=\""
                                                                                    + ouSubTitle + "\"",
                                                                                    "UTF-8"));
                                                            NodeList ouList = document.getElementsByTagNameNS(
                                                                    "http://www.escidoc.de/schemas/organizationalunit/0.8",
                                                                    "organizational-unit");
                                                            Element ou = (Element) ouList.item(0);
                                                            String href = ou.getAttribute("xlink:href");
                                                            String ouId = href
                                                                    .substring(href.lastIndexOf("/") + 1);
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(identifier));
                                                            org.setIdentifier(ouId);
                                                            personVO.getOrganizations().add(org);
                                                        } catch (Exception e) {
                                                            logger.error("Error getting OUs", e);
                                                            throw new RuntimeException(
                                                                    "Error getting Organizational Unit for "
                                                                            + identifier);
                                                        }
                                                    }
                                                } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                    throw new RuntimeException(
                                                            "Ambigous CoNE entries for " + query);
                                                }
                                                currentNode = currentNode.getNextSibling();
                                            }
                                        } else {
                                            throw new RuntimeException("Missing CoNE entry for " + query);
                                        }
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("empty brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors"))
                                                && (editor.getTags().get("brackets") != null))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeEditorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(name));
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    } else {
                                        throw new RuntimeException("Missing CoNE entry for " + query);
                                    }
                                } else if (configuration != null && "true".equals(configuration.get("CoNE"))
                                        && ("no".equals(configuration.get("CurlyBracketsForCoNEAuthors")))) {
                                    String query = personVO.getFamilyName() + ", " + personVO.getGivenName();
                                    Node coneEntries = Util.queryConeExact("persons", query,
                                            (configuration.get("OrganizationalUnit") != null
                                                    ? configuration.get("OrganizationalUnit")
                                                    : ""));
                                    Node coneNode = coneEntries.getFirstChild().getFirstChild();
                                    if (coneNode != null) {
                                        Node currentNode = coneNode.getFirstChild();
                                        boolean first = true;
                                        while (currentNode != null) {
                                            if (currentNode.getNodeType() == Node.ELEMENT_NODE && first) {
                                                first = false;
                                                noConeEditorFound = false;
                                                Node coneEntry = currentNode;
                                                String coneId = coneEntry.getAttributes()
                                                        .getNamedItem("rdf:about").getNodeValue();
                                                personVO.setIdentifier(new IdentifierVO(IdType.CONE, coneId));
                                                for (int i = 0; i < coneEntry.getChildNodes()
                                                        .getLength(); i++) {
                                                    Node posNode = coneEntry.getChildNodes().item(i);
                                                    if ("escidoc:position".equals(posNode.getNodeName())) {
                                                        String from = null;
                                                        String until = null;
                                                        String name = null;
                                                        String id = null;
                                                        Node node = posNode.getFirstChild().getFirstChild();
                                                        while (node != null) {
                                                            if ("eprints:affiliatedInstitution"
                                                                    .equals(node.getNodeName())) {
                                                                name = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:start-date"
                                                                    .equals(node.getNodeName())) {
                                                                from = node.getFirstChild().getNodeValue();
                                                            } else if ("escidoc:end-date"
                                                                    .equals(node.getNodeName())) {
                                                                until = node.getFirstChild().getNodeValue();
                                                            } else if ("dc:identifier"
                                                                    .equals(node.getNodeName())) {
                                                                id = node.getFirstChild().getNodeValue();
                                                            }
                                                            node = node.getNextSibling();
                                                        }
                                                        if (smaller(from, dateString)
                                                                && smaller(dateString, until)) {
                                                            OrganizationVO org = new OrganizationVO();
                                                            org.setName(new TextVO(name));
                                                            org.setIdentifier(id);
                                                            personVO.getOrganizations().add(org);
                                                        }
                                                    }
                                                }
                                            } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                                                throw new RuntimeException(
                                                        "Ambigous CoNE entries for " + query);
                                            }
                                            currentNode = currentNode.getNextSibling();
                                        }
                                    }
                                }
                                /*
                                 * Case for MPI-RA (Radio Astronomy) with identifier and affiliation in brackets
                                 * This Case is using NO CoNE!
                                 */
                                if (configuration != null && "false".equals(configuration.get("CoNE"))
                                        && ("identifier and affiliation in brackets"
                                                .equals(configuration.get("CurlyBracketsForCoNEAuthors")))
                                        && (editor.getTags().get("identifier") != null)) {
                                    String identifier = editor.getTags().get("identifier");
                                    String authoAffiliation = editor.getTags().get("affiliation0");
                                    OrganizationVO org = new OrganizationVO();
                                    org.setName(new TextVO(authoAffiliation));
                                    org.setIdentifier(identifier);
                                    personVO.getOrganizations().add(org);
                                }
                                if (affiliation != null) {
                                    OrganizationVO organization = new OrganizationVO();
                                    organization.setIdentifier(PropertyReader
                                            .getProperty("escidoc.pubman.external.organisation.id"));
                                    organization.setName(new TextVO(affiliation));
                                    organization.setAddress(affiliationAddress);
                                    personVO.getOrganizations().add(organization);
                                }
                                CreatorVO creatorVO = new CreatorVO(personVO, CreatorVO.CreatorRole.EDITOR);
                                if ((bibGenre == BibTexUtil.Genre.article || bibGenre == BibTexUtil.Genre.inbook
                                        || bibGenre == BibTexUtil.Genre.inproceedings
                                        || bibGenre == BibTexUtil.Genre.conference
                                        || bibGenre == BibTexUtil.Genre.incollection)
                                        && (sourceVO.getTitle() != null
                                                || sourceVO.getTitle().getValue() == null)) {
                                    sourceVO.getCreators().add(creatorVO);
                                } else {
                                    mds.getCreators().add(creatorVO);
                                }
                            }
                        }
                        if (!teams.isEmpty()) {
                            mds.getCreators().addAll(teams);
                        }
                    } catch (Exception e) {
                        this.logger.error("An error occured while getting field 'editor'.", e);
                        throw new RuntimeException(e);
                    }
                }
            }
            // No CoNE Author or Editor Found
            if (noConeAuthorFound == true && noConeEditorFound == true && configuration != null
                    && "true".equals(configuration.get("CoNE"))) {
                throw new RuntimeException("No CoNE-Author and no CoNE-Editor was found");
            }
            // If no affiliation is given, set the first author to "external"
            boolean affiliationFound = false;
            for (CreatorVO creator : mds.getCreators()) {
                if (creator.getPerson() != null && creator.getPerson().getOrganizations() != null) {
                    for (OrganizationVO organization : creator.getPerson().getOrganizations()) {
                        if (organization.getIdentifier() != null) {
                            affiliationFound = true;
                            break;
                        }
                    }
                }
            }
            if (!affiliationFound && mds.getCreators().size() > 0) {
                OrganizationVO externalOrganization = new OrganizationVO();
                externalOrganization.setName(new TextVO("External Organizations"));
                try {
                    externalOrganization.setIdentifier(
                            PropertyReader.getProperty("escidoc.pubman.external.organisation.id"));
                } catch (Exception e) {
                    throw new RuntimeException("Property escidoc.pubman.external.organisation.id not found", e);
                }
                if (mds.getCreators().get(0).getPerson() != null) {
                    mds.getCreators().get(0).getPerson().getOrganizations().add(externalOrganization);
                }
            }
            // Mapping of "common" (maybe relevant), non standard BibTeX Entries
            // abstract
            if (fields.get("abstract") != null) {
                mds.getAbstracts().add(new TextVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("abstract").toString()), false)));
            }
            // contents
            if (fields.get("contents") != null) {
                mds.setTableOfContents(new TextVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("contents").toString()), false)));
            }
            // isbn
            if (fields.get("isbn") != null) {
                if (bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO != null) {
                        sourceVO.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISBN, BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("isbn").toString()), false)));
                    }
                } else {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISBN, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("isbn").toString()), false)));
                }
            }
            // issn
            if (fields.get("issn") != null) {
                if (bibGenre == BibTexUtil.Genre.inproceedings || bibGenre == BibTexUtil.Genre.inbook
                        || bibGenre == BibTexUtil.Genre.incollection
                        || bibGenre == BibTexUtil.Genre.conference) {
                    if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()) {
                        sourceVO.getSources().get(0).getIdentifiers()
                                .add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil.stripBraces(
                                        BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                    }
                } else if (bibGenre == BibTexUtil.Genre.article) {
                    if (sourceVO != null) {
                        sourceVO.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil
                                .stripBraces(BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                    }
                } else {
                    mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.ISSN, BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("issn").toString()), false)));
                }
            }
            // keywords
            if (fields.get("keywords") != null) {
                mds.setFreeKeywords(new TextVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("keywords").toString()), false)));
            }
            // language
            /*
             * if (fields.get("language") != null) {
             * mds.getLanguages().add(BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("language").toString
             * ()), false)); }
             */
            // subtitle
            if (fields.get("subtitle") != null) {
                mds.getAlternativeTitles().add(new TextVO(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("subtitle").toString()), false)));
            }
            // url is now mapped to locator
            if (fields.get("url") != null) {
                // mds.getIdentifiers().add(
                // new IdentifierVO(
                // IdentifierVO.IdType.URI,
                // BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("url").toString()), false)));
                FileVO locator = new FileVO();
                locator.setContent(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("url").toString()), false));
                locator.setName("Link");
                locator.setStorage(FileVO.Storage.EXTERNAL_URL);
                locator.setVisibility(FileVO.Visibility.PUBLIC);
                locator.setContentCategory(
                        "http://purl.org/escidoc/metadata/ves/content-categories/any-fulltext");
                MdsFileVO metadata = new MdsFileVO();
                metadata.setContentCategory(
                        "http://purl.org/escidoc/metadata/ves/content-categories/any-fulltext");
                metadata.setTitle(new TextVO("Link"));
                locator.getMetadataSets().add(metadata);
                itemVO.getFiles().add(locator);
            }
            // web_url as URI-Identifier
            else if (fields.get("web_url") != null) {
                mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.URI, BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("web_url").toString()), false)));
            }
            // Prevent the creation of an empty source
            if (sourceVO.getTitle() != null && sourceVO.getTitle().getValue() != null
                    && sourceVO.getTitle().getValue() != "" && sourceVO.getGenre() != null) {
                mds.getSources().add(sourceVO);
                // Prevent the creation of an empty second
                if (sourceVO.getSources() != null && !sourceVO.getSources().isEmpty()
                        && sourceVO.getSources().get(0) != null
                        && sourceVO.getSources().get(0).getTitle() != null
                        && sourceVO.getSources().get(0).getTitle().getValue() != null
                        && sourceVO.getSources().get(0).getTitle().getValue() != "") {
                    mds.getSources().add(sourceVO.getSources().get(0));
                }
            }
            // Prevent the creation of an empty second source
            if (secondSourceVO.getTitle() != null && secondSourceVO.getTitle().getValue() != null
                    && secondSourceVO.getTitle().getValue() != "" && secondSourceVO.getGenre() != null) {
                mds.getSources().add(secondSourceVO);
                // Prevent the creation of an empty second
                if (secondSourceVO.getSources() != null && !secondSourceVO.getSources().isEmpty()
                        && secondSourceVO.getSources().get(0) != null
                        && secondSourceVO.getSources().get(0).getTitle() != null
                        && secondSourceVO.getSources().get(0).getTitle().getValue() != null
                        && secondSourceVO.getSources().get(0).getTitle().getValue() != "") {
                    mds.getSources().add(secondSourceVO.getSources().get(0));
                }
            }
            // New mapping for MPIS
            // DOI
            if (fields.get("doi") != null) {
                mds.getIdentifiers().add(new IdentifierVO(IdentifierVO.IdType.DOI,
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("doi").toString()), false)));
            }
            // eid
            if (fields.get("eid") != null) {
                if (mds.getSources().size() == 1) {
                    mds.getSources().get(0).setSequenceNumber(BibTexUtil
                            .stripBraces(BibTexUtil.bibtexDecode(fields.get("eid").toString()), false));
                }
            }
            // rev
            if (fields.get("rev") != null) {
                if ("Peer".equals(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("rev").toString()), false))) {
                    mds.setReviewMethod(ReviewMethod.PEER);
                } else if ("No review".equals(
                        BibTexUtil.stripBraces(BibTexUtil.bibtexDecode(fields.get("rev").toString()), false))) {
                    mds.setReviewMethod(ReviewMethod.NO_REVIEW);
                }
            }
            // MPG-Affil
            if (fields.get("MPG-Affil") != null) {
                if ("Peer".equals(BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("MPG-Affil").toString()), false))) {
                    // TODO
                }
            }
            // MPIS Groups
            if (fields.get("group") != null) {
                String[] groups = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("group").toString()), false).split(",");
                for (String group : groups) {
                    group = group.trim();
                    if (!"".equals(group)) {
                        if (groupSet == null) {
                            try {
                                groupSet = loadGroupSet();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                        if (!groupSet.contains(group)) {
                            throw new RuntimeException("Group '" + group + "' not found.");
                        }
                        mds.getSubjects()
                                .add(new TextVO(group, null, SubjectClassification.MPIS_GROUPS.toString()));
                    }
                }
            }
            // MPIS Projects
            if (fields.get("project") != null) {
                String[] projects = BibTexUtil
                        .stripBraces(BibTexUtil.bibtexDecode(fields.get("project").toString()), false)
                        .split(",");
                for (String project : projects) {
                    project = project.trim();
                    if (!"".equals(project)) {
                        if (projectSet == null) {
                            try {
                                projectSet = loadProjectSet();
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                        if (!projectSet.contains(project)) {
                            throw new RuntimeException("Project '" + project + "' not found.");
                        }
                        mds.getSubjects()
                                .add(new TextVO(project, null, SubjectClassification.MPIS_PROJECTS.toString()));
                    }
                }
            }
            // Cite Key
            mds.getIdentifiers().add(new IdentifierVO(IdType.BIBTEX_CITEKEY, entry.getEntryKey()));
        } else if (object instanceof BibtexToplevelComment) {
            this.logger.debug("Comment found: " + ((BibtexToplevelComment) object).getContent());
        }
    }
    XmlTransforming xmlTransforming = new XmlTransformingBean();
    try {
        if (entryFound) {
            return xmlTransforming.transformToItem(itemVO);
        } else {
            this.logger.warn("No entry found in BibTex record.");
            throw new RuntimeException();
        }
    } catch (TechnicalException e) {
        this.logger.error("An error ocurred while transforming the item.");
        throw new RuntimeException(e);
    }
}

From source file:org.apache.synapse.mediators.transform.PayloadFactoryMediator.java

/**
 * Replaces the payload format with property values from messageContext.
 *
 * @param format//from   w w w. ja va  2 s. c  o m
 * @param resultCTX
 * @param synCtx
 */
private void replaceCTX(String format, StringBuffer resultCTX, MessageContext synCtx) {
    Matcher ctxMatcher;

    if (mediaType != null && (mediaType.equals(JSON_TYPE) || mediaType.equals(TEXT_TYPE))) {
        ctxMatcher = ctxPattern.matcher(format);
    } else {
        ctxMatcher = ctxPattern.matcher("<pfPadding>" + format + "</pfPadding>");
    }
    while (ctxMatcher.find()) {
        String ctxMatchSeq = ctxMatcher.group();
        String expressionTxt = ctxMatchSeq.substring(5, ctxMatchSeq.length());

        String replaceValue = synCtx.getProperty(expressionTxt).toString();

        if (mediaType.equals(JSON_TYPE) && inferReplacementType(replaceValue).equals(XML_TYPE)) {
            // XML to JSON conversion here
            try {
                replaceValue = "<jsonObject>" + replaceValue + "</jsonObject>";
                OMElement omXML = AXIOMUtil.stringToOM(replaceValue);
                replaceValue = JsonUtil.toJsonString(omXML).toString();
            } catch (XMLStreamException e) {
                handleException(
                        "Error parsing XML for JSON conversion, please check your property values return valid XML: ",
                        synCtx);
            } catch (AxisFault e) {
                handleException("Error converting XML to JSON", synCtx);
            }
        } else if (mediaType.equals(XML_TYPE) && inferReplacementType(replaceValue).equals(JSON_TYPE)) {
            // JSON to XML conversion here
            try {
                OMElement omXML = JsonUtil.toXml(IOUtils.toInputStream(replaceValue), false);
                if (JsonUtil.isAJsonPayloadElement(omXML)) { // remove <jsonObject/> from result.
                    Iterator children = omXML.getChildElements();
                    String childrenStr = "";
                    while (children.hasNext()) {
                        childrenStr += (children.next()).toString().trim();
                    }
                    replaceValue = childrenStr;
                } else {
                    replaceValue = omXML.toString();
                }
            } catch (AxisFault e) {
                handleException(
                        "Error converting JSON to XML, please check your property values return valid JSON: ",
                        synCtx);
            }
        }
        ctxMatcher.appendReplacement(resultCTX, replaceValue);
    }
    ctxMatcher.appendTail(resultCTX);
}

From source file:org.muse.mneme.impl.AttachmentServiceImpl.java

/**
 * {@inheritDoc}//from  w ww. java 2  s. co m
 */
public String translateEmbeddedReferences(String data, List<Translation> translations) {
    if (data == null)
        return data;
    if (translations == null)
        return data;

    // pattern to find any src= or href= text
    // groups: 0: the whole matching text 1: src|href 2: the string in the quotes
    Pattern p = Pattern.compile("(src|href)[\\s]*=[\\s]*\"([^\"]*)\"");

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    // process each "harvested" string (avoiding like strings that are not in src= or href= patterns)
    while (m.find()) {
        if (m.groupCount() == 2) {
            String ref = m.group(2);

            // harvest any content hosting reference
            int index = ref.indexOf("/access/content/");
            if (index != -1) {
                // except for any in /user/ or /public/
                if (ref.indexOf("/access/content/user/") != -1) {
                    index = -1;
                } else if (ref.indexOf("/access/content/public/") != -1) {
                    index = -1;
                }
            }

            // harvest also the mneme docs references
            if (index == -1)
                index = ref.indexOf("/access/mneme/content/");

            if (index != -1) {
                // save just the reference part (i.e. after the /access);
                String normal = ref.substring(index + 7);

                // deal with %20 and other encoded URL stuff
                try {
                    normal = URLDecoder.decode(normal, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    M_log.warn("harvestAttachmentsReferenced: " + e);
                }

                // translate the normal form
                String translated = normal;
                for (Translation translation : translations) {
                    translated = translation.translate(translated);
                }

                // URL encode translated
                String escaped = EscapeRefUrl.escapeUrl(translated);

                // if changed, replace
                if (!normal.equals(translated)) {
                    m.appendReplacement(sb, Matcher.quoteReplacement(
                            m.group(1) + "=\"" + ref.substring(0, index + 7) + escaped + "\""));
                }
            }
        }
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.intermine.bio.dataconversion.SgdConverter.java

private String getCasedName(String name) throws Exception {

    String newname = name;//  ww w . ja  v a 2s . co m

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(name);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }
    newname = m.appendTail(sb) + "p".toString();

    return newname;

}

From source file:org.codehaus.groovy.grails.web.mapping.RegexUrlMapping.java

@SuppressWarnings({ "unchecked" })
private String createURLInternal(Map paramValues, String encoding, boolean includeContextPath) {

    if (encoding == null)
        encoding = "utf-8";

    String contextPath = "";
    if (includeContextPath) {
        GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.getRequestAttributes();
        if (webRequest != null) {
            contextPath = webRequest.getAttributes().getApplicationUri(webRequest.getCurrentRequest());
        }// www . j  av a2  s.  c  o m
    }
    if (paramValues == null)
        paramValues = Collections.EMPTY_MAP;
    StringBuilder uri = new StringBuilder(contextPath);
    Set usedParams = new HashSet();

    String[] tokens = urlData.getTokens();
    int paramIndex = 0;
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        if (i == tokens.length - 1 && urlData.hasOptionalExtension()) {
            token += UrlMapping.OPTIONAL_EXTENSION_WILDCARD;
        }
        Matcher m = OPTIONAL_EXTENSION_WILDCARD_PATTERN.matcher(token);
        if (m.find()) {

            if (token.startsWith(CAPTURED_WILDCARD)) {
                ConstrainedProperty prop = constraints[paramIndex++];
                String propName = prop.getPropertyName();

                Object value = paramValues.get(propName);
                usedParams.add(propName);

                if (value != null) {
                    token = token.replaceFirst(DOUBLE_WILDCARD_PATTERN.pattern(), value.toString());
                } else if (prop.isNullable()) {
                    break;
                }
            }
            uri.append(SLASH);
            ConstrainedProperty prop = constraints[paramIndex++];
            String propName = prop.getPropertyName();
            Object value = paramValues.get(propName);
            usedParams.add(propName);
            if (value != null) {
                String ext = "." + value;
                uri.append(token.replace(OPTIONAL_EXTENSION_WILDCARD + '?', ext)
                        .replace(OPTIONAL_EXTENSION_WILDCARD, ext));
            } else {
                uri.append(token.replace(OPTIONAL_EXTENSION_WILDCARD + '?', "")
                        .replace(OPTIONAL_EXTENSION_WILDCARD, ""));
            }

            continue;
        }
        if (token.endsWith("?")) {
            token = token.substring(0, token.length() - 1);
        }
        m = DOUBLE_WILDCARD_PATTERN.matcher(token);
        if (m.find()) {
            StringBuffer buf = new StringBuffer();
            do {
                ConstrainedProperty prop = constraints[paramIndex++];
                String propName = prop.getPropertyName();
                Object value = paramValues.get(propName);
                usedParams.add(propName);
                if (value == null && !prop.isNullable()) {
                    throw new UrlMappingException("Unable to create URL for mapping [" + this
                            + "] and parameters [" + paramValues + "]. Parameter [" + prop.getPropertyName()
                            + "] is required, but was not specified!");
                } else if (value == null) {
                    m.appendReplacement(buf, "");
                } else {
                    m.appendReplacement(buf, Matcher.quoteReplacement(value.toString()));
                }
            } while (m.find());

            m.appendTail(buf);

            try {
                String v = buf.toString();
                if (v.indexOf(SLASH) > -1 && CAPTURED_DOUBLE_WILDCARD.equals(token)) {
                    // individually URL encode path segments
                    if (v.startsWith(SLASH)) {
                        // get rid of leading slash
                        v = v.substring(SLASH.length());
                    }
                    String[] segs = v.split(SLASH);
                    for (String segment : segs) {
                        uri.append(SLASH).append(encode(segment, encoding));
                    }
                } else if (v.length() > 0) {
                    // original behavior
                    uri.append(SLASH).append(encode(v, encoding));
                } else {
                    // Stop processing tokens once we hit an empty one.
                    break;
                }
            } catch (UnsupportedEncodingException e) {
                throw new ControllerExecutionException("Error creating URL for parameters [" + paramValues
                        + "], problem encoding URL part [" + buf + "]: " + e.getMessage(), e);
            }
        } else {
            uri.append(SLASH).append(token);
        }
    }
    populateParameterList(paramValues, encoding, uri, usedParams);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Created reverse URL mapping [" + uri.toString() + "] for parameters [" + paramValues + "]");
    }
    return uri.toString();
}

From source file:com.ibm.cics.ca1y.Emit.java

/**
 * Using the key and pattern to identify a token, replace all tokens for the key value in the property.
 * //w ww.  java2s  . co m
 * @param key
 *            - The string to search for tokens
 * @param pattern
 *            - The pattern to use to find tokens
 * @param props
 *            - The properties to use as the lookup table
 * @param cicsChannel
 *            - The channel to get containers from
 * @param channelDFHEP
 *            - True if the name of the current CICS channel is identified as event processing
 * @param allowReevaluateForTokens
 *            - reevaluated the key for tokens if at least one token was replaced with a value that could contain
 *            another token
 * @return true if at least one token was replaced
 */
private static boolean resolveTokensInKey(String key, Pattern pattern, EmitProperties props,
        Channel cicsChannel, boolean channelDFHEP, boolean allowReevaluateForTokens) {

    if ((key == null) || (pattern == null) || (props == null) || (props.getProperty(key) == null)) {
        return false;
    }

    if (props.getPropertyResolved(key)) {
        // Property is already resolved
        if (logger.isLoggable(Level.FINE)) {
            logger.fine(messages.getString("PropertyResolved") + " "
                    + EmitProperties.getPropertySummary(props, key));
        }

        return false;
    }

    if ((logger.isLoggable(Level.FINE))) {
        logger.fine(
                messages.getString("PropertyResolving") + " " + EmitProperties.getPropertySummary(props, key));
    }

    // Do not change the order of the command options, as later processing relies on this order
    final String[] nomoretokensCommand = { "nomoretokens" };
    final String[] noinnertokensCommand = { "noinnertokens" };
    final String[] datetimeCommand = { "datetime=" };
    final String[] mimeCommand = { "mime=", ":to=", ":xslt=" };
    final String[] nameCommand = { "name=" };
    final String[] responsecontainerCommand = { "responsecontainer=" };
    final String[] fileCommand = { "file=", ":encoding=", ":binary", ":options=" };
    final String[] zipCommand = { "zip=", ":include=" };
    final String[] doctemplateCommand = { "doctemplate=", ":addPropertiesAsSymbols", ":binary" };
    final String[] transformCommand = { "transform=", ":xmltransform=", ":jsontransform=", ":jvmserver=" };
    final String[] ftpCommand = { "ftp=", ":server=", ":username=", ":userpassword=", ":transfer=", ":mode=",
            ":epsv=", ":protocol=", ":trustmgr=", ":datatimeout=", ":proxyserver=", ":proxyusername=",
            ":proxypassword=" };
    final String[] hexCommand = { "hex=" };
    final String[] systemsymbolCommand = { "systemsymbol=" };
    final String[] commonBaseEventRESTCommand = { "commonbaseeventrest" };
    final String[] commonBaseEventCommandCommand = { "commonbaseevent" };
    final String[] cicsFlattenedEventCommand = { "cicsflattenedevent" };
    final String[] jsonCommand = { "json", ":properties=" };
    final String[] emitCommand = { "emit" };
    final String[] linkCommand = { "link=" };
    final String[] putcontainerCommand = { "putcontainer=" };
    final String[] htmltableCommand = { "htmltable", ":properties=", ":containers=", ":summary" };
    final String[] texttableCommand = { "texttable", ":properties=", ":containers=", ":summary" };
    final String[] trimCommand = { "trim" };

    final int maxTimesToResolveTokens = 10;

    boolean tokenReplaced = false;
    boolean allowPropertyToBeReevaluatedForTokens = true;
    String putContainer = null;

    // Indicate the property has been resolved at this point to prevent recursion resolving tokens within the
    // property
    props.setPropertyResolved(key, true);

    // As a token's replacement may itself contain tokens we need to iterate token searching, up to a maximum number
    // of times
    for (int i = 0; (i < maxTimesToResolveTokens) && (allowPropertyToBeReevaluatedForTokens); i++) {
        boolean reevaluateForTokens = false;
        Matcher matcher = pattern.matcher(props.getProperty(key));
        StringBuffer buffer = new StringBuffer();

        while (matcher.find()) {
            String token = matcher.group(1).trim();

            if (logger.isLoggable(Level.FINE)) {
                logger.fine(Emit.messages.getString("ResolvingToken") + " " + token);
            }

            if (token.startsWith(nomoretokensCommand[0])) {
                // Do not process any more tokens in the key
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                reevaluateForTokens = false;
                break;

            } else if (token.startsWith(noinnertokensCommand[0])) {
                // Processing all tokens in this property but prevent re-evaluation tokens in resolve tokens
                // eg. token is "noinntertokens"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                allowPropertyToBeReevaluatedForTokens = false;

            } else if (token.startsWith(transformCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, transformCommand);

                if (isCICS == false) {
                    // if CICS API not available, ignore this token

                } else if (options[1] != null) {
                    // eg. {transform=property:xmltranform=resource}
                    try {
                        Container inputContainer = cicsChannel.createContainer("CA1Y-TRANSFORM-I");
                        Container outputContainer = cicsChannel.createContainer("CA1Y-TRANSFORM-O");

                        if (props.getPropertyAttachment(options[0]) == null) {
                            // property is text
                            inputContainer.putString(props.getProperty(options[0]));

                        } else {
                            // property is binary
                            inputContainer.put(props.getPropertyAttachment(options[0]));
                        }

                        XmlTransform xmltransform = new XmlTransform(options[1]);
                        TransformInput transforminput = new TransformInput();
                        transforminput.setChannel(cicsChannel);
                        transforminput.setDataContainer(inputContainer);
                        transforminput.setXmlContainer(outputContainer);
                        transforminput.setXmltransform(xmltransform);

                        Transform.dataToXML(transforminput);

                        buffer.append(new String(outputContainer.get(Emit.defaultCharset)));

                        inputContainer.delete();
                        outputContainer.delete();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else if (options[2] != null) {
                    // eg. {transform=propertyName:jsontransform=jsontransfrmResource:jvmserver=jvmResource}
                    try {
                        Container inputContainer = cicsChannel.createContainer("DFHJSON-DATA");
                        inputContainer.put(props.getPropertyAttachment(options[0]));

                        inputContainer = cicsChannel.createContainer("DFHJSON-TRANSFRM");
                        inputContainer.putString(options[2]);

                        if (options[3] != null) {
                            inputContainer = cicsChannel.createContainer("DFHJSON-JVMSERVR");
                            inputContainer.putString(options[3]);
                        }

                        Program p = new Program();
                        p.setName("DFHJSON");
                        p.link(cicsChannel);

                        Container outputContainer = (cicsChannel).createContainer("DFHSON-JSON");
                        buffer.append(outputContainer.get("UTF-8"));

                        inputContainer.delete();
                        outputContainer.delete();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(mimeCommand[0])) {
                // Store the MIME type in the property
                // eg. token is "mime=application/octet-stream"
                // eg. token is "mime=application/xml"
                // eg. token is "mime=text/xsl:to=application/pdf"
                // eg. token is "mime=text/xml:to=application/pdf:xslt=xsltProperty"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, mimeCommand);

                props.setPropertyMime(key, options[0]);

                if (options[1] != null) {
                    props.setPropertyMimeTo(key, options[1]);
                }

                if (options[2] != null) {
                    props.setPropertyXSLT(key, options[2]);
                }

            } else if (token.startsWith(nameCommand[0])) {
                // Store the name in the property
                // eg. token is "name=My Picture"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, nameCommand);
                props.setPropertyAlternateName(key, options[0]);

            } else if (token.startsWith(datetimeCommand[0])) {
                // For tokens that start with DATETIME_PREFIX, replace the token with the resolved date and time
                // format
                // eg. token is "datetime=yyyy.MM.dd G 'at' HH:mm:ss z"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, datetimeCommand);

                Date date = null;
                if (props.containsKey("EPCX_ABSTIME")) {
                    date = Util.getDateFromAbstime(props.getProperty("EPCX_ABSTIME"));

                } else {
                    date = new Date();
                }

                if (options[0].isEmpty()) {
                    buffer.append(date.toString());

                } else {
                    buffer.append((new SimpleDateFormat(options[0])).format(date));
                }

            } else if (token.startsWith(zipCommand[0])) {
                // Store the zip in the property
                // eg. token is "zip="
                // eg. token is "zip=MyZip.zip"
                // eg. token is "zip=MyZip.zip:include=property1"
                // eg. token is "zip=MyZip.zip:include=property1,property2"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, zipCommand);

                if (options[0].isEmpty()) {
                    props.setPropertyZip(key, key + ".zip");

                } else {
                    props.setPropertyZip(key, options[0]);
                }

                if (options[1] != null) {
                    props.setPropertyZipInclude(key, options[1]);

                } else {
                    props.setPropertyZipInclude(key, key);
                }

            } else if (token.startsWith(fileCommand[0])) {
                // Replace the token with the content of the file
                // eg. token is "file=//dataset"
                // eg. token is "file=//dataset:encoding=Cp1047"
                // eg. token is "file=//dataset:binary"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, fileCommand);

                if (options[0].startsWith("//")) {
                    ZFile zf = null;

                    if (options[2] != null) {

                        try {
                            // Read dataset in binary using IBM JZOS library
                            zf = new ZFile(options[0], (options[3] == null) ? "rb" : options[3]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, zf.getActualFilename());
                            }

                            if (props.getPropertyMime(key) == null) {
                                props.setPropertyMimeDefault(key, zf.getActualFilename());
                            }

                            props.setPropertyAttachment(key, IOUtils.toByteArray(zf.getInputStream()));

                        } catch (Exception e) {
                            e.printStackTrace();

                        } finally {
                            if (zf != null) {
                                try {
                                    zf.close();
                                } catch (Exception e2) {
                                }
                            }
                        }

                    } else {
                        try {
                            // Read dataset in text using IBM JZOS library
                            zf = new ZFile(options[0], (options[3] == null) ? "r" : options[3]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, zf.getActualFilename());
                            }

                            buffer.append(IOUtils.toString(zf.getInputStream(),
                                    (options[1] == null) ? ZUtil.getDefaultPlatformEncoding() : options[1]));
                            reevaluateForTokens = true;

                        } catch (Exception e) {
                            e.printStackTrace();

                        } finally {
                            if (zf != null) {
                                try {
                                    zf.close();
                                } catch (Exception e2) {
                                }
                            }
                        }
                    }

                } else {
                    File f = null;

                    if (options[2] != null) {
                        try {
                            f = new File(options[0]);
                            // Read zFS file as binary

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, f.getName());
                            }

                            if (props.getPropertyMime(key) == null) {
                                props.setPropertyMimeDefault(key, f.getName());
                            }

                            props.setPropertyAttachment(key, FileUtils.readFileToByteArray(f));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        try {
                            // Read zFS file as text
                            f = new File(options[0]);

                            if (props.getPropertyAlternateName(key) == null) {
                                props.setPropertyAlternateName(key, f.getName());
                            }

                            buffer.append(FileUtils.readFileToString(f, options[1]));
                            reevaluateForTokens = true;

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

            } else if (token.startsWith(doctemplateCommand[0])) {
                // Replace the token with the contents of the CICS document template resource
                // eg. token is "doctemplate=MyTemplate"
                // eg. token is "doctemplate=MyTemplate:addPropertiesAsSymbols"
                // eg. token is "doctemplate=MyTemplate:binary"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, doctemplateCommand);

                if (options[2] == null) {
                    try {
                        // Read doctemplate as text
                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, options[0]);
                        }

                        Document document = new Document();

                        if (options[1] != null) {
                            // Add all properties except the current property as symbols to document
                            Enumeration<?> e = props.propertyNamesOrdered();

                            while (e.hasMoreElements()) {
                                String key2 = (String) e.nextElement();

                                // Only add property if it is not the current key and is not private
                                if ((key.equals(key2) == false) && (props.getPropertyPrivacy(key2) == false)) {
                                    try {
                                        if ((props.getPropertyResolved(key2) == false)
                                                && (allowPropertyToBeReevaluatedForTokens)) {
                                            // resolve the token before attempting to use it
                                            resolveTokensInKey(key2, pattern, props, cicsChannel, channelDFHEP,
                                                    allowReevaluateForTokens);
                                        }

                                        document.addSymbol(key2, props.getProperty(key2));

                                    } catch (Exception e2) {
                                        // Continue even if there are errors adding a symbol
                                    }
                                }
                            }
                        }

                        document.createTemplate(options[0]);
                        buffer.append(new String(document.retrieve("UTF-8", true), "UTF-8"));
                        reevaluateForTokens = true;

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    try {
                        // Read doctemplate as binary
                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, options[0]);
                        }

                        if (props.getPropertyMime(key) == null) {
                            props.setPropertyMimeDefault(key, options[0]);
                        }

                        Document document = new Document();
                        document.createTemplate(options[0]);
                        props.setPropertyAttachment(key, document.retrieve());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(commonBaseEventRESTCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCommonBaseEventREST(props));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_XML);
                }

            } else if (token.startsWith(commonBaseEventCommandCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCommonBaseEvent(props));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_XML);
                }

            } else if (token.startsWith(cicsFlattenedEventCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(getCicsFlattenedEvent(props));
                reevaluateForTokens = true;

            } else if (token.startsWith(jsonCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, jsonCommand);
                buffer.append(getJson(props, options[1]));
                reevaluateForTokens = true;

                if (props.getPropertyMime(key) == null) {
                    props.setPropertyMime(key, MIME_JSON);
                }

            } else if (token.startsWith(htmltableCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, htmltableCommand);
                buffer.append(getHtmlTable(props, options[1], options[2], options[3], isCICS));

            } else if (token.startsWith(texttableCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, texttableCommand);
                buffer.append(getTextTable(props, options[1], options[2], options[3]));

            } else if (token.startsWith(ftpCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, ftpCommand);
                String defaultanonymouspassword = null;

                if (isCICS) {
                    defaultanonymouspassword = channelDFHEP ? props.getProperty("EPCX_USERID")
                            : props.getProperty("TASK_USERID");

                    if (defaultanonymouspassword != null) {
                        try {
                            defaultanonymouspassword = (new StringBuilder(
                                    String.valueOf(defaultanonymouspassword))).append("@")
                                            .append(InetAddress.getLocalHost().getHostName()).toString();
                        } catch (Exception _ex) {
                        }
                    }
                }

                byte b[] = getFileUsingFTP(options[0], options[1], options[2], options[3], options[4],
                        options[5], options[6], options[7], options[8], options[9], options[10], options[11],
                        options[12], defaultanonymouspassword);

                if (b != null) {
                    if ("binary".equalsIgnoreCase(options[4])) {
                        String filename = FilenameUtils.getName(options[0]);

                        if (props.getPropertyAlternateName(key) == null) {
                            props.setPropertyAlternateName(key, filename);
                        }

                        if (props.getPropertyMime(key) == null) {
                            props.setPropertyMimeDefault(key, filename);
                        }

                        props.setPropertyAttachment(key, b);

                    } else {
                        try {
                            buffer.append(new String(b, "US-ASCII"));
                            reevaluateForTokens = true;

                        } catch (Exception _ex) {
                            // Ignore
                        }
                    }
                }

            } else if (token.startsWith(responsecontainerCommand[0])) {
                // Store the name in the property
                // eg. token is "responsecontainer="
                // eg. token is "responsecontainer=MyContainer"
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String[] options = Util.getOptions(token, responsecontainerCommand);
                props.setPropertyReturnContainer(key, (options[0].isEmpty()) ? key : options[0]);

            } else if (token.startsWith(hexCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, hexCommand);

                if (props.getProperty(options[0]) != null) {
                    buffer.append(Util.getHex(props.getProperty(options[0]).getBytes()));

                } else {
                    if (props.getPropertyAttachment(options[0]) != null)
                        buffer.append(Util.getHex(props.getPropertyAttachment(options[0])));
                }

            } else if (token.startsWith(emitCommand[0])) {
                // Set this property to be emitted
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                props.putBusinessInformationItem(key, 0);

            } else if (token.startsWith(systemsymbolCommand[0])) {
                // Get the z/OS system symbol
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, systemsymbolCommand);

                try {
                    buffer.append(Util.getSystemSymbol(options[0]));
                } catch (Exception e) {
                }

            } else if (token.startsWith(linkCommand[0])) {
                // Link to CICS program
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, linkCommand);

                if (isCICS) {
                    // Only execute if CICS API is available

                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine(messages.getString("LinkTo") + " " + options[0]);
                    }

                    Program p = new Program();
                    p.setName(options[0]);

                    try {
                        p.link(cicsChannel);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } else if (token.startsWith(putcontainerCommand[0])) {
                // Put the value of this property into a CICS container once the tokens have been resolve
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                String options[] = Util.getOptions(token, putcontainerCommand);

                putContainer = options[0];

            } else if (token.startsWith(trimCommand[0])) {
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                // Remove leading and training spaces from the buffer up to the point of the token
                buffer = new StringBuffer(buffer.toString().trim());

            } else if (props.containsKey(token)) {
                // The token refers to a property
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                // Do not copy property if it is the current key or it is marked as private
                if ((key.equals(token) == false) && (props.getPropertyPrivacy(key) == false)) {

                    if ((props.getPropertyResolved(token) == false)
                            && (allowPropertyToBeReevaluatedForTokens)) {
                        // Resolve the tokens in the property before attempting to use it
                        resolveTokensInKey(token, pattern, props, cicsChannel, channelDFHEP,
                                allowReevaluateForTokens);
                    }

                    if (props.getPropertyAttachment(token) == null) {
                        // Copy the tokens' property
                        buffer.append(props.getProperty(token));

                    } else {
                        // Copy the tokens' property attachment

                        if (props.getPropertyAttachment(key) == null) {
                            // current property does not have an attachment
                            props.setPropertyAttachment(key, Arrays.copyOf(props.getPropertyAttachment(token),
                                    props.getPropertyAttachment(token).length));

                        } else {
                            // Current property has an attachment, so append the tokens' attachment
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                            try {
                                outputStream.write(props.getPropertyAttachment(key));
                                outputStream.write(props.getPropertyAttachment(token));

                            } catch (Exception e) {
                            }

                            props.setPropertyAttachment(key, outputStream.toByteArray());
                        }
                    }

                    props.setPropertyAlternateName(key, props.getPropertyAlternateName(token));
                }

            } else if (System.getProperty(token) != null) {
                // The token refers to a system property
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");
                buffer.append(System.getProperty(token));

            } else if (isCICS && (token.getBytes().length >= CONTAINER_NAME_LENGTH_MIN)
                    && (token.getBytes().length <= CONTAINER_NAME_LENGTH_MAX)) {
                // If this is not an EP event and the token is a max of 16 characters, attempt to replace token with
                // contents of the CICS container named by the token in current channel
                tokenReplaced = true;
                matcher.appendReplacement(buffer, "");

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("GetContainer") + " " + token);
                }

                try {
                    Container container = (cicsChannel).createContainer(token);

                    try {
                        // Assume container is of type CHAR and get CICS to convert it
                        buffer.append(container.getString());
                        reevaluateForTokens = true;

                    } catch (CodePageErrorException e) {
                        // As container could not be converted, assume it is of type BIT and copy the contents into
                        // an attachment

                        if (props.getPropertyAttachment(key) == null) {
                            props.setPropertyAttachment(key, container.get());

                        } else {
                            // Append the property named by token to the existing attachment
                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                            try {
                                outputStream.write(props.getPropertyAttachment(key));
                                outputStream.write(container.get());

                            } catch (Exception e2) {
                            }

                            props.setPropertyAttachment(key, outputStream.toByteArray());
                        }
                    }

                    if (props.getPropertyAlternateName(key) == null) {
                        props.setPropertyAlternateName(key, token);
                    }

                } catch (Exception e) {
                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine(messages.getString("GetContainerError") + " " + token);
                    }
                }

            } else {
                // Token could not be resolved, so just remove it
                matcher.appendReplacement(buffer, "");

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("UnResolvedToken") + " " + token);
                }
            }
        }

        matcher.appendTail(buffer);
        props.setProperty(key, buffer.toString());

        if ((reevaluateForTokens == false) || (allowReevaluateForTokens == false)) {
            break;
        }
    }

    if ((isCICS) && (putContainer != null) && (putContainer.length() >= CONTAINER_NAME_LENGTH_MIN)
            && (putContainer.length() <= CONTAINER_NAME_LENGTH_MAX)) {
        try {
            Container container = cicsChannel.createContainer(putContainer);

            if (props.getPropertyAttachment(key) == null) {
                // Create a container of type CHAR.
                // Should use container.putString but this was only added in
                // CICS TS V5.1
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("PutContainerChar") + " " + putContainer);
                }

                container.putString(props.getProperty(key));

            } else {
                // Creates a container of type BIT
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine(messages.getString("PutContainerBit") + " " + putContainer);
                }

                container.put(props.getPropertyAttachment(key));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    if (logger.isLoggable(Level.FINE) && (tokenReplaced)) {
        logger.fine(
                messages.getString("PropertyReplaced") + " " + EmitProperties.getPropertySummary(props, key));
    }

    return tokenReplaced;
}