Package nz.govt.natlib.fx

Examples of nz.govt.natlib.fx.FileDataSource


  public void adapt(File file, ParserContext ctx) throws IOException {
    // add the MetaData to the tree!
    DataSource ftk = null;
   
    try {
      ftk = new FileDataSource(file);
     
      ctx.fireStartParseEvent("WAV");
      writeFileInfo(file, ctx);
 
      ctx.fireStartParseEvent("RIFF");
View Full Code Here


   
    try {
      // Start to read the bitmap, and make sure that the first
      // two bytes are "BM", which is the standard "magic bytes" for
      // a bitmap file.
      ftk = new FileDataSource(file);
      String header = FXUtil.getFixedStringValue(ftk, 2);
      bmp = header.equalsIgnoreCase("bm");
    } catch (Exception ex) {
      // We got an exception trying to read the expected characters.
      // Perhaps the file doesn't have two bytes. Whatever the
View Full Code Here

  public String getVersion() {
    return "1.0";
  }

  public void adapt(File file, ParserContext ctx) throws IOException {
    DataSource ftk = new FileDataSource(file);

    // Open the BMP section of the metadata file. Part of the
    // bmp.dtd defintion is that the native format starts with an
    // opening <BMP> tag.
    ctx.fireStartParseEvent("BMP");

    // Extract the basic file metadata. This includes things like
    // last modified date and so forth.
    writeFileInfo(file, ctx);

    // Extract information from the header element.
    ctx.fireStartParseEvent("header");
    headerElement.read(ftk, ctx);
    ctx.fireEndParseEvent("header");

    // Extract information from the "information" element.
    ctx.fireStartParseEvent("information");
    infoHeaderElement.read(ftk, ctx);
    ctx.fireEndParseEvent("information");

    // is there a color map? We can check from the "bitcount" attribute
    // found in the "information" element.
    if (ctx.getIntAttribute("BMP.information.bitcount") <= 8) {

      // Using metadata already extracted, we can move to the right place
      // in the file...
      long length = ctx.getIntAttribute("BMP.information.length");
      long colors = ctx.getIntAttribute("BMP.information.colors");
      ftk.setPosition(ftk.getPosition() + (40 - length));

      // Now iterate through each color and write out a color index
      // metadata section.
      for (int i = 0; i < colors; i++) {
        ctx.fireStartParseEvent("color");
        ctx.fireParseEvent("index", i);
        colorTableElement.read(ftk, ctx);
        ctx.fireEndParseEvent("color");
      }
    }

    // We must now close the root <BMP> tag.
    ctx.fireEndParseEvent("BMP");

    // Close the datasource.
    ftk.close();
  }
View Full Code Here

    }
    return false;
  }

  public void process(File file, ParserContext ctx) throws IOException {
    DataSource ftk = new FileDataSource(file);
    FIB pFib = new FIB(Word2LanguageMap.getLanguageMap(),
        Word2Adapter.Offset_To_DOP);

    ctx.fireStartParseEvent("header");
    WordUtils.getHeader(ftk, ctx);
    ctx.fireEndParseEvent("header");

    ctx.fireStartParseEvent("FIB");
    pFib.read(ftk, ctx);
    ctx.fireEndParseEvent("FIB");

    // Check fib is good
    ftk.setPosition(Offset_To_sttbfAssoc);
    int iSttbAssocFP = (int) FXUtil.getNumericalValue(ftk,
        IntegerElement.INT_SIZE, false);

    // Set position in file to the summary strings start
    // Skip first bytes
    ftk.setPosition(iSttbAssocFP + 2);

    String assocFileNext = FXUtil.getPascalStringValue(ftk);
    String assocDot = FXUtil.getPascalStringValue(ftk);
    String assocTitle = FXUtil.getPascalStringValue(ftk);
    String subject = FXUtil.getPascalStringValue(ftk);
    String AssocKeysWords = FXUtil.getPascalStringValue(ftk);
    String Comments = FXUtil.getPascalStringValue(ftk);
    String Author = FXUtil.getPascalStringValue(ftk);
    String LastRevBy = FXUtil.getPascalStringValue(ftk);

    ctx.fireStartParseEvent("summary");
    ctx.fireParseEvent("template", assocDot);
    ctx.fireParseEvent("title", assocTitle);
    ctx.fireParseEvent("subject", subject);
    ctx.fireParseEvent("keywords", AssocKeysWords);
    ctx.fireParseEvent("comments", Comments);
    ctx.fireParseEvent("lastReviewedBy", LastRevBy);
    ctx.fireParseEvent("author", Author);
    ctx.fireEndParseEvent("summary");

    ctx.fireStartParseEvent("Properties");
    long dopPos = ctx.getIntAttribute("Word.FIB.dopOffset");
    WordUtils.getDOPProperties(ftk, (int) dopPos, ctx);
    ctx.fireParseEvent("Application", "Word 2.0");
    ctx.fireEndParseEvent("Properties");
    ftk.close();
  }
View Full Code Here

public class PDFAdapter extends DataAdapter {

  public boolean acceptsFile(File file) {
    boolean pdf = false;
    try {
      DataSource ftk = new FileDataSource(file);
      // Header and default information
      String head = FXUtil.getFixedStringValue(ftk, 4);
      if ((head.toLowerCase().equals("%pdf"))) {
        // version follows - but this adpater will do all!
        pdf = true;
      }
      ftk.close();
    } catch (IOException ex) {
      LogManager.getInstance().logMessage(LogMessage.WORTHLESS_CHATTER,
          "IO Exception determining PDF file type");
    }
    return pdf;
View Full Code Here

  public boolean acceptsFile(File file) {
    boolean gif = false;
   
    try {
      // Read the header bytes to check if this really is a GIF.
      DataSource ftk = new FileDataSource(file);
      // Header and default information
      String head = FXUtil.getFixedStringValue(ftk, 6);
      if ((head.toLowerCase().equals("gif87a"))
          || (head.toLowerCase().equals("gif89a"))) {
        gif = true;
      }
      ftk.close();
    } catch (IOException ex) {
      LogManager.getInstance().logMessage(LogMessage.WORTHLESS_CHATTER,
          "IO Exception determining GIF file type");
    }
    return gif;
View Full Code Here

    return "image/gif";
  }

  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("GIF");
    writeFileInfo(oFile, ctx);
    try {
      gifHeaderElement.read(ftk, ctx);

      String version = ((String) ctx.getAttribute("GIF.version"))
          .toLowerCase();
      // this is where the standards file formats diverge
      if (version.equals("87a")) {
        gifScreenElement87a.read(ftk, ctx);
      } else if (version.equals("89a")) {
        gifScreenElement89a.read(ftk, ctx);
      } else {
        throw new RuntimeException("Unknown GIF Version :" + version);
      }

      // now jump over the global colour map.
      boolean hasGlobalMap = ctx
          .getBooleanAttribute("GIF.PACKED.global-map");
      // System.out.println(hasGlobalMap);
      if (hasGlobalMap) {
        int bitsPerPixel = (int) ctx
            .getIntAttribute("GIF.PACKED.bits-per-pixel");
        readColorTable(ftk, ctx, bitsPerPixel);
      }

      // start reading all the blocks...
      int imgC = 1;
      boolean clean = false;
      while (true) {
        long b = FXUtil.getNumericalValue(ftk.getData(1), false);

        // System.out.println("Block head "+b+"
        // ("+Integer.toHexString((int)b)+")");
        if (b == 0x21) {
          /* Control Block */
          // there are different kinds of control block...
          long sb = FXUtil.getNumericalValue(ftk.getData(1), false);
          if (sb == 0xf9) {
            ctx.fireStartParseEvent("control-block");
            ctx.fireParseEvent("sequence", imgC);
            gifControlElement89a.read(ftk, ctx);
            ctx.fireEndParseEvent("control-block");
          } else if (sb == 0x01) {
            // plain text
            ctx.fireStartParseEvent("text");
            gifPlainTextElement89a.read(ftk, ctx);
            int blocks = readSubBlocks(ftk, ctx, false);
            ctx.fireEndParseEvent("text");

          } else if (sb == 0xfe) {
            // plain text
            ctx.fireStartParseEvent("comment");
            int blocks = readSubBlocks(ftk, ctx, false);
            ctx.fireEndParseEvent("comment");
          } else if (sb == 0xff) {
            ctx.fireStartParseEvent("application");
            gifApplicationElement.read(ftk, ctx);
            int blocks = readSubBlocks(ftk, ctx, true);
            ctx.fireParseEvent("data-blocks", blocks);
            ctx.fireEndParseEvent("application");
          } else {
            // System.out.println("unknown extension block "+sb+" at
            // "+(ftk.getPosition()-1));
            // can we recover and move on... No!
            throw new RuntimeException("unknown extension block "
                + sb + " at " + (ftk.getPosition()));
          }
        } else if (b == 0x3b) {
          /* Terminator */
          clean = true;
          break;
        } else if (b == 0x2c) {
          /* The Image... */
          ctx.fireStartParseEvent("image-info");
          ctx.fireParseEvent("sequence", imgC);
          gifImageElement.read(ftk, ctx);

          // move the pointer along to the next block (skip local
          // color table and image data)
          boolean hasLocalMap = ctx
              .getBooleanAttribute("GIF.image-info.PACKED.has-local-map");
          if (hasLocalMap) {
            int bitsPerPixel = (int) ctx
                .getIntAttribute("GIF.PACKED.image-bits-per-pixel");
            readColorTable(ftk, ctx, bitsPerPixel);
          }

          // ...and the image data itself...
          byte lzwMinCodeSize = ftk.getData(1)[0];
          int blocks = readSubBlocks(ftk, ctx, true);
          ctx.fireParseEvent("data-blocks", blocks);

          // done!
          ctx.fireEndParseEvent("image-info");
          imgC++;
        } else {
          /* Unknown */
          clean = false;
          break;
        }
      }

      ctx.fireParseEvent("frames", imgC - 1);
      ctx.fireParseEvent("clean-termination", clean);
      ctx.fireParseEvent("animated", imgC > 2 ? "true" : "false");

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

  public boolean acceptsFile(File file) {
    boolean jpg = false;
    DataSource ftk = null;
    try {
      // Read the header and see if this appears to be a JPG.
      ftk = new FileDataSource(file);
      // Header and default information
      JpgMarker marker = null;
      marker = readMarker(ftk);
      if (marker.type == 0xd8 && marker.length == 0) {
        jpg = true;
View Full Code Here

    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

  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

TOP

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

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.