001/*****************************************************************************
002 * Copyright by The HDF Group.                                               *
003 * Copyright by the Board of Trustees of the University of Illinois.         *
004 * All rights reserved.                                                      *
005 *                                                                           *
006 * This file is part of the HDF Java Products distribution.                  *
007 * The full copyright notice, including terms governing use, modification,   *
008 * and redistribution, is contained in the files COPYING and Copyright.html. *
009 * COPYING can be found at the root of the source code distribution tree.    *
010 * Or, see https://support.hdfgroup.org/products/licenses.html               *
011 * If you do not have access to either file, you may request a copy from     *
012 * help@hdfgroup.org.                                                        *
013 ****************************************************************************/
014
015package hdf.object.h5;
016
017import java.util.List;
018
019import hdf.hdf5lib.H5;
020import hdf.hdf5lib.HDF5Constants;
021import hdf.hdf5lib.HDFNativeData;
022import hdf.hdf5lib.exceptions.HDF5Exception;
023import hdf.hdf5lib.structs.H5O_info_t;
024
025import hdf.object.Attribute;
026import hdf.object.FileFormat;
027import hdf.object.HObject;
028import hdf.object.MetaDataContainer;
029
030/**
031 * An H5Link object represents an existing HDF5 object in file.
032 *
033 * H5Link object is an HDF5 object that is either a soft or an external link to
034 * an object in a file that does not exist. The type of the object is unknown.
035 * Once the object being linked to is created, and the type is known, then
036 * H5link object will change its type.
037 *
038 * @version 2.7.2 7/6/2010
039 * @author Nidhi Gupta
040 */
041
042public class H5Link extends HObject implements MetaDataContainer
043{
044    private static final long serialVersionUID = -8137277460521594367L;
045
046    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(H5Link.class);
047
048    /** the object properties */
049    private H5O_info_t objInfo;
050
051    /**
052     * Constructs an HDF5 link with specific name, path, and parent.
053     *
054     * @param theFile
055     *            the file which containing the link.
056     * @param theName
057     *            the name of this link, e.g. "link1".
058     * @param thePath
059     *            the full path of this link, e.g. "/groups/".
060     */
061    public H5Link(FileFormat theFile, String theName, String thePath) {
062        this(theFile, theName, thePath, null);
063    }
064
065    /**
066     * Constructs an HDF5 link with specific name, path, parent and oid.
067     *
068     * @param theFile
069     *            the file which containing the link.
070     * @param theName
071     *            the name of this link, e.g. "link1".
072     * @param thePath
073     *            the full path of this link, e.g. "/groups/".
074     * @param oid
075     *            the oid of this link, e.g. "/groups/".
076     */
077    @SuppressWarnings("deprecation")
078    public H5Link(FileFormat theFile, String theName, String thePath, long[] oid) {
079        super(theFile, theName, thePath, oid);
080
081        objInfo = new H5O_info_t(-1L, -1L, -1, 0, -1L, 0L, 0L, 0L, 0L, null, null, null);
082
083        if (theFile != null) {
084            if (oid == null) {
085                // retrieve the object ID
086                try {
087                    byte[] refBuf = H5.H5Rcreate(theFile.getFID(), this.getFullName(), HDF5Constants.H5R_OBJECT, -1);
088                    this.oid = new long[1];
089                    this.oid[0] = HDFNativeData.byteToLong(refBuf, 0);
090                }
091                catch (Exception ex) {
092                    log.debug("constructor ID {} for {} failed H5Rcreate", theFile.getFID(), this.getFullName());
093                }
094            }
095        }
096        else
097            this.oid = null;
098    }
099
100    /*
101     * (non-Javadoc)
102     *
103     * @see hdf.object.HObject#open()
104     */
105    @Override
106    public long open() {
107        return 0;
108    }
109
110    /*
111     * (non-Javadoc)
112     *
113     * @see hdf.object.HObject#close(int)
114     */
115    @Override
116    public void close(long id) {
117    }
118
119    /**
120     * Check if the object has any attributes attached.
121     *
122     * @return true if it has any attributes, false otherwise.
123     */
124    @Override
125    public boolean hasAttribute() {
126        return false;
127    }
128
129    /**
130     * Removes all of the elements from metadata list.
131     * The list should be empty after this call returns.
132     */
133    @Override
134    public void clear() {
135    }
136
137    /**
138     * Retrieves the object's metadata, such as attributes, from the file.
139     *
140     * Metadata, such as attributes, is stored in a List.
141     *
142     * @return the list of metadata objects.
143     *
144     * @throws HDF5Exception
145     *             if the metadata can not be retrieved
146     */
147    @Override
148    public List<Attribute> getMetadata() throws HDF5Exception {
149        try {
150            this.linkTargetObjName = H5File.getLinkTargetName(this);
151        }
152        catch (Exception ex) {
153            log.debug("getMetadata(): getLinkTargetName failed: ", ex);
154        }
155
156        return null;
157    }
158
159    /**
160     * Retrieves the object's metadata, such as attributes, from the file.
161     *
162     * Metadata, such as attributes, is stored in a List.
163     *
164     * @param attrPropList
165     *             the list of properties to get
166     *
167     * @return the list of metadata objects.
168     *
169     * @throws HDF5Exception
170     *             if the metadata can not be retrieved
171     */
172    public List<Attribute> getMetadata(int... attrPropList) throws HDF5Exception {
173        try {
174            this.linkTargetObjName = H5File.getLinkTargetName(this);
175        }
176        catch (Exception ex) {
177            log.debug("getMetadata(): getLinkTargetName failed: ", ex);
178        }
179
180        return null;
181    }
182
183    /**
184     * Writes a specific piece of metadata (such as an attribute) into the file.
185     *
186     * If an HDF(4&amp;5) attribute exists in the file, this method updates its
187     * value. If the attribute does not exist in the file, it creates the
188     * attribute in the file and attaches it to the object. It will fail to
189     * write a new attribute to the object where an attribute with the same name
190     * already exists. To update the value of an existing attribute in the file,
191     * one needs to get the instance of the attribute by getMetadata(), change
192     * its values, then use writeMetadata() to write the value.
193     *
194     * @param info
195     *            the metadata to write.
196     *
197     * @throws Exception
198     *             if the metadata can not be written
199     */
200    @Override
201    public void writeMetadata(Object info) throws Exception {
202    }
203
204    /**
205     * Deletes an existing piece of metadata from this object.
206     *
207     * @param info
208     *            the metadata to delete.
209     *
210     * @throws HDF5Exception
211     *             if the metadata can not be removed
212     */
213    @Override
214    public void removeMetadata(Object info) throws HDF5Exception {
215    }
216
217    /**
218     * Updates an existing piece of metadata attached to this object.
219     *
220     * @param info
221     *            the metadata to update.
222     *
223     * @throws HDF5Exception
224     *             if the metadata can not be updated
225     */
226    @Override
227    public void updateMetadata(Object info) throws HDF5Exception {
228    }
229
230    /*
231     * (non-Javadoc)
232     *
233     * @see hdf.object.HObject#setName(java.lang.String)
234     */
235    @Override
236    public void setName(String newName) throws Exception {
237        if (newName == null)
238            throw new IllegalArgumentException("The new name is NULL");
239
240        H5File.renameObject(this, newName);
241        super.setName(newName);
242    }
243}