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.hwpf.model;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: import org.apache.poi.hwpf.usermodel.ParagraphProperties;
023: import org.apache.poi.hwpf.sprm.ParagraphSprmUncompressor;
024: import org.apache.poi.hwpf.sprm.SprmBuffer;
025: import org.apache.poi.hwpf.sprm.SprmOperation;
026:
027: /**
028: * Comment me
029: *
030: * @author Ryan Ackley
031: */
032:
033: public class PAPX extends PropertyNode {
034:
035: private ParagraphHeight _phe;
036: private int _hugeGrpprlOffset = -1;
037:
038: public PAPX(int fcStart, int fcEnd, byte[] papx,
039: ParagraphHeight phe, byte[] dataStream) {
040: super (fcStart, fcEnd, new SprmBuffer(papx));
041: _phe = phe;
042: SprmBuffer buf = findHuge(new SprmBuffer(papx), dataStream);
043: if (buf != null)
044: _buf = buf;
045: }
046:
047: public PAPX(int fcStart, int fcEnd, SprmBuffer buf,
048: byte[] dataStream) {
049: super (fcStart, fcEnd, buf);
050: _phe = new ParagraphHeight();
051: buf = findHuge(buf, dataStream);
052: if (buf != null)
053: _buf = buf;
054: }
055:
056: private SprmBuffer findHuge(SprmBuffer buf, byte[] datastream) {
057: byte[] grpprl = buf.toByteArray();
058: if (grpprl.length == 8 && datastream != null) // then check for sprmPHugePapx
059: {
060: SprmOperation sprm = new SprmOperation(grpprl, 2);
061: if ((sprm.getOperation() == 0x45 || sprm.getOperation() == 0x46)
062: && sprm.getSizeCode() == 3) {
063: int hugeGrpprlOffset = sprm.getOperand();
064: if (hugeGrpprlOffset + 1 < datastream.length) {
065: int grpprlSize = LittleEndian.getShort(datastream,
066: hugeGrpprlOffset);
067: if (hugeGrpprlOffset + grpprlSize < datastream.length) {
068: byte[] hugeGrpprl = new byte[grpprlSize + 2];
069: // copy original istd into huge Grpprl
070: hugeGrpprl[0] = grpprl[0];
071: hugeGrpprl[1] = grpprl[1];
072: // copy Grpprl from dataStream
073: System.arraycopy(datastream,
074: hugeGrpprlOffset + 2, hugeGrpprl, 2,
075: grpprlSize);
076: // save a pointer to where we got the huge Grpprl from
077: _hugeGrpprlOffset = hugeGrpprlOffset;
078: return new SprmBuffer(hugeGrpprl);
079: }
080: }
081: }
082: }
083: return null;
084: }
085:
086: public ParagraphHeight getParagraphHeight() {
087: return _phe;
088: }
089:
090: public byte[] getGrpprl() {
091: return ((SprmBuffer) _buf).toByteArray();
092: }
093:
094: public int getHugeGrpprlOffset() {
095: return _hugeGrpprlOffset;
096: }
097:
098: public short getIstd() {
099: byte[] buf = getGrpprl();
100: if (buf.length == 0) {
101: return 0;
102: } else {
103: return LittleEndian.getShort(buf);
104: }
105: }
106:
107: public SprmBuffer getSprmBuf() {
108: return (SprmBuffer) _buf;
109: }
110:
111: public ParagraphProperties getParagraphProperties(StyleSheet ss) {
112: short istd = getIstd();
113: ParagraphProperties baseStyle = ss.getParagraphStyle(istd);
114: ParagraphProperties props = ParagraphSprmUncompressor
115: .uncompressPAP(baseStyle, getGrpprl(), 2);
116: return props;
117: }
118:
119: public boolean equals(Object o) {
120: if (super .equals(o)) {
121: return _phe.equals(((PAPX) o)._phe);
122: }
123: return false;
124: }
125: }
|