All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ee.sk.digidoc.Manifest Maven / Gradle / Ivy

/*
 * Manifest
 * PROJECT: JDigiDoc
 * DESCRIPTION: Contents of manifest.xml file. 
 * AUTHOR:  Veiko Sinivee, Sunset Software OÜ
 *==================================================
 * Copyright (C) AS Sertifitseerimiskeskus
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * GNU Lesser General Public Licence is available at
 * http://www.gnu.org/copyleft/lesser.html
 *==================================================
 */
package ee.sk.digidoc;
import java.io.Serializable;
import ee.sk.utils.ConvertUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Models contents of a BDOC format manifest.xml file  
 * @author  Veiko Sinivee
 * @version 1.0
 */
public class Manifest implements Serializable
{
	private static final long serialVersionUID = 1L;
	/** manifest urn */
	private static final String MANIFEST_URN = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0";
	public static final String MANIFEST_BDOC_MIME_1_0 = "application/vnd.bdoc-1.0";
	public static final String MANIFEST_BDOC_MIME_1_1 = "application/vnd.bdoc-1.1";
	public static final String MANIFEST_BDOC_MIME_2_0 = "application/vnd.etsi.asic-e+zip";
	/** file entries */
	private ArrayList m_fileEntries;
	
	/**
	 * Default constructor for Manifest
	 */
	public Manifest()
	{
		m_fileEntries = null;
	}
	
	// accessors
	/**
	 * Retrieves number of  elements
	 * @return number of  elements
	 */
	public int getNumFileEntries() { return ((m_fileEntries != null) ? m_fileEntries.size() : 0); }
	
	/**
	 * Retrieves the desired  element
	 * @param nIdx index of entry
	 * @return desired  element or null if not existent
	 */
	public ManifestFileEntry getFileEntry(int nIdx) {
		if(nIdx >= 0 && m_fileEntries != null && nIdx < m_fileEntries.size())
			return (ManifestFileEntry)m_fileEntries.get(nIdx);
		else
			return null;
	}
	
	// mutators
	
	/**
	 * Adds a new 
	 * @param fe  element to add
	 */
	public void addFileEntry(ManifestFileEntry fe) {
		if(m_fileEntries == null)
			m_fileEntries = new ArrayList();
		m_fileEntries.add(fe);
	}
	
	/**
	 * Removes a 
	 * @param nIdx index of entry
	 */
	public void removeFileEntry(int nIdx) {
		if(nIdx >= 0 && m_fileEntries != null && nIdx < m_fileEntries.size())
			m_fileEntries.remove(nIdx);
	}
	
	/**
	 * Removes a 
	 * @param fullPath full-path of entry
	 */
	public void removeFileEntryWithPath(String fullPath) {
		for(int i = 0; (m_fileEntries != null) && (i < m_fileEntries.size()); i++) {
			ManifestFileEntry fe = (ManifestFileEntry)m_fileEntries.get(i);
			if(fe.getFullPath().equals(fullPath))
				m_fileEntries.remove(i);
		}
	}
	
	/**
	 * Finds a file-entry by path
	 * @param fullPath full-path of entry
	 * @return file-entry if found
	 */
	public ManifestFileEntry findFileEntryByPath(String fullPath) {
		for(int i = 0; (m_fileEntries != null) && (i < m_fileEntries.size()); i++) {
			ManifestFileEntry fe = (ManifestFileEntry)m_fileEntries.get(i);
			if(fe.getFullPath().equals(fullPath))
				return fe;
		}
		return null;
	}
	
	/**
     * Converts the Manifest to XML form
     * @return XML representation of Manifest 
     */
    public byte[] toXML()
        throws DigiDocException
    {
        ByteArrayOutputStream bos = 
                new ByteArrayOutputStream();
        try {
            bos.write(ConvertUtils.str2data("\n"));
            //bos.write(ConvertUtils.str2data("\n"));
            bos.write(ConvertUtils.str2data("\n"));
            for(int i = 0; (m_fileEntries != null) && (i < m_fileEntries.size()); i++) {
            	ManifestFileEntry fe = (ManifestFileEntry)m_fileEntries.get(i);
            	bos.write(fe.toXML());
            }
            bos.write(ConvertUtils.str2data("\n"));
         } catch(IOException ex) {
            DigiDocException.handleException(ex, DigiDocException.ERR_XML_CONVERT);
        }
        return bos.toByteArray();
    }

    /**
     * return the stringified form of Manifest
     * @return Manifest string representation
     */
    public String toString() {
        String str = null;
        try {
            str = new String(toXML());
        } catch(Exception ex) {}
        return str;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy