view software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/externalObjects/app/ExtElement.java @ 12:fba5577e49d9

diverse Fehlerbehebungen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 19 Apr 2011 16:51:26 +0200
parents 1ec29fdd0db8
children 5df60f24e997
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.externalObjects.app;

import java.util.Date;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
import de.mpg.mpiwg.berlin.mpdl.util.XmlUtil;

public class ExtElement extends ExtObject {
  private String pageNumber;
  private String xpath;  // path to element
  private String point;  // ".0", ".1" or a positive integer

  public ExtElement() {
    this.type = "element";  
  }
  
  public static ExtElement parseXmlStr(String xmlStr) throws ApplicationException {
    XmlUtil xmlUtil = XmlUtil.getInstance();
    xmlUtil.setNsContext("general");
    String uid = xmlUtil.evaluateToString(xmlStr, "/object/@uid", null);
    String dateStr = xmlUtil.evaluateToString(xmlStr, "/object/@modificationDate", null);
    String docId = xmlUtil.evaluateToString(xmlStr, "/object/@documentId", null);
    String xpointer = xmlUtil.evaluateToString(xmlStr, "/object/@xpointer", null);
    String pageNumber = null;
    String xpath = null;
    String point = null; 
    if (xpointer != null) {
      pageNumber = xpointer.replaceAll("#xpointer\\(id\\('page(.+)?'\\).*", "$1");
      if (xpointer.contains("point(")) {
        xpath = xpointer.replaceAll("#xpointer\\(id\\('page.+?'\\)(.*)?/point\\(.+?\\)\\)", "$1");
        point = xpointer.replaceAll("#xpointer\\(id\\('page.+?'\\).*?/point\\((.+)?\\)\\)", "$1");
      } else {
        xpath = xpointer.replaceAll("#xpointer\\(id\\('page.+?'\\)(.*)?.*?\\)", "$1");
      }
    }
    String content = xmlUtil.evaluateToXmlString(xmlStr, "/object/content/*", null);
    Date modDate = xmlUtil.toDate(dateStr);
    ExtElement e = new ExtElement();
    e.setUid(uid);
    e.setModificationDate(modDate);
    e.setDocumentId(docId);
    e.setPageNumber(pageNumber);
    e.setXpath(xpath);
    e.setPoint(point);
    e.setContent(content);
    return e;
  }

  public String toString() {
    return getXmlString();
  }
  
  public String getXmlString() {
    String xmlString = "<object xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
    xmlString = xmlString + " type=\"" + type + "\"";
    if (modificationDate != null) {
      XmlUtil xmlUtil = XmlUtil.getInstance();
      String dateStr = xmlUtil.toXsDate(modificationDate);
      xmlString = xmlString + " modificationDate=\"" + dateStr + "\"";
    }
    if (uid != null)
      xmlString = xmlString + " uid=\"" + uid + "\"";
    if (documentId != null)
      xmlString = xmlString + " documentId=\"" + documentId + "\"";
    if (xpath != null)
      xmlString = xmlString + " xmlNodeId=\"" + xpath + "\"";
    if (pageNumber != null)
      xmlString = xmlString + " xpointer=\"#xpointer(id('page" + pageNumber + "')";
    if (xpath != null)
      xmlString = xmlString + xpath;
    if (point != null)
      xmlString = xmlString + "/point(" + point + ")";
    xmlString = xmlString + ")\">";
    if (content != null) {
      // TODO wieder ausbauen
      // write the uid and modificationDate into the content node
      if (! content.contains("uid")) {
        int firstClose = content.indexOf(">");
        if (firstClose != -1)
          content = content.substring(0, firstClose) + " uid=\"" + uid + "\" modificationDate=\"" + modificationDate + "\" " + content.substring(firstClose);
      }
      xmlString = xmlString + "<content>" + content + "</content>";
    }
    xmlString = xmlString + "</object>";
    return xmlString;
  }
  
  public String getXpath() {
    return xpath;
  }

  public void setXpath(String xpath) {
    this.xpath = xpath;
  }

  public String getPoint() {
    return point;
  }

  public void setPoint(String point) {
    this.point = point;
  }

  public String getPageNumber() {
    return pageNumber;
  }

  public void setPageNumber(String pageNumber) {
    this.pageNumber = pageNumber;
  }

}