'use strict';
module.exports = {
  // TODO get ontology terms and other data

  toJson: function(str, pathwayMetadata, callback) {
    var thisJquery;
    var xmlSelection;
    // if we were just using Cheerio, we could select namespace elements like this:
    // xmlSelection('bp\\:PublicationXref')
    // but in my tests with this in Chrome, it appears jQuery doesn't work with that selector
    // so to make this work in both Node.js and the browser, I'm removing the namespace prefixes from element tagNames
    // namespace prefixes appear to work OK as attribute names.
    var plainElementTagsStr = str.replace(/bp\:/g, '').replace(/rdf\:id/g, 'rdf:ID');
    if (typeof window === 'undefined') {
      var Cheerio = require('cheerio');
      thisJquery = Cheerio.load(plainElementTagsStr, {
        normalizeWhitespace: true,
        xmlMode: true,
        decodeEntities: true,
        lowerCaseTags: false
      });
      xmlSelection = thisJquery.root();
    } else {
      thisJquery = $;
      var xmlDoc = $.parseXML(plainElementTagsStr);
      xmlSelection = $(xmlDoc);
    }

    var jsonBiopax = pathwayMetadata;
    jsonBiopax.entities = [];

    var displayName = 1;
    xmlSelection.find('PublicationXref').each(function() {
      var xmlPublicationXrefSelection = thisJquery( this );
      var publicationXref = {};
      publicationXref.type = 'PublicationXref';
      publicationXref.id = xmlPublicationXrefSelection.attr('rdf:ID') || xmlPublicationXrefSelection.attr('rdf:about');
      publicationXref.dbName = xmlPublicationXrefSelection.find('db').text().toLowerCase();
      publicationXref.dbId = xmlPublicationXrefSelection.find('id').text();
      if (!!publicationXref && !!publicationXref.id && publicationXref.id.indexOf('identifiers') === -1 && (publicationXref.dbName === 'pubmed' || publicationXref.dbName === 'medline') && /^\d+$/g.test(publicationXref.dbId)) {
        publicationXref.deprecatedId = publicationXref.id;
        publicationXref.id = 'http://identifiers.org/pubmed/' + publicationXref.dbId;
      }
      publicationXref.displayName = displayName;
      displayName += 1;
      jsonBiopax.entities.push(publicationXref);
    });
    callback(null, jsonBiopax);
  }
};

@@ -1509,161 +1509,1205 @@
     
-publicationXref.dbName = xmlPublicationXrefSelection.find('db').text().toLowerCase
+var dbNameElements = xmlPublicationXrefSelection.find('db');%0A      if (!!dbNameElements && dbNameElements.length %3E 1) %7B%0A        dbNameElements = thisJquery(dbNameElements%5B0%5D);%0A      %7D%0A      publicationXref.dbName = dbNameElements.text().toLowerCase();%0A%0A      var dbIdElements = xmlPublicationXrefSelection.find('id');%0A      if (!!dbIdElements && dbIdElements.length %3E 1) %7B%0A        dbIdElements = thisJquery(dbIdElements%5B0%5D);%0A      %7D%0A      publicationXref.dbId = dbIdElements.text();%0A%0A      var titleElements = xmlPublicationXrefSelection.find('title');%0A      if (!!titleElements && titleElements.length %3E 1) %7B%0A        titleElements = thisJquery(titleElements%5B0%5D);%0A      %7D%0A      publicationXref.title = titleElements.text
 ();%0A
+%0A
       
-publicationXref.dbId = xmlPublicationXrefSelection.find('id')
+var sourceElements = xmlPublicationXrefSelection.find('source');%0A      if (!!sourceElements && sourceElements.length %3E 1) %7B%0A        sourceElements = thisJquery(sourceElements%5B0%5D);%0A      %7D%0A      publicationXref.source = sourceElements.text();%0A%0A      var yearElements = xmlPublicationXrefSelection.find('year');%0A      if (!!yearElements && yearElements.length %3E 1) %7B%0A        yearElements = thisJquery(yearElements%5B0%5D);%0A      %7D%0A      publicationXref.year = yearElements
 .tex
@@ -2707,24 +2707,25 @@
 nts.text();%0A
+%0A
       if (!!

address case where biopax source data is invalid because it has more than one bp:db and/or bp:id. Also, add title, source, year and author