@Override
  public ISecurityHandler getSecurityHandler(COSEncryption encryption) throws COSSecurityException {
    COSName name = encryption.getFilter();
    if (name == null) {
      throw new COSSecurityException("security handler not specified"); // $NON-NLS-1$
    }
    if (name.equals(CN_Standard)) {
      int revision = encryption.getFieldInt(DK_R, 0);
      if (revision == 2) {
        return new StandardSecurityHandlerR2();
      } else if (revision == 3) {
        return new StandardSecurityHandlerR3();
      } else if (revision == 4) {
        return new StandardSecurityHandlerR4();
      } else {
        return new StandardSecurityHandlerR2();
      }
    }

    // maybe provide a registry some day
    throw new COSSecurityException(
        "no security handler '" //$NON-NLS-1$
            + name.stringValue()
            + "'"); //$NON-NLS-1$
  }
 @Override
 public void detach(STDocument doc) throws COSSecurityException {
   if (doc == null) {
     return;
   }
   COSEncryption encryption = getEncryption();
   encryption.cosRemoveField(COSEncryption.DK_Filter);
   encryption.cosRemoveField(StandardSecurityHandler.DK_R);
   encryption.cosRemoveField(StandardSecurityHandler.DK_P);
   super.detach(doc);
 }
 @Override
 public void attach(STDocument doc) {
   super.attach(doc);
   if (doc == null) {
     return;
   }
   COSEncryption encryption = getEncryption();
   encryption.cosSetField(COSEncryption.DK_Filter, StandardSecurityHandlerFactory.CN_Standard);
   encryption.cosSetField(StandardSecurityHandler.DK_R, COSInteger.create(getRevision()));
   // apply default permissions
   getEncryption().setFieldInt(StandardSecurityHandler.DK_P, DEFAULT_ACCESS_PERMISSIONS);
 }
 @Override
 protected void initializeFromScratch() {
   super.initializeFromScratch();
   COSEncryption encryption = getEncryption();
   encryption.setFieldInt(COSEncryption.DK_Length, DEFAULT_LENGTH);
 }
 public void setLength(int length) {
   COSEncryption encryption = getEncryption();
   encryption.setFieldInt(COSEncryption.DK_Length, length);
 }
 protected void initializeFromScratch() {
   COSEncryption encryption = getEncryption();
   encryption.cosSetField(COSEncryption.DK_V, COSInteger.create(getVersion()));
 }
 public int getLength() {
   COSEncryption encryption = getEncryption();
   return encryption.getFieldInt(COSEncryption.DK_Length, DEFAULT_LENGTH);
 }