Package nz.govt.natlib.fx

Examples of nz.govt.natlib.fx.DataSource


    return "Adapts JPEG Graphics Files, including any EXIF data found";
  }

  public void adapt(File oFile, ParserContext ctx) throws IOException {
    // Add the MetaData to the tree!
    DataSource ftk = new FileDataSource(oFile);
    // Header and default information
    ctx.fireStartParseEvent("JPG");
    writeFileInfo(oFile, ctx);
    try {
      JpgMarker marker = null;

      // watchdog variable - for untydy or invalid jpgs
      long posWatchDog = ftk.getPosition();
      long posWatchDogLoops = 0;
      boolean sos = false;
      boolean jfif = false;
      boolean exif = false;
      boolean loop = true;
     
      while (loop) {
        marker = readMarker(ftk);
        long iPos = ftk.getPosition(); // get this now, so whatever the
                        // 'readers' do we can always
                        // move to the next marker

        // System.out.println(iPos+":"+marker);
        // readers of particular markers...
        if (marker.delim == 0xFF) {
          switch ((int) marker.type) {
          case 0xe0: {
            readJFIF(ftk, ctx, marker);
            jfif = true;
            break;
          }
          case 0xe1: {
            if (!exif) {
              readEXIF(ftk, ctx, marker);
            }
            exif = true;
            break;
          }
          case 0xda: {
            readScan(ftk, ctx, marker);
            sos = true;
            break;
          }
          case 0xc0:
            readFrame(ftk, ctx, marker);
            break;
          case 0xfe:
            readComment(ftk, ctx, marker);
            break;
          case 0xd8:
            readStartOfImage(ftk, ctx, marker);
            break;
          case 0xd9:
            readEndOfImage(ftk, ctx, marker);
            break;
          default:
            String name = JpgUtil.getJpgMarkerName(marker.delim,
                marker.type);
            ctx.fireStartParseEvent(name);
            ctx.fireEndParseEvent(name);
            break;
          }
        }

        if (marker.type == 0xda) {
          // after a 'Start Of Scan' the rest is the IMAGE itself, so
          // move the file pos to the end of the file (less 2 bytes),
          // to pick up the EOF
          ftk.setPosition(oFile.length() - 2);
        } else {
          ftk.setPosition(marker.length + iPos);
        }

        if (marker.type == 0xd9) {
          break; // this is the EOF marker, we are done!
        }

        // WATCH DOG LOOP CHECK FOR JPGS THAT DON'T GO ANYWHERE
        if (posWatchDog == ftk.getPosition()) {
          posWatchDogLoops++;
        } else {
          posWatchDogLoops = 0;
        }
        if (posWatchDogLoops == 2) {
          if (sos && (jfif || exif)) {
            loop = false; // just finish and tidy up - you got
                    // everything you could...
            LogManager.getInstance()
                .logMessage(
                    LogMessage.INFO,
                    "Early termination of "
                        + oFile.getName()
                        + " after Scan Data, "
                        + (exif ? "EXIF"
                            : (jfif ? "JFIF" : ""))
                        + " data WAS harvested");
          } else {
            LogManager
                .getInstance()
                .logMessage(
                    LogMessage.ERROR,
                    "Early termination of "
                        + oFile.getName()
                        + " before Scan Data, no data was harvested");
            throw new RuntimeException(
                "Endless Loop detected in JPG Data of "
                    + oFile.getName() + "");
          }
        }
        posWatchDog = ftk.getPosition();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new RuntimeException(ex);
    } finally {
      ctx.fireEndParseEvent("JPG");
      ftk.close();
    }
  }
View Full Code Here


      this.into = into;
    }

    public void processPOIFSReaderEvent(POIFSReaderEvent event) {

      DataSource ftk = null;
      try {
        long dopPos = into.getIntAttribute("MSExcel.FIB.dopOffset");
        ftk = new DocumentDataSource(event.getStream());
        into.fireStartParseEvent("Properties");
        OLEUtils.getDOPProperties(ftk, (int) dopPos, into);
        into.fireEndParseEvent("Properties");
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        if (ftk != null) {
          try {
            ftk.close();
          } catch (Exception ex) {
            ;
          }
        }
      }
View Full Code Here

    public MainStreamReader(ParserContext into) {
      this.into = into;
    }

    public void processPOIFSReaderEvent(POIFSReaderEvent event) {
      DataSource ftk = null;
      try {
        ftk = new DocumentDataSource(event.getStream());

        into.fireStartParseEvent("header");
        OLEUtils.getHeader(ftk, into);
        into.fireEndParseEvent("header");

      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        if (ftk != null) {
          try {
            ftk.close();
          } catch (Exception ex) {
            ;
          }
        }
      }
View Full Code Here

  public void adapt(File file, ParserContext ctx) throws IOException {
    long SECS = 1000;
    long MINS = 60 * SECS;
    long HRS = 60 * MINS;
    // Add the MetaData to the tree!
    DataSource ftk = new FileDataSource(file);
    ctx.fireStartParseEvent("mp3");
    writeFileInfo(file, ctx);
    try {
      boolean headFound = false;

      // read the ID3v2.x tag...
      ftk.setPosition(0);
      String id3 = FXUtil.getFixedStringValue(ftk, 3);
      if (id3.equalsIgnoreCase("ID3")) {
        headFound = true;
        ctx.fireStartParseEvent("header");
        byte[] ver = ftk.getData(1);
        byte[] subVer = ftk.getData(1);
        byte[] fg = ftk.getData(1);
        byte[] s = ftk.getData(4);
        int version = (int) FXUtil.getNumericalValue(ver, true);
        int subVersion = (int) FXUtil.getNumericalValue(subVer, true);
        int flags = (int) FXUtil.getNumericalValue(fg, true);
        int size = (int) FXUtil.getNumericalValue(s, true);
        boolean unsync = BitFieldUtil.isSet(flags, 64);
        boolean ext = BitFieldUtil.isSet(flags, 32);
        boolean exp = BitFieldUtil.isSet(flags, 16);
        boolean foot = BitFieldUtil.isSet(flags, 8);
        size = getSyncsafeInt(size);

        ctx.fireParseEvent("tag-version", "ID3v2." + version + "."
            + subVersion);

        // if there is an extended header then that needs to be read...
        if (ext) {
          byte[] extS = ftk.getData(4);
          int extSize = (int) FXUtil.getNumericalValue(extS, true);
          extSize = getSyncsafeInt(extSize);
          // move past the crap...
          ftk.setPosition(ftk.getPosition() + extSize - 4);
        }

        // allow for the footer to be present
        if (foot) {
          size -= 10;
        }

        // now read the frames...
        int read = 0;
        while (read < size) {
          String frameType = FXUtil.getFixedStringValue(ftk, 4);
          byte[] fS = ftk.getData(4);
          byte[] frameFlags = ftk.getData(2);
          int frameSize = (int) FXUtil.getNumericalValue(fS, true);
          frameSize = getSyncsafeInt(frameSize);
          String data = FXUtil.getFixedStringValue(ftk, frameSize);
          ID3v2Tag v2tag = MP3Util.getTag(frameType, data);
          if (v2tag != null) {
            HashMap map = v2tag.getMap();
            if (map != null) {
              ctx.fireParseEvent(v2tag.getName(), v2tag
                  .getValue(), false, map);
            } else {
              ctx.fireParseEvent(v2tag.getName(), v2tag
                  .getValue());
            }
          }
          read += frameSize + 10;
        }

        ctx.fireEndParseEvent("header");
      }

      // read the ID3v1.x tag...
      if (!headFound) {
        ftk.setPosition(file.length() - 128);
        String tag = FXUtil.getFixedStringValue(ftk, 3);
        if (tag.equalsIgnoreCase("TAG")) {
          ctx.fireStartParseEvent("header");
          ctx.fireParseEvent("tag-version", "ID3v1.x");
          ctx.fireParseEvent("version", "1.x");
          id3v11Element.read(ftk, ctx);
          ctx.fireEndParseEvent("header");
          headFound = true;
        }
      }

      // what to do if headFound = false?
      if (!headFound) {
        ctx.fireStartParseEvent("header");
        ctx.fireStartParseEvent("!-- no header found--");
        ctx.fireEndParseEvent("header");
      }

      // now scan the music file - as a stream - to find a frame start
      // index point
      // MP3's can be cut in half and still work, like worms!
      boolean seek = true;
      int seekTo = 0;
      byte previous = 0x00;
      byte current = 0x00;
      while (seekTo < file.length() && seek) {
        ftk.setPosition(seekTo);
        byte[] c = ftk.getData(1024);
        for (int i = 0; i < c.length; i++) {
          previous = current;
          current = c[i];
          seekTo++;
          if (previous == (byte) 0xFF) {
            // posible frame buffer index point...
            // if the current has the most significant 3 bits set
            // then we are in!
            if ((current & 0xE0) == 0xE0) {
              seekTo -= 2;
              seek = false;
              break;
            }
          }
        }
      }
      // the above line finds the next sync index point - now we are good
      // to go!
      ftk.setPosition(seekTo);
      int fsync = (int) FXUtil.getNumericalValue(ftk, 1, false);
      int type = (int) FXUtil.getNumericalValue(ftk, 1, false);
      int version = BitFieldUtil.getNumber(type, 5, 4);
      int layer = BitFieldUtil.getNumber(type, 3, 2);
      boolean pro = BitFieldUtil.isSet(type, 0x01);
      int bitRate = (int) FXUtil.getNumericalValue(ftk, 1, false);
      int bitRateIndex = BitFieldUtil.getNumber(bitRate, 8, 5);
      int realBitRate = getBitRate(version, layer, bitRateIndex);
      int sampleRateIndex = BitFieldUtil.getNumber(bitRate, 4, 3);
      int realSampleRate = getSampleRate(version, layer, sampleRateIndex);
      boolean pad = BitFieldUtil.isSet(bitRate, 0x02);
      boolean priv = BitFieldUtil.isSet(bitRate, 0x01);
      int mode = (int) FXUtil.getNumericalValue(ftk, 1, false);
      int channels = BitFieldUtil.getNumber(mode, 8, 7);
      int modeExt = BitFieldUtil.getNumber(mode, 6, 5);
      boolean copyright = BitFieldUtil.isSet(mode, 0x08);
      boolean original = BitFieldUtil.isSet(mode, 0x04);
      int emph = BitFieldUtil.getNumber(mode, 2, 1);
      long durationMs = getDuration(realBitRate, realSampleRate,
          channels, file.length());
      long durationHrs = durationMs / HRS;
      long durationMins = (durationMs - (durationHrs * HRS)) / MINS;
      long durationSecs = (durationMs - (durationHrs * HRS) - (durationMins * MINS))
          / SECS;
      long durationMsecs = durationMs - (durationHrs * HRS)
          - (durationMins * MINS) - (durationSecs * SECS);

      ctx.fireStartParseEvent("MPEG");
      ctx.fireParseEvent("version-name", getVersionName(version));
      ctx.fireParseEvent("version", getVersionNumber(version));
      ctx.fireParseEvent("layer-name", getLayerName(layer));
      ctx.fireParseEvent("layer", getLayerNumber(layer));
      ctx.fireParseEvent("protection", pro);
      ctx.fireParseEvent("bit-rate", realBitRate);
      ctx.fireParseEvent("bit-rate-unit", "kbps");
      ctx.fireParseEvent("sample-rate", realSampleRate);
      ctx.fireParseEvent("sample-rate-unit", "Hz");
      ctx.fireParseEvent("mode", getMode(channels));
      ctx.fireParseEvent("channels", getChannels(channels));
      if (channels == 1) {
        ctx.fireParseEvent("mode-extension", getModeExtension(layer,
            modeExt));
      }
      ctx.fireStartParseEvent("duration");
      ctx.fireParseEvent("total-milliseconds", durationMs);
      ctx.fireParseEvent("hours", durationHrs);
      ctx.fireParseEvent("minutes", durationMins);
      ctx.fireParseEvent("seconds", durationSecs);
      ctx.fireParseEvent("ms", durationMsecs);
      ctx.fireEndParseEvent("duration");
      ctx.fireParseEvent("copyright", copyright);
      ctx.fireParseEvent("original", original);
      ctx.fireParseEvent("emph", getEmph(emph));
      ctx.fireEndParseEvent("MPEG");

    } catch (Exception ex) {
      throw new RuntimeException(ex);
    } finally {
      ftk.close();
    }
    ctx.fireEndParseEvent("mp3");
  }
View Full Code Here

  private static final LanguageMap langMap = OLELanguageMap.getLanguageMap();

  public boolean acceptsFile(File file) {
    String name = file.getName().toLowerCase();
    if (WordUtils.isDocFile(file) == true) {
      DataSource ftk = null;
      try {
        ftk = new FileDataSource(file);
        long signature = FXUtil.getNumericalValue(ftk,
            IntegerElement.LONG_SIZE, false);
        if (signature == OLE_TYPE_SIGNATURE) {
View Full Code Here

    wordHeader.read(ftk, ctx);
  }

  public static boolean isWord2(File file) throws java.io.IOException {
    DataSource ftk = null;
    try {
      ftk = new FileDataSource(file);
      ftk.setPosition(Offset_To_NFib);
      int iNFib = (int) FXUtil.getNumericalValue(ftk,
          IntegerElement.SHORT_SIZE, false);
      // System.out.println("iNFib " + Integer.toHexString(iNFib));
      // NFib >= 101 => Word6 or greater
      if (iNFib < 0x101) {
View Full Code Here

      AdapterUtils.close(ftk);
    }
  }

  public static boolean isOLEWord(File file) throws java.io.IOException {
    DataSource ftk = new FileDataSource(file);
    ftk.setPosition(Offset_To_NFib);
    int iNFib = (int) FXUtil.getNumericalValue(ftk,
        IntegerElement.SHORT_SIZE, false);
    // NFib >= 101 => Word6 or greater
    if (iNFib >= 0x101) {
      return true;
View Full Code Here

    return "1.0";
  }

  public boolean acceptsFile(File file) {
    boolean wp = false;
    DataSource ftk = null;
    try {
      ftk = new FileDataSource(file);
      // Header and default information
      String head = FXUtil.getFixedStringValue(ftk, 4);
      if ((head.equals("�WPC"))) {
View Full Code Here

    return "Adapts all Corel WordPerfect files from version 5.1 to 12.0";
  }

  public void adapt(File file, ParserContext ctx) throws IOException {
    // Add the MetaData to the tree!
    DataSource ftk = new FileDataSource(file);
    ctx.fireStartParseEvent("wordperfect");
    writeFileInfo(file, ctx);
    // ctx.fireParseEvent("Version", "");
    try {
      ctx.fireStartParseEvent("header");
      wpHeaderElement.read(ftk, ctx);
      ctx.fireEndParseEvent("header");
      boolean fivePointOnePlus = ctx
          .getIntAttribute("wordperfect.header.minor-version") >= 1;
      boolean sixPlus = Double.parseDouble(ctx
          .getAttribute("wordperfect.header.major-version")
          + "") >= 6;

      if (sixPlus) {
        // next version should encompass WordPerfect 6.0+
      } else {

        IndexHeaderElement indexReader = new IndexHeaderElement();
        PacketHeaderElement blockHead = new PacketHeaderElement();
        boolean readingBlocks = true;
        boolean summary = false;
        while (readingBlocks) {
          indexReader.read(ftk, ctx);
          // System.out.println(indexReader);
          for (int i = 0; i < indexReader.count - 1; i++) {
            blockHead.read(ftk, ctx);
            // arrange to retrive the data from the appropriate
            // place
            long pos = ftk.getPosition();
            ftk.setPosition(blockHead.dataOffset);

            // System.out.println(" >"+blockHead);
            switch (blockHead.getType()) {
            case 0xc:
              ctx.fireStartParseEvent("printer");
              printerElement.read(ftk, ctx);
              ctx.fireEndParseEvent("printer");
              break;
            case 0x8:
              ctx.fireStartParseEvent("graphics");
              graphicElement.read(ftk, ctx);
              ctx.fireEndParseEvent("graphics");
              break;
            case 0x6:
              ctx.fireStartParseEvent("document");
              documentElement.read(ftk, ctx);
              ctx.fireEndParseEvent("document");
              break;
            case 0x1:
              ctx.fireStartParseEvent("summary");
              summary = true;
              System.out.println("Found Sumary at :"
                  + ftk.getPosition());
              summaryElement.read(ftk, ctx);
              if (fivePointOnePlus) {
                summary2Element.read(ftk, ctx);
              }
              ctx.fireEndParseEvent("summary");
              break;
            }

            // move back to the next block...
            ftk.setPosition(pos);
          }

          // now move to the next block...
          if (indexReader.getNextBlockOffset() == 0) {
            readingBlocks = false;
            break;
          }
          ftk.setPosition(indexReader.getNextBlockOffset());
        }

        if (!summary) {
          ctx.fireParseEvent("summary", "");
        }
View Full Code Here

  }

  public void adapt(File file, ParserContext ctx) throws IOException {
    HashMap results = new HashMap();
    // add the MetaData to the tree!
    DataSource ftk = null;
   
    try {
      ftk = new FileDataSource(file);
      ctx.fireStartParseEvent("tiff");
      writeFileInfo(file, ctx);
 
      // work out the endian...
      long endianIndicator = FXUtil.getNumericalValue(ftk,
          IntegerElement.SHORT_SIZE, false);
      boolean bigEndian = endianIndicator == 0x4D4D;
      long version = FXUtil.getNumericalValue(ftk, IntegerElement.SHORT_SIZE,
          bigEndian);
 
      ctx.fireStartParseEvent("Header");
      ctx.fireParseEvent("LittleEndian", !bigEndian);
      ctx.fireParseEvent("Version", "1.0");
      ctx.fireEndParseEvent("Header");
 
      // read the position of the first IFD record...
      long next = FXUtil.getNumericalValue(ftk, IntegerElement.INT_SIZE,
          bigEndian);
 
      // READ THE IFD DIRECTORIES
      while (next != 0x00) {
        ftk.setPosition(next);
        Element tiffIFDRecord = new ImageFileDirectory(bigEndian);
        ctx.fireStartParseEvent("ImageFileDirectory");
        tiffIFDRecord.read(ftk, ctx);
        ctx.fireEndParseEvent("ImageFileDirectory");
 
View Full Code Here

TOP

Related Classes of nz.govt.natlib.fx.DataSource

Copyright © 2018 www.massapicom. 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.