Ejemplo n.º 1
0
  public void testCopyRecursively() throws Exception {
    POIFSFileSystem fsD = new POIFSFileSystem();
    POIFSFileSystem fs = new POIFSFileSystem();
    DirectoryEntry dirA = fs.createDirectory("DirA");
    DirectoryEntry dirB = fs.createDirectory("DirB");

    DocumentEntry entryR = fs.createDocument(new ByteArrayInputStream(dataSmallA), "EntryRoot");
    DocumentEntry entryA1 = dirA.createDocument("EntryA1", new ByteArrayInputStream(dataSmallA));
    DocumentEntry entryA2 = dirA.createDocument("EntryA2", new ByteArrayInputStream(dataSmallB));

    // Copy docs
    assertEquals(0, fsD.getRoot().getEntryCount());
    EntryUtils.copyNodeRecursively(entryR, fsD.getRoot());

    assertEquals(1, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("EntryRoot"));

    EntryUtils.copyNodeRecursively(entryA1, fsD.getRoot());
    assertEquals(2, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("EntryRoot"));
    assertNotNull(fsD.getRoot().getEntry("EntryA1"));

    EntryUtils.copyNodeRecursively(entryA2, fsD.getRoot());
    assertEquals(3, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("EntryRoot"));
    assertNotNull(fsD.getRoot().getEntry("EntryA1"));
    assertNotNull(fsD.getRoot().getEntry("EntryA2"));

    // Copy directories
    fsD = new POIFSFileSystem();
    assertEquals(0, fsD.getRoot().getEntryCount());

    EntryUtils.copyNodeRecursively(dirB, fsD.getRoot());
    assertEquals(1, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("DirB"));
    assertEquals(0, ((DirectoryEntry) fsD.getRoot().getEntry("DirB")).getEntryCount());

    EntryUtils.copyNodeRecursively(dirA, fsD.getRoot());
    assertEquals(2, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("DirB"));
    assertEquals(0, ((DirectoryEntry) fsD.getRoot().getEntry("DirB")).getEntryCount());
    assertNotNull(fsD.getRoot().getEntry("DirA"));
    assertEquals(2, ((DirectoryEntry) fsD.getRoot().getEntry("DirA")).getEntryCount());

    // Copy the whole lot
    fsD = new POIFSFileSystem();
    assertEquals(0, fsD.getRoot().getEntryCount());

    EntryUtils.copyNodes(fs, fsD, new ArrayList<String>());
    assertEquals(3, fsD.getRoot().getEntryCount());
    assertNotNull(fsD.getRoot().getEntry(dirA.getName()));
    assertNotNull(fsD.getRoot().getEntry(dirB.getName()));
    assertNotNull(fsD.getRoot().getEntry(entryR.getName()));
    assertEquals(0, ((DirectoryEntry) fsD.getRoot().getEntry("DirB")).getEntryCount());
    assertEquals(2, ((DirectoryEntry) fsD.getRoot().getEntry("DirA")).getEntryCount());
  }
Ejemplo n.º 2
0
 /** Returns the ZipFileIndexEntry for a path, if there is one. */
 synchronized Entry getZipIndexEntry(RelativePath path) {
   try {
     checkIndex();
     DirectoryEntry de = directories.get(path.dirname());
     String lookFor = path.basename();
     return (de == null) ? null : de.getEntry(lookFor);
   } catch (IOException e) {
     return null;
   }
 }
Ejemplo n.º 3
0
 public void fetchFrom(OpenFile file) {
   byte buffer[] = new byte[tableSize * DirectoryEntry.sizeOf()];
   // read the file
   file.readAt(buffer, 0, tableSize * DirectoryEntry.sizeOf(), 0);
   // unmarshall
   int pos = 0;
   for (int i = 0; i < tableSize; i++) {
     table[i].internalize(buffer, pos);
     pos += DirectoryEntry.sizeOf();
   }
 }
Ejemplo n.º 4
0
  public void writeBack(OpenFile file) {
    byte buffer[] = new byte[tableSize * DirectoryEntry.sizeOf()];
    // marshall
    int pos = 0;
    for (int i = 0; i < tableSize; i++) {
      table[i].externalize(buffer, pos);
      pos += DirectoryEntry.sizeOf();
    }

    // write the file
    file.writeAt(buffer, 0, tableSize * DirectoryEntry.sizeOf(), 0);
  }
Ejemplo n.º 5
0
  private boolean readIndex() {
    if (triedToReadIndex || !usePreindexedCache) {
      return false;
    }

    boolean ret = false;
    synchronized (this) {
      triedToReadIndex = true;
      RandomAccessFile raf = null;
      try {
        File indexFileName = getIndexFile();
        raf = new RandomAccessFile(indexFileName, "r");

        long fileStamp = raf.readLong();
        if (zipFile.lastModified() != fileStamp) {
          ret = false;
        } else {
          directories = new LinkedHashMap<RelativeDirectory, DirectoryEntry>();
          int numDirs = raf.readInt();
          for (int nDirs = 0; nDirs < numDirs; nDirs++) {
            int dirNameBytesLen = raf.readInt();
            byte[] dirNameBytes = new byte[dirNameBytesLen];
            raf.read(dirNameBytes);

            RelativeDirectory dirNameStr = getRelativeDirectory(new String(dirNameBytes, "UTF-8"));
            DirectoryEntry de = new DirectoryEntry(dirNameStr, this);
            de.numEntries = raf.readInt();
            de.writtenOffsetOffset = raf.readLong();
            directories.put(dirNameStr, de);
          }
          ret = true;
          zipFileLastModified = fileStamp;
        }
      } catch (Throwable t) {
        // Do nothing
      } finally {
        if (raf != null) {
          try {
            raf.close();
          } catch (Throwable tt) {
            // Do nothing
          }
        }
      }
      if (ret == true) {
        readFromIndex = true;
      }
    }

    return ret;
  }
Ejemplo n.º 6
0
  /** Returns a javac List of filenames within a directory in the ZipFileIndex. */
  public synchronized com.sun.tools.javac.util.List<String> getFiles(RelativeDirectory path) {
    try {
      checkIndex();

      DirectoryEntry de = directories.get(path);
      com.sun.tools.javac.util.List<String> ret = de == null ? null : de.getFiles();

      if (ret == null) {
        return com.sun.tools.javac.util.List.<String>nil();
      }
      return ret;
    } catch (IOException e) {
      return com.sun.tools.javac.util.List.<String>nil();
    }
  }
Ejemplo n.º 7
0
  /** Creates new PostTable */
  protected PostTable(final DirectoryEntry de, final DataInput di) throws IOException {
    this.de = (DirectoryEntry) de.clone();
    version = di.readInt();
    italicAngle = di.readInt();
    underlinePosition = di.readShort();
    underlineThickness = di.readShort();
    isFixedPitch = di.readInt();
    minMemType42 = di.readInt();
    maxMemType42 = di.readInt();
    minMemType1 = di.readInt();
    maxMemType1 = di.readInt();

    if (version == 0x00020000) {
      numGlyphs = di.readUnsignedShort();
      glyphNameIndex = new int[numGlyphs];
      for (int i = 0; i < numGlyphs; i++) {
        glyphNameIndex[i] = di.readUnsignedShort();
      }
      int h = highestGlyphNameIndex();
      if (h > 257) {
        h -= 257;
        psGlyphName = new String[h];
        for (int i = 0; i < h; i++) {
          final int len = di.readUnsignedByte();
          final byte[] buf = new byte[len];
          di.readFully(buf);
          psGlyphName[i] = new String(buf);
        }
      }
      // } else if (version == 0x00025000) {
      // } else if (version == 0x00030000) {
    }
  }
Ejemplo n.º 8
0
  protected NameTable(DirectoryEntry de, RandomAccessFileEmulator raf) throws IOException {
    raf.seek(de.getOffset());
    formatSelector = raf.readShort();
    numberOfNameRecords = raf.readShort();
    stringStorageOffset = raf.readShort();
    records = new NameRecord[numberOfNameRecords];

    // Load the records, which contain the encoding information and string offsets
    for (int i = 0; i < numberOfNameRecords; i++) {
      records[i] = new NameRecord(raf);
    }

    // Now load the strings
    for (int i = 0; i < numberOfNameRecords; i++) {
      records[i].loadString(raf, de.getOffset() + stringStorageOffset);
    }
  }
Ejemplo n.º 9
0
  public void testAreDocumentsIdentical() throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem();
    DirectoryEntry dirA = fs.createDirectory("DirA");
    DirectoryEntry dirB = fs.createDirectory("DirB");

    DocumentEntry entryA1 = dirA.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
    DocumentEntry entryA1b = dirA.createDocument("Entry1b", new ByteArrayInputStream(dataSmallA));
    DocumentEntry entryA2 = dirA.createDocument("Entry2", new ByteArrayInputStream(dataSmallB));
    DocumentEntry entryB1 = dirB.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));

    // Names must match
    assertEquals(false, entryA1.getName().equals(entryA1b.getName()));
    assertEquals(false, EntryUtils.areDocumentsIdentical(entryA1, entryA1b));

    // Contents must match
    assertEquals(false, EntryUtils.areDocumentsIdentical(entryA1, entryA2));

    // Parents don't matter if contents + names are the same
    assertEquals(false, entryA1.getParent().equals(entryB1.getParent()));
    assertEquals(true, EntryUtils.areDocumentsIdentical(entryA1, entryB1));

    // Can work with NPOIFS + POIFS
    ByteArrayOutputStream tmpO = new ByteArrayOutputStream();
    fs.writeFilesystem(tmpO);
    ByteArrayInputStream tmpI = new ByteArrayInputStream(tmpO.toByteArray());
    NPOIFSFileSystem nfs = new NPOIFSFileSystem(tmpI);

    DirectoryEntry dN1 = (DirectoryEntry) nfs.getRoot().getEntry("DirA");
    DirectoryEntry dN2 = (DirectoryEntry) nfs.getRoot().getEntry("DirB");
    DocumentEntry eNA1 = (DocumentEntry) dN1.getEntry(entryA1.getName());
    DocumentEntry eNA2 = (DocumentEntry) dN1.getEntry(entryA2.getName());
    DocumentEntry eNB1 = (DocumentEntry) dN2.getEntry(entryB1.getName());

    assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, eNA2));
    assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, eNB1));

    assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, entryA1b));
    assertEquals(false, EntryUtils.areDocumentsIdentical(eNA1, entryA2));

    assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, entryA1));
    assertEquals(true, EntryUtils.areDocumentsIdentical(eNA1, entryB1));
  }
Ejemplo n.º 10
0
  /** Creates a new instance of BaseTable */
  protected BaseTable(DirectoryEntry de, DataInput di) throws IOException {
    _de = (DirectoryEntry) de.clone();

    // Load entire table into a buffer, and create another input stream
    _buf = new byte[de.getLength()];
    di.readFully(_buf);
    DataInput di2 = getDataInputForOffset(0);

    _version = di2.readInt();
    _horizAxisOffset = di2.readUnsignedShort();
    _vertAxisOffset = di2.readUnsignedShort();
    if (_horizAxisOffset != 0) {
      _horizAxis = new Axis(_horizAxisOffset);
    }
    if (_vertAxisOffset != 0) {
      _vertAxis = new Axis(_vertAxisOffset);
    }

    // Let go of the buffer
    _buf = null;
  }
Ejemplo n.º 11
0
  protected Os2Table(DirectoryEntry de, DataInput di) throws IOException {
    this._de = (DirectoryEntry) de.clone();
    _version = di.readUnsignedShort();
    _xAvgCharWidth = di.readShort();
    _usWeightClass = di.readUnsignedShort();
    _usWidthClass = di.readUnsignedShort();
    _fsType = di.readShort();
    _ySubscriptXSize = di.readShort();
    _ySubscriptYSize = di.readShort();
    _ySubscriptXOffset = di.readShort();
    _ySubscriptYOffset = di.readShort();
    _ySuperscriptXSize = di.readShort();
    _ySuperscriptYSize = di.readShort();
    _ySuperscriptXOffset = di.readShort();
    _ySuperscriptYOffset = di.readShort();
    _yStrikeoutSize = di.readShort();
    _yStrikeoutPosition = di.readShort();
    _sFamilyClass = di.readShort();
    byte[] buf = new byte[10];
    di.readFully(buf);
    _panose = new Panose(buf);
    _ulUnicodeRange1 = di.readInt();
    _ulUnicodeRange2 = di.readInt();
    _ulUnicodeRange3 = di.readInt();
    _ulUnicodeRange4 = di.readInt();
    _achVendorID = di.readInt();
    _fsSelection = di.readShort();
    _usFirstCharIndex = di.readUnsignedShort();
    _usLastCharIndex = di.readUnsignedShort();
    _sTypoAscender = di.readShort();
    _sTypoDescender = di.readShort();
    _sTypoLineGap = di.readShort();
    _usWinAscent = di.readUnsignedShort();
    _usWinDescent = di.readUnsignedShort();
    _ulCodePageRange1 = di.readInt();
    _ulCodePageRange2 = di.readInt();

    // OpenType 1.3
    if (_version == 2) {
      _sxHeight = di.readShort();
      _sCapHeight = di.readShort();
      _usDefaultChar = di.readUnsignedShort();
      _usBreakChar = di.readUnsignedShort();
      _usMaxContext = di.readUnsignedShort();
    }
  }
Ejemplo n.º 12
0
 protected MaxpTable(DirectoryEntry de, RandomAccessFile raf) throws IOException {
   raf.seek(de.getOffset());
   versionNumber = raf.readInt();
   numGlyphs = raf.readUnsignedShort();
   maxPoints = raf.readUnsignedShort();
   maxContours = raf.readUnsignedShort();
   maxCompositePoints = raf.readUnsignedShort();
   maxCompositeContours = raf.readUnsignedShort();
   maxZones = raf.readUnsignedShort();
   maxTwilightPoints = raf.readUnsignedShort();
   maxStorage = raf.readUnsignedShort();
   maxFunctionDefs = raf.readUnsignedShort();
   maxInstructionDefs = raf.readUnsignedShort();
   maxStackElements = raf.readUnsignedShort();
   maxSizeOfInstructions = raf.readUnsignedShort();
   maxComponentElements = raf.readUnsignedShort();
   maxComponentDepth = raf.readUnsignedShort();
 }
Ejemplo n.º 13
0
 protected VheaTable(DirectoryEntry de, DataInput di) throws IOException {
   _de = (DirectoryEntry) de.clone();
   _version = di.readInt();
   _ascent = di.readShort();
   _descent = di.readShort();
   _lineGap = di.readShort();
   _advanceHeightMax = di.readShort();
   _minTopSideBearing = di.readShort();
   _minBottomSideBearing = di.readShort();
   _yMaxExtent = di.readShort();
   _caretSlopeRise = di.readShort();
   _caretSlopeRun = di.readShort();
   for (int i = 0; i < 5; ++i) {
     di.readShort();
   }
   _metricDataFormat = di.readShort();
   _numberOfLongVerMetrics = di.readUnsignedShort();
 }
Ejemplo n.º 14
0
 protected HeadTable(DirectoryEntry de, RandomAccessFileEmulator raf) throws IOException {
   raf.seek(de.getOffset());
   versionNumber = raf.readInt();
   fontRevision = raf.readInt();
   checkSumAdjustment = raf.readInt();
   magicNumber = raf.readInt();
   flags = raf.readShort();
   unitsPerEm = raf.readShort();
   created = raf.readLong();
   modified = raf.readLong();
   xMin = raf.readShort();
   yMin = raf.readShort();
   xMax = raf.readShort();
   yMax = raf.readShort();
   macStyle = raf.readShort();
   lowestRecPPEM = raf.readShort();
   fontDirectionHint = raf.readShort();
   indexToLocFormat = raf.readShort();
   glyphDataFormat = raf.readShort();
 }
Ejemplo n.º 15
0
  private boolean writeIndex() {
    boolean ret = false;
    if (readFromIndex || !usePreindexedCache) {
      return true;
    }

    if (!writeIndex) {
      return true;
    }

    File indexFile = getIndexFile();
    if (indexFile == null) {
      return false;
    }

    RandomAccessFile raf = null;
    long writtenSoFar = 0;
    try {
      raf = new RandomAccessFile(indexFile, "rw");

      raf.writeLong(zipFileLastModified);
      writtenSoFar += 8;

      List<DirectoryEntry> directoriesToWrite = new ArrayList<DirectoryEntry>();
      Map<RelativeDirectory, Long> offsets = new HashMap<RelativeDirectory, Long>();
      raf.writeInt(directories.keySet().size());
      writtenSoFar += 4;

      for (RelativeDirectory dirName : directories.keySet()) {
        DirectoryEntry dirEntry = directories.get(dirName);

        directoriesToWrite.add(dirEntry);

        // Write the dir name bytes
        byte[] dirNameBytes = dirName.getPath().getBytes("UTF-8");
        int dirNameBytesLen = dirNameBytes.length;
        raf.writeInt(dirNameBytesLen);
        writtenSoFar += 4;

        raf.write(dirNameBytes);
        writtenSoFar += dirNameBytesLen;

        // Write the number of files in the dir
        List<Entry> dirEntries = dirEntry.getEntriesAsCollection();
        raf.writeInt(dirEntries.size());
        writtenSoFar += 4;

        offsets.put(dirName, new Long(writtenSoFar));

        // Write the offset of the file's data in the dir
        dirEntry.writtenOffsetOffset = 0L;
        raf.writeLong(0L);
        writtenSoFar += 8;
      }

      for (DirectoryEntry de : directoriesToWrite) {
        // Fix up the offset in the directory table
        long currFP = raf.getFilePointer();

        long offsetOffset = offsets.get(de.dirName).longValue();
        raf.seek(offsetOffset);
        raf.writeLong(writtenSoFar);

        raf.seek(currFP);

        // Now write each of the files in the DirectoryEntry
        List<Entry> list = de.getEntriesAsCollection();
        for (Entry zfie : list) {
          // Write the name bytes
          byte[] zfieNameBytes = zfie.name.getBytes("UTF-8");
          int zfieNameBytesLen = zfieNameBytes.length;
          raf.writeInt(zfieNameBytesLen);
          writtenSoFar += 4;
          raf.write(zfieNameBytes);
          writtenSoFar += zfieNameBytesLen;

          // Write isDir
          raf.writeByte(zfie.isDir ? (byte) 1 : (byte) 0);
          writtenSoFar += 1;

          // Write offset of bytes in the real Jar/Zip file
          raf.writeInt(zfie.offset);
          writtenSoFar += 4;

          // Write size of the file in the real Jar/Zip file
          raf.writeInt(zfie.size);
          writtenSoFar += 4;

          // Write compressed size of the file in the real Jar/Zip file
          raf.writeInt(zfie.compressedSize);
          writtenSoFar += 4;

          // Write java time stamp of the file in the real Jar/Zip file
          raf.writeLong(zfie.getLastModified());
          writtenSoFar += 8;
        }
      }
    } catch (Throwable t) {
      // Do nothing
    } finally {
      try {
        if (raf != null) {
          raf.close();
        }
      } catch (IOException ioe) {
        // Do nothing
      }
    }

    return ret;
  }
Ejemplo n.º 16
0
 private DataInput getDataInputForOffset(int offset) {
   return new DataInputStream(new ByteArrayInputStream(_buf, offset, _de.getLength() - offset));
 }
Ejemplo n.º 17
0
  public static Table create(
      final OTFontCollection fc,
      final OTFont font,
      final DirectoryEntry de,
      final DataInputStream dis)
      throws IOException {
    Table t = null;

    // First, if we have a font collection, look for the table there
    if (fc != null) {
      t = fc.getTable(de);
      if (t != null) {
        return t;
      }
    }

    // Create the table
    switch (de.getTag()) {
      case Table.BASE:
        t = new BaseTable(de, dis);
        break;
      case Table.CFF:
        t = new CffTable(de, dis);
        break;
      case Table.DSIG:
        t = new DsigTable(de, dis);
        break;
      case Table.EBDT:
        break;
      case Table.EBLC:
        break;
      case Table.EBSC:
        break;
      case Table.GDEF:
        break;
      case Table.GPOS:
        t = new GposTable(de, dis);
        break;
      case Table.GSUB:
        t = new GsubTable(de, dis);
        break;
      case Table.JSTF:
        break;
      case Table.LTSH:
        t = new LtshTable(de, dis);
        break;
      case Table.MMFX:
        break;
      case Table.MMSD:
        break;
      case Table.OS_2:
        t = new Os2Table(de, dis);
        break;
      case Table.PCLT:
        t = new PcltTable(de, dis);
        break;
      case Table.VDMX:
        t = new VdmxTable(de, dis);
        break;
      case Table.cmap:
        t = new CmapTable(de, dis);
        break;
      case Table.cvt:
        t = new CvtTable(de, dis);
        break;
      case Table.fpgm:
        t = new FpgmTable(de, dis);
        break;
      case Table.fvar:
        break;
      case Table.gasp:
        t = new GaspTable(de, dis);
        break;
      case Table.glyf:
        t = new GlyfTable(de, dis, font.getMaxpTable(), font.getLocaTable());
        break;
      case Table.hdmx:
        t = new HdmxTable(de, dis, font.getMaxpTable());
        break;
      case Table.head:
        t = new HeadTable(de, dis);
        break;
      case Table.hhea:
        t = new HheaTable(de, dis);
        break;
      case Table.hmtx:
        t = new HmtxTable(de, dis, font.getHheaTable(), font.getMaxpTable());
        break;
      case Table.kern:
        t = new KernTable(de, dis);
        break;
      case Table.loca:
        t = new LocaTable(de, dis, font.getHeadTable(), font.getMaxpTable());
        break;
      case Table.maxp:
        t = new MaxpTable(de, dis);
        break;
      case Table.name:
        t = new NameTable(de, dis);
        break;
      case Table.prep:
        t = new PrepTable(de, dis);
        break;
      case Table.post:
        t = new PostTable(de, dis);
        break;
      case Table.vhea:
        t = new VheaTable(de, dis);
        break;
      case Table.vmtx:
        t = new VmtxTable(de, dis, font.getVheaTable(), font.getMaxpTable());
        break;
    }

    // If we have a font collection, add this table to it
    if ((fc != null) && (t != null)) {
      fc.addTable(t);
    }
    return t;
  }
  private List<DirectoryEntry> readTemplateDirectory() {
    Pattern linepattern = Pattern.compile("\\s*([^\\s]+)\\s*=\\s*(.*)");

    Map<String, DirectoryEntry> categoryMap = new HashMap<>();

    List<DirectoryEntry> categories = new ArrayList<>();

    String url = Activator.getTemplatesPath();
    if (url == null || url.isEmpty()) {
      return categories;
    }

    File baseDirectory = new File(url);
    File[] files =
        baseDirectory.listFiles(
            new FilenameFilter() {

              @Override
              public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
              }
            });

    for (File file : files) {
      try (BufferedReader br =
          new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
        DirectoryEntry entry = new DirectoryEntry(null);
        String line = null;
        while ((line = br.readLine()) != null) {
          Matcher m = linepattern.matcher(line);
          if (m.matches()) {
            String key = m.group(1);
            String value = m.group(2);
            switch (key.toLowerCase()) {
              case "name":
                entry.name = value;
                break;
              case "category":
                DirectoryEntry category = categoryMap.get(value);
                if (category == null) {
                  category = new DirectoryEntry(value);
                  categoryMap.put(value, category);
                  categories.add(category);
                }
                category.children.add(entry);
                break;
              case "description":
                entry.description = value;
                break;
              case "file":
                {
                  File f = new File(value);
                  if (!f.exists()) {
                    f = new File(baseDirectory, value);
                  }
                  entry.file = f.getAbsolutePath();
                  break;
                }
              default:
                if (key.startsWith("repl.")) {
                  key = key.substring("repl.".length());
                  entry.patterns.put(key, value);
                }
            }
          }
        }
      } catch (IOException ioe) {

      }
    }

    return categories;
  }
Ejemplo n.º 19
0
  public void testAreDirectoriesIdentical() throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem();
    DirectoryEntry dirA = fs.createDirectory("DirA");
    DirectoryEntry dirB = fs.createDirectory("DirB");

    // Names must match
    assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA, dirB));

    // Empty dirs are fine
    DirectoryEntry dirA1 = dirA.createDirectory("TheDir");
    DirectoryEntry dirB1 = dirB.createDirectory("TheDir");
    assertEquals(0, dirA1.getEntryCount());
    assertEquals(0, dirB1.getEntryCount());
    assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));

    // Otherwise children must match
    dirA1.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
    assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));

    dirB1.createDocument("Entry1", new ByteArrayInputStream(dataSmallA));
    assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));

    dirA1.createDirectory("DD");
    assertEquals(false, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));
    dirB1.createDirectory("DD");
    assertEquals(true, EntryUtils.areDirectoriesIdentical(dirA1, dirB1));

    // Excludes support
    List<String> excl = Arrays.asList(new String[] {"Ignore1", "IgnDir/Ign2"});
    FilteringDirectoryNode fdA = new FilteringDirectoryNode(dirA1, excl);
    FilteringDirectoryNode fdB = new FilteringDirectoryNode(dirB1, excl);

    assertEquals(true, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    // Add an ignored doc, no notice is taken
    fdA.createDocument("Ignore1", new ByteArrayInputStream(dataSmallA));
    assertEquals(true, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    // Add a directory with filtered contents, not the same
    DirectoryEntry dirAI = dirA1.createDirectory("IgnDir");
    assertEquals(false, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    DirectoryEntry dirBI = dirB1.createDirectory("IgnDir");
    assertEquals(true, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    // Add something to the filtered subdir that gets ignored
    dirAI.createDocument("Ign2", new ByteArrayInputStream(dataSmallA));
    assertEquals(true, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    // And something that doesn't
    dirAI.createDocument("IgnZZ", new ByteArrayInputStream(dataSmallA));
    assertEquals(false, EntryUtils.areDirectoriesIdentical(fdA, fdB));

    dirBI.createDocument("IgnZZ", new ByteArrayInputStream(dataSmallA));
    assertEquals(true, EntryUtils.areDirectoriesIdentical(fdA, fdB));
  }
Ejemplo n.º 20
0
 protected FpgmTable(DirectoryEntry de, RandomAccessFileEmulator raf) throws IOException {
   raf.seek(de.getOffset());
   readInstructions(raf, de.getLength());
 }
  @Override
  public void serialize(Collection<DirectoryEntry> entries, OutputStream os) throws IOException {
    ArrayList<DirectoryEntry> sortedEntries = new ArrayList<DirectoryEntry>(entries);
    Collections.sort(sortedEntries);

    boolean needBzNamespace = false, needDcNamespace = false;
    for (FileInfo f : sortedEntries) {
      if (f.getFsObjectType() == FSObjectType.BLOB) {
        if (f.getLastModificationTime() != -1) needDcNamespace = true;
        if (f.getSize() != -1) needBzNamespace = true;
      }
    }

    Writer w = new OutputStreamWriter(os, "UTF-8");
    w.write("<Directory xmlns=\"http://ns.nuke24.net/ContentCouch/\"");
    if (needBzNamespace) w.write(" xmlns:bz=\"http://bitzi.com/xmlns/2002/01/bz-core#\"");
    if (needDcNamespace) w.write(" xmlns:dc=\"http://purl.org/dc/terms/\"");
    w.write(" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n");
    if (sortedEntries.size() > 0) {
      w.write("\t<entries rdf:parseType=\"Collection\">\n");

      for (DirectoryEntry f : sortedEntries) {
        String tag;
        boolean showSize, showMtime;
        switch (f.getFsObjectType()) {
          case BLOB:
            tag = "Blob";
            showSize = f.getSize() != -1;
            showMtime = everIncludeMtime && f.getLastModificationTime() != -1;
            break;
          case DIRECTORY:
            tag = "Directory";
            showSize = false;
            showMtime = false;
            break;
          default:
            throw new RuntimeException(
                "Don't know how to encode directory entry with file type " + f.getFsObjectType());
        }

        w.write("\t\t<DirectoryEntry>\n");
        w.write("\t\t\t<name>" + XMLUtil.xmlEscapeText(f.name) + "</name>\n");
        w.write("\t\t\t<target>\n");
        w.write(
            "\t\t\t\t<" + tag + " rdf:about=\"" + XMLUtil.xmlEscapeAttribute(f.getUrn()) + "\"");
        if (showSize) {
          w.write(">\n");
          w.write("\t\t\t\t\t<bz:fileLength>" + String.valueOf(f.getSize()) + "</bz:fileLength>\n");
          w.write("\t\t\t\t</" + tag + ">\n");
        } else {
          w.write("/>\n");
        }
        w.write("\t\t\t</target>\n");
        if (showMtime) {
          w.write(
              "\t\t\t<dc:modified>"
                  + DateUtil.formatDate(f.getLastModificationTime())
                  + "</dc:modified>\n");
        }
        w.write("\t\t</DirectoryEntry>\n");
      }
      w.write("\t</entries>\n");
    } else {
      w.write("\t<entries rdf:parseType=\"Collection\"/>\n");
    }
    w.write("</Directory>\n");
    w.flush();
  }