WikiPageXW.java :  » Wiki-Engine » wikipipes » de » fzi » wikipipes » impl » xwiki » Java Open Source

Java Open Source » Wiki Engine » wikipipes 
wikipipes » de » fzi » wikipipes » impl » xwiki » WikiPageXW.java
package de.fzi.wikipipes.impl.xwiki;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Locale;

import javax.xml.transform.TransformerException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xerces.parsers.DOMParser;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.DOMReader;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import de.fzi.wikipipes.IWikiPage;
import de.fzi.wikipipes.impl.AbstractWebWikiPage;
import de.fzi.wikipipes.impl.Util;

public class WikiPageXW extends AbstractWebWikiPage {

  private HttpClient httpclient;

  private static final Log log = LogFactory.getLog(WikiPageXW.class);

  public WikiPageXW(WikiRepositoryXW repository, String name) {
    super(repository, name);
    this.httpclient = repository.getHttpClient();
  }

  @Override
  protected String getURL() {
    return this.repository.getServerURL() + "/" + getName();
  }

  public boolean delete() {
    throw new RuntimeException("not implemented");
  }

  public String getAuthor() {
    InputStream in = Util.getInputStreamFromUrl(this.httpclient,
        this.repository.getPageURL(this.name));
    org.dom4j.Document pageDoc = Util.getInputStreamAsDocument(in);
    Node author = pageDoc
        .selectSingleNode("//*[name()='DIV' and ./@class=\"about\"]"
            + "/descendant::*[name()='SPAN' and ./@class=\"wikilink\"]/*[name()='A']");
    return author.getText();
  }

  public long getChangeDate() {
    InputStream in = Util.getInputStreamFromUrl(this.httpclient,
        this.repository.getPageURL(this.name));
    org.dom4j.Document pageDoc = Util.getInputStreamAsDocument(in);
    Node time = pageDoc
        .selectSingleNode("//*[name()='DIV' and ./@class=\"about\"]"
            + "/descendant::*[name()='SPAN' and ./@class=\"wikilink\"]/following::text()");
    SimpleDateFormat dateformat = new SimpleDateFormat(
        "'on' dd/MM/yyyy 'at' HH:mm", Locale.US);
    try {
      return dateformat.parse(time.getText().trim()).getTime();
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }

  public Reader getContentAsWif() {
    Reader htmlSyntax = getRawHtmlContent();

    try {
      String wif = Util.transformToStringAndBufferXslt(htmlSyntax,
          "xwiki2wif.xslt");
      return new StringReader(wif);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(e);
    } catch (TransformerException e) {
      throw new RuntimeException(e);
    }
  }

  public Collection<IWikiPage> getSubPages() {
    throw new RuntimeException("not implemented");
  }

  public void setSubPages(Collection<IWikiPage> subpages) {
    throw new RuntimeException("not implemented");
  }

  public String getNativeWikitext() {
    GetMethod get = new GetMethod(getEditURL());
    try {
      this.httpclient.executeMethod(get);
      if (get.getStatusCode() == 200) {
        // ok
        DOMParser parser = new DOMParser();
        InputSource in = new InputSource(get.getResponseBodyAsStream());

        String s = Util.getAsString(new BufferedReader(
            new InputStreamReader(get.getResponseBodyAsStream())));
        in = new InputSource(Util.tagsoup(Util.getStringAsReader(s)));
        try {
          parser.parse(in);
          Document w3cDocument = parser.getDocument();

          /** converts a W3C DOM document into a dom4j document */
          DOMReader xmlReader = new DOMReader();
          org.dom4j.Document dom4jDocument = xmlReader
              .read(w3cDocument);
          return dom4jDocument.selectSingleNode(
              "//*[name()='textarea']").getText();
        } catch (SAXException e) {
          throw new RuntimeException(e);
        }
      } else {
        log.warn("status is " + get.getStatusLine());
        return null;
      }
    } catch (HttpException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      get.releaseConnection();
    }
  }

  public void setNativeWikitext(String nativeWikiSource) {
    GetMethod get = new GetMethod(getEditURL());
    org.dom4j.Document indexDoc = null;
    try {
      this.httpclient.executeMethod(get);
      indexDoc = Util.getInputStreamAsDocument(get
          .getResponseBodyAsStream());
    } catch (HttpException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    String postURL = getEditURL().replaceFirst("/edit/", "/save/");
    PostMethod post = new PostMethod(postURL);
    post.addParameter("template", "");
    Element xcontinue = (Element) indexDoc
        .selectSingleNode("//*[local-name()='INPUT' and @name='xcontinue']");
    assert xcontinue != null;
    post.addParameter("xcontinue", xcontinue.attributeValue("value"));
    post.addParameter("xredirect", "");
    post.addParameter("language", "en");
    post.addParameter("content", nativeWikiSource);
    post.addParameter("comment", "");
    post.addParameter("parent", "");
    post.addParameter("tags", "");

    try {
      this.httpclient.executeMethod(post);
    } catch (HttpException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      post.releaseConnection();
    }
  }

  private String getEditURL() {
    String editURL = ((WikiRepositoryXW) this.repository).getRootURL()
        + "/edit/" + getName();
    return editURL;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.