Package fitnesse.responders

Source Code of fitnesse.responders.WikiImportingTraverser

package fitnesse.responders;

import java.io.IOException;

import fitnesse.components.TraversalListener;
import fitnesse.components.Traverser;
import fitnesse.wiki.PageData;
import fitnesse.wiki.WikiImportProperty;
import fitnesse.wiki.WikiPage;
import fitnesse.wiki.WikiPagePath;

public class WikiImportingTraverser implements WikiImporterClient, Traverser<Object> {
  private final WikiImporter importer;
  private final WikiPage page;
  private final PageData data;
  private final boolean isUpdate;
  private final boolean isNonRoot;
  private final String remoteWikiUrl;
  private final WikiPagePath pagePath;
  private TraversalListener<Object> traversalListener;

  public WikiImportingTraverser(WikiImporter wikiImporter, WikiPage page, String remoteWikiUrl) throws IOException {
    this.importer = wikiImporter;
    this.page = page;
    this.data = page.getData();
    this.pagePath = page.getPageCrawler().getFullPath();
    WikiImportProperty importProperty = WikiImportProperty.createFrom(data.getProperties());
    if (importProperty != null) {
      this.remoteWikiUrl = importProperty.getSourceUrl();
      this.isUpdate = true;
      this.isNonRoot = !importProperty.isRoot();
    } else {
      this.remoteWikiUrl = remoteWikiUrl;
      this.isUpdate = false;
      this.isNonRoot = false;
    }
    initializeImporter();
  }

  public WikiImportingTraverser(WikiImporter wikiImporter, WikiPage page) throws IOException {
    this(wikiImporter, page, null);
  }

  private void initializeImporter() throws IOException {
    importer.setWikiImporterClient(this);
    importer.setLocalPath(pagePath);
    importer.parseUrl(remoteWikiUrl);
  }

  public boolean isUpdate() {
    return isUpdate;
  }

  @Override
  public void traverse(TraversalListener<Object> traversalListener) {
    this.traversalListener = traversalListener;
    try {
      if (isNonRoot) {
        importer.importRemotePageContent(page);
      }

      importer.importWiki(page);

      if (!isUpdate) {
        WikiImportProperty importProperty = new WikiImportProperty(importer.remoteUrl());
        importProperty.setRoot(true);
        importProperty.setAutoUpdate(importer.getAutoUpdateSetting());
        importProperty.addTo(data.getProperties());
        page.commit(data);
      }
    } catch (WikiImporter.WikiImporterException e) {
      traversalListener.process(new ImportError("ERROR", "The remote resource, " + importer.remoteUrl() + ", was not found."));
    } catch (WikiImporter.AuthenticationRequiredException e) {
      traversalListener.process(new ImportError("AUTH", e.getMessage()));
    } catch (Exception e) {
      traversalListener.process(new ImportError("ERROR", e.getMessage(), e));
    }

  }

  @Override
  public void pageImported(WikiPage localPage) {
    traversalListener.process(localPage);
  }

  @Override
  public void pageImportError(WikiPage localPage, Exception e) {
    traversalListener.process(new ImportError("PAGEERROR", e.getMessage(), e));
  }

  public static class ImportError {
    private String message;
    private String type;
    private Exception exception;

    public ImportError(String type, String message) {
      this(type, message, null);
    }

    public ImportError(String type, String message, Exception exception) {
      super();
      this.type = type;
      this.message = message;
      this.exception = exception;
    }

    public String getType() {
      return type;
    }

    public String getMessage() {
      return message;
    }

    public Exception getException() {
      return exception;
    }

    public String toString() {
      return getClass().getName() + ": " + getMessage();
    }
  }
}
TOP

Related Classes of fitnesse.responders.WikiImportingTraverser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.