001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.ddf;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: /**
027: * Generates a property given a reference into the byte array storing that property.
028: *
029: * @author Glen Stampoultzis
030: */
031: public class EscherPropertyFactory {
032: /**
033: * Create new properties from a byte array.
034: *
035: * @param data The byte array containing the property
036: * @param offset The starting offset into the byte array
037: * @return The new properties
038: */
039: public List createProperties(byte[] data, int offset,
040: short numProperties) {
041: List results = new ArrayList();
042:
043: int pos = offset;
044:
045: // while ( bytesRemaining >= 6 )
046: for (int i = 0; i < numProperties; i++) {
047: short propId;
048: int propData;
049: propId = LittleEndian.getShort(data, pos);
050: propData = LittleEndian.getInt(data, pos + 2);
051: short propNumber = (short) (propId & (short) 0x3FFF);
052: boolean isComplex = (propId & (short) 0x8000) != 0;
053: boolean isBlipId = (propId & (short) 0x4000) != 0;
054:
055: byte propertyType = EscherProperties
056: .getPropertyType((short) propNumber);
057: if (propertyType == EscherPropertyMetaData.TYPE_BOOLEAN)
058: results.add(new EscherBoolProperty(propId, propData));
059: else if (propertyType == EscherPropertyMetaData.TYPE_RGB)
060: results.add(new EscherRGBProperty(propId, propData));
061: else if (propertyType == EscherPropertyMetaData.TYPE_SHAPEPATH)
062: results.add(new EscherShapePathProperty(propId,
063: propData));
064: else {
065: if (!isComplex)
066: results.add(new EscherSimpleProperty(propId,
067: propData));
068: else {
069: if (propertyType == EscherPropertyMetaData.TYPE_ARRAY)
070: results.add(new EscherArrayProperty(propId,
071: new byte[propData]));
072: else
073: results.add(new EscherComplexProperty(propId,
074: new byte[propData]));
075:
076: }
077: }
078: pos += 6;
079: // bytesRemaining -= 6 + complexBytes;
080: }
081:
082: // Get complex data
083: for (Iterator iterator = results.iterator(); iterator.hasNext();) {
084: EscherProperty p = (EscherProperty) iterator.next();
085: if (p instanceof EscherComplexProperty) {
086: if (p instanceof EscherArrayProperty) {
087: pos += ((EscherArrayProperty) p).setArrayData(data,
088: pos);
089: } else {
090: byte[] complexData = ((EscherComplexProperty) p)
091: .getComplexData();
092: System.arraycopy(data, pos, complexData, 0,
093: complexData.length);
094: pos += complexData.length;
095: }
096: }
097: }
098:
099: return results;
100: }
101:
102: }
|