?? mutablesection.java
字號:
/* ==================================================================== Copyright 2002-2004 Apache Software Foundation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */ package org.apache.poi.hpsf;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.ListIterator;import java.util.Map;import org.apache.poi.hpsf.wellknown.PropertyIDMap;import org.apache.poi.util.LittleEndian;/** * <p>Adds writing capability to the {@link Section} class.</p> * * <p>Please be aware that this class' functionality will be merged into the * {@link Section} class at a later time, so the API will change.</p> * * @version $Id: MutableSection.java,v 1.14 2005/04/01 16:58:00 klute Exp $ * @since 2002-02-20 */public class MutableSection extends Section{ /** * <p>If the "dirty" flag is true, the section's size must be * (re-)calculated before the section is written.</p> */ private boolean dirty = true; /** * <p>List to assemble the properties. Unfortunately a wrong * decision has been taken when specifying the "properties" field * as an Property[]. It should have been a {@link java.util.List}.</p> */ private List preprops; /** * <p>Contains the bytes making out the section. This byte array is * established when the section's size is calculated and can be reused * later. It is valid only if the "dirty" flag is false.</p> */ private byte[] sectionBytes; /** * <p>Creates an empty mutable section.</p> */ public MutableSection() { dirty = true; formatID = null; offset = -1; preprops = new LinkedList(); } /** * <p>Constructs a <code>MutableSection</code> by doing a deep copy of an * existing <code>Section</code>. All nested <code>Property</code> * instances, will be their mutable counterparts in the new * <code>MutableSection</code>.</p> * * @param s The section set to copy */ public MutableSection(final Section s) { setFormatID(s.getFormatID()); final Property[] pa = s.getProperties(); final MutableProperty[] mpa = new MutableProperty[pa.length]; for (int i = 0; i < pa.length; i++) mpa[i] = new MutableProperty(pa[i]); setProperties(mpa); setDictionary(s.getDictionary()); } /** * <p>Sets the section's format ID.</p> * * @param formatID The section's format ID * * @see #setFormatID(byte[]) * @see #getFormatID */ public void setFormatID(final ClassID formatID) { this.formatID = formatID; } /** * <p>Sets the section's format ID.</p> * * @param formatID The section's format ID as a byte array. It components * are in big-endian format. * * @see #setFormatID(ClassID) * @see #getFormatID */ public void setFormatID(final byte[] formatID) { ClassID fid = getFormatID(); if (fid == null) { fid = new ClassID(); setFormatID(fid); } fid.setBytes(formatID); } /** * <p>Sets this section's properties. Any former values are overwritten.</p> * * @param properties This section's new properties. */ public void setProperties(final Property[] properties) { this.properties = properties; preprops = new LinkedList(); for (int i = 0; i < properties.length; i++) preprops.add(properties[i]); dirty = true; } /** * <p>Sets the value of the property with the specified ID. If a * property with this ID is not yet present in the section, it * will be added. An already present property with the specified * ID will be overwritten.</p> * * @param id The property's ID * @param value The property's value. It will be written as a Unicode * string. * * @see #setProperty(int, long, Object) * @see #getProperty */ public void setProperty(final int id, final String value) { setProperty(id, Variant.VT_LPWSTR, value); dirty = true; } /** * <p>Sets the value and the variant type of the property with the * specified ID. If a property with this ID is not yet present in * the section, it will be added. An already present property with * the specified ID will be overwritten. A default mapping will be * used to choose the property's type.</p> * * @param id The property's ID. * @param variantType The property's variant type. * @param value The property's value. * * @see #setProperty(int, String) * @see #getProperty * @see Variant */ public void setProperty(final int id, final long variantType, final Object value) { final MutableProperty p = new MutableProperty(); p.setID(id); p.setType(variantType); p.setValue(value); setProperty(p); dirty = true; } /** * <p>Sets a property. If a property with the same ID is not yet present in * the section, the property will be added to the section. If there is * already a property with the same ID present in the section, it will be * overwritten.</p> * * @param p The property to be added to the section * * @see #setProperty(int, long, Object) * @see #setProperty(int, String) * @see #getProperty * @see Variant */ public void setProperty(final Property p) { final long id = p.getID(); removeProperty(id); preprops.add(p); dirty = true; } /** * <p>Removes a property.</p> * * @param id The ID of the property to be removed */ public void removeProperty(final long id) { for (final Iterator i = preprops.iterator(); i.hasNext();) if (((Property) i.next()).getID() == id) { i.remove(); break; } dirty = true; } /** * <p>Sets the value of the boolean property with the specified * ID.</p> * * @param id The property's ID * @param value The property's value * * @see #setProperty(int, long, Object) * @see #getProperty * @see Variant */ protected void setPropertyBooleanValue(final int id, final boolean value) { setProperty(id, (long) Variant.VT_BOOL, new Boolean(value)); } /** * <p>Returns the section's size.</p> * * @return the section's size. */ public int getSize() { if (dirty) { try { size = calcSize(); dirty = false; } catch (HPSFRuntimeException ex) { throw ex; } catch (Exception ex) { throw new HPSFRuntimeException(ex); } } return size; } /** * <p>Calculates the section's size. It is the sum of the lengths of the * section's header (8), the properties list (16 times the number of * properties) and the properties themselves.</p> * * @return the section's length in bytes. */ private int calcSize() throws WritingNotSupportedException, IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out); out.close(); /* Pad to multiple of 4 bytes so that even the Windows shell (explorer) * shows custom properties. */ sectionBytes = Util.pad4(out.toByteArray());
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -