Class VirtualContainer

java.lang.Object
org.ofdrw.pkg.container.VirtualContainer
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
AnnotsDir, DocDir, OFDDir, PageDir, PagesDir, ResDir, SignDir, SignsDir, TempsDir

public class VirtualContainer extends Object implements Closeable
虚拟容器对象
Since:
2020-04-02 19:01:04
Author:
权观宇
  • Constructor Details

  • Method Details

    • getContainerName

      public String getContainerName()
      获取虚拟容器的名称
      Returns:
      名称
    • syncCacheWithFileSystem

      public VirtualContainer syncCacheWithFileSystem()
      使目录与文件缓存与当前文件系统保持一致(仅用于读取方向)

      用于兼容文档中路径大小写与压缩包内实际路径不一致的情况: 当按文档路径精确定位容器失败、而按大小写不敏感方式命中实际目录时, 需要把缓存中不存在的名称键清理掉,否则后续按实际目录名查找会被 先前写入的旧键阻断(缓存按名称命中前会先检查目录是否存在)。

      该方法只清理键名与文件系统不一致的缓存项,不会创建任何目录或文件。

      Returns:
      this
    • getSysAbsPath

      public String getSysAbsPath()
      获取当前容器完整路径
      Returns:
      容器完整路径(绝对路径)
    • setContainerPath

      public VirtualContainer setContainerPath(String actualFullPath)
      修正容器的文件系统路径与名称

      用于兼容文档中路径大小写与压缩包内实际路径不一致的情况: 虚拟容器按文档中的名称创建时,其系统路径使用的是文档中的大小写, 在大小写敏感的文件系统上无法访问到实际目录,这里按实际存在的路径修正。

      该方法不创建目录、不改变容器缓存,仅修正路径与名称。

      Parameters:
      actualFullPath - 实际存在的目录路径(绝对路径)
      Returns:
      this
      Throws:
      IllegalArgumentException - 参数为空或目录不存在
    • putFile

      public VirtualContainer putFile(Path file) throws IOException
      向虚拟容器中加入文件
      Parameters:
      file - 文件路径对象
      Returns:
      this
      Throws:
      IOException - IO异常
    • putFileWithPath

      public Path putFileWithPath(Path file) throws IOException
      向虚拟容器中加入文件,并获取文件在容器中的绝对路径
      Parameters:
      file - 文件路径对象
      Returns:
      null 或 文件在容器中的绝对路径
      Throws:
      IOException - IO异常
    • addRaw

      public VirtualContainer addRaw(String fileName, InputStream in) throws IOException
      向虚拟容器中直接加入流类型资源

      根据提供文件名称创建文件

      输入流内容将直接写入文件内,不做检查

      若文件已经存在,那么将会覆盖原文件!

      Parameters:
      fileName - 文件名称
      in - 输入流,流的关闭应由调用者负责
      Returns:
      this
      Throws:
      IOException - 文件复制异常
    • putObj

      public VirtualContainer putObj(String fileName, org.dom4j.Element element)
      向虚拟容器加入对象
      Parameters:
      fileName - 文件名
      element - 元素对象
      Returns:
      this
    • getObj

      public org.dom4j.Element getObj(ST_Loc loc) throws org.dom4j.DocumentException
      通过路径获取元素

      例如:

      当前

      /Doc_0/Pages,获取 /Doc_0/Pages/Page_0/Content.xml 结果 Page Element

      当前

      /Doc_0/Tpl 结果 获取 /Doc_0/Pages/Page_0/Content.xml 结果 null

      当前

      /Doc_0/Pages 结果 获取 Page_0/Content.xml 结果 Page Element
      Parameters:
      loc - 元素所在路径,支持相对路径,若是绝对路径且不在当前容器中,那么返回null
      Returns:
      元素对象,不存在返回null
      Throws:
      org.dom4j.DocumentException - 元素序列化异常
    • getFile

      public Path getFile(ST_Loc loc)
      通过路径获取文件

      注意在文档尚未关闭之前,获取的元素对象是缓存的,此时若获取文件将会出现 FileNotFoundException。

      获取获取时若使用绝对路径,与当前目录没有相同的前缀则返回null。

      例如:

      当前

      /Doc_0/Pages,获取 /Doc_0/Pages/Page_0/Content.xml 结果 Content.xml

      当前

      /Doc_0/Pages,获取 /Doc_0/Pages/Page_0/Content.xml 结果 null

      当前

      /Doc_0/Pages,获取 Page_0/Content.xml 结果 Content.xml
      Parameters:
      loc - 文件路径,可以是相对路径,也可以是绝对路径。
      Returns:
      文件路径 或 文件不存在返回null。
    • getObj

      public org.dom4j.Element getObj(String fileName) throws FileNotFoundException, org.dom4j.DocumentException
      通过文件名获取元素对象
      Parameters:
      fileName - 文件名
      Returns:
      元素对象(不含代理)
      Throws:
      FileNotFoundException - 文件不存在
      org.dom4j.DocumentException - 元素序列化异常
    • getFile

      public Path getFile(String fileName) throws FileNotFoundException
      获取容器中的文件对象
      Parameters:
      fileName - 文件名称
      Returns:
      文件路径对象
      Throws:
      FileNotFoundException - 文件不存在
    • obtainContainer

      public <R extends VirtualContainer> R obtainContainer(String name, Function<Path,R> mapper)
      获取一个虚拟容器对象

      如果容器存在,那么取出元素

      如果容器不存在,那么创建一个新的对象

      Type Parameters:
      R - 容器子类
      Parameters:
      name - 容器名称
      mapper - 容器构造器引用
      Returns:
      新建或已经存在的容器
    • getContainer

      public <R extends VirtualContainer> R getContainer(String name, Function<Path,R> mapper) throws FileNotFoundException
      获取虚拟容器
      Type Parameters:
      R - 容器子类
      Parameters:
      name - 容器名称
      mapper - 容器构造器引用
      Returns:
      容器对象
      Throws:
      FileNotFoundException - 文件不存在
    • getParent

      public VirtualContainer getParent()
      获取该容器所属容器
      Returns:
      所属容器对象
    • setParent

      protected VirtualContainer setParent(VirtualContainer parent)
      设置所属容器
      Parameters:
      parent - 容器
      Returns:
      this
    • getContainerPath

      public Path getContainerPath()
      获取虚拟容器所处的文件系统路径
      Returns:
      文件系统路径
    • exit

      @Deprecated public boolean exit(String fileName)
      Deprecated.
      判断文件或对象是否存在(错误的函数名称)
      Parameters:
      fileName - 文件名称
      Returns:
      true - 存在;false - 不存在
    • exist

      public boolean exist(String fileName)
      判断文件或对象是否存在
      Parameters:
      fileName - 文件名称
      Returns:
      true - 存在;false - 不存在
    • deleteFile

      public void deleteFile(String fileName) throws IOException
      删除文件

      注意该方法只能删除当前容器中的文件,不能删除子容器中的文件。

      Parameters:
      fileName - 文件名称
      Throws:
      IOException - 文件删除异常
    • clean

      public void clean()
      删除整个虚拟容器
    • flush

      public void flush() throws IOException
      将缓存中的对象写入到文件系统中
      Throws:
      IOException - 文件读写IO异常
    • flushContainerByName

      public VirtualContainer flushContainerByName(String name) throws IOException
      从缓存中刷新指定容器到文件系统中
      Parameters:
      name - 容器名称
      Returns:
      this
      Throws:
      IOException - 写入文件IO异常
    • flushFileByName

      public VirtualContainer flushFileByName(String name) throws IOException
      从缓存将指定对象写入到文件系统中
      Parameters:
      name - 文件名称
      Returns:
      this
      Throws:
      IOException - 写入文件IO异常
    • getAbsLoc

      public ST_Loc getAbsLoc()
      获取在容器中的绝对路径
      Returns:
      绝对路径对象
    • putFile

      public void putFile(String fileName, InputStream in) throws IOException
      向虚拟容器中加入文件
      Parameters:
      fileName - 文件名称
      in - 输入流,流的关闭应由调用者负责
      Throws:
      IOException - 文件复制异常
    • putFile

      public void putFile(String fileName, byte[] data) throws IOException
      向虚拟容器中加入文件
      Parameters:
      fileName - 文件名称
      data - 数据内容
      Throws:
      IOException - 文件写入异常
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException