Package net.sourceforge.javautil.common.io

Source Code of net.sourceforge.javautil.common.io.VirtualArtifactAbstract

package net.sourceforge.javautil.common.io;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.impl.SimplePath;
import net.sourceforge.javautil.common.logging.ILogger;
import net.sourceforge.javautil.common.logging.LoggingContext;

/**
* A base that can be used for most implementations.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: VirtualArtifactAbstract.java 2731 2011-02-03 05:04:14Z ponderator $
*/
public abstract class VirtualArtifactAbstract<A extends IVirtualArtifact> implements IVirtualArtifact<A> {
 
  protected static final ILogger log = LoggingContext.getLogger(IVirtualArtifact.class);
 
  protected IVirtualPath path;
 
  public void clearCache () {
    this.path = null;
  }
 
  public IVirtualPath getPath() {
    if (path == null) {
      List<String> parts = new ArrayList<String>();
     
      if (!this.isRootPathPart(getName())) parts.add(getName());
     
      IVirtualArtifact artifact = this;
      while (artifact.getOwner() != null) {
        String name = (artifact = artifact.getOwner()).getName();
        if (!this.isRootPathPart(name)) parts.add(0, name);
      }
     
      this.path = new SimplePath(parts.toArray(new String[parts.size()]));
    }
   
    return path;
  }
 
  public IVirtualArtifact getRootOwner() {
    IVirtualArtifact artifact = this;
    while (artifact.getOwner() != null) {
      artifact = artifact.getOwner();
    }
    return artifact;
  }

  /**
   * @param part The part to validate
   * @return True if the path is null, equals '/' or trimmed equals nothing, an empty string, otherwise false
   */
  protected boolean isRootPathPart (String part) {
    if (part == null) return true;
    if ("/".equals(part)) return true;
    return "".equals(part.trim());
  }
 
  public boolean equals (Object o) {
    if (o == null) return false;
    if (!(o instanceof IVirtualArtifact)) return false;
   
    return this.compareTo( (IVirtualArtifact) o ) == 0;
  }

  public int compareTo(IVirtualArtifact o) {
    if (o == null) return 1;
   
    IVirtualPath tpath = getPath();
    IVirtualPath opath = o.getPath();
   
    if (tpath.getPartCount() > opath.getPartCount()) return 1;
    if (tpath.getPartCount() < opath.getPartCount()) return -1;
   
    for (int p=0; p<tpath.getPartCount(); p++) {
      int pc = tpath.getPart(p).compareTo(opath.getPart(p));
      if (pc != 0) return pc;
    }
   
    return 0;
  }
 
  /**
   * Facility method using internal {@link VirtualArtifactHandler} for creating virtual URL's. One of the parents of the
   * artifact or the artifact itself must be directly related to a {@link VirtualArtifactSystem}, otherwise the URL will break
   * when creating new URL's using its protocol and without the special handler.
   *
   * @return A new URL that will allow access to the artifact's input/output
   */
  protected URL createURL () {
    try {
     
      VirtualArtifactSystem vas = VirtualArtifactSystem.getSystemFor(this);
     
      String file = getPath().toString("/") + (this instanceof IVirtualDirectory ? "/" : "");
      if (!file.startsWith("/")) file = "/" + file;
     
      return vas == null ?
        new URL(VirtualArtifactSystem.VAS_PROTOCOL, null, 0, file, new VirtualArtifactURLStreamHandler(this)) :
        new URL(VirtualArtifactSystem.VAS_PROTOCOL, vas.getVASName(), 0, file, new VirtualArtifactURLStreamHandler(this));
       
    } catch (MalformedURLException e) {
      throw ThrowableManagerRegistry.caught(new VirtualArtifactException(this, e));
    }
  }
 
}
TOP

Related Classes of net.sourceforge.javautil.common.io.VirtualArtifactAbstract

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.