You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by sa...@apache.org on 2003/11/12 02:33:30 UTC
cvs commit: jakarta-poi/src/scratchpad/src/org/apache/poi/hwpf HWPFDocument.java Range.java
sackley 2003/11/11 17:33:30
Modified: src/scratchpad/src/org/apache/poi/hwpf HWPFDocument.java
Range.java
Log:
latest changes
Revision Changes Path
1.5 +19 -0 jakarta-poi/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
Index: HWPFDocument.java
===================================================================
RCS file: /home/cvs/jakarta-poi/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- HWPFDocument.java 10 Nov 2003 11:48:26 -0000 1.4
+++ HWPFDocument.java 12 Nov 2003 01:33:30 -0000 1.5
@@ -96,6 +96,8 @@
/** Contains text of the document wrapped in a obfuscated Wod data structure*/
private ComplexFileTable _cft;
+ private TextPieceTable _tpt;
+
/** Contains formatting properties for text*/
private CHPBinTable _cbt;
@@ -151,8 +153,19 @@
// load up our standard structures.
_dop = new DocumentProperties(_tableStream, _fib.getFcDop());
_cft = new ComplexFileTable(_mainStream, _tableStream, _fib.getFcClx(), fcMin);
+ _tpt = _cft.getTextPieceTable();
_cbt = new CHPBinTable(_mainStream, _tableStream, _fib.getFcPlcfbteChpx(), _fib.getLcbPlcfbteChpx(), fcMin);
_pbt = new PAPBinTable(_mainStream, _tableStream, _fib.getFcPlcfbtePapx(), _fib.getLcbPlcfbtePapx(), fcMin);
+
+ // Word XP puts in a zero filled buffer in front of the text and it screws
+ // up my system for offsets. This is an adjustment.
+ int cpMin = _tpt.getCpMin();
+ if (cpMin > 0)
+ {
+ _cbt.adjustForDelete(0, 0, cpMin);
+ _pbt.adjustForDelete(0, 0, cpMin);
+ }
+
_st = new SectionTable(_mainStream, _tableStream, _fib.getFcPlcfsed(), _fib.getLcbPlcfsed(), fcMin);
_ss = new StyleSheet(_tableStream, _fib.getFcStshf());
_ft = new FontTable(_tableStream, _fib.getFcSttbfffn(), _fib.getLcbSttbfffn());
@@ -166,6 +179,10 @@
return _ss;
}
+ public Range getRange()
+ {
+ return new Range(0, _fib.getFcMac() - _fib.getFcMin(), this);
+ }
/**
* Writes out the word file that is represented by an instance of this class.
@@ -310,6 +327,8 @@
try
{
HWPFDocument doc = new HWPFDocument(new FileInputStream(args[0]));
+ Range range = doc.getRange();
+ range.insertBefore("Hello World!!! HAHAHAHAHA I DID IT!!!");
OutputStream out = new FileOutputStream(args[1]);
doc.write(out);
1.3 +48 -12 jakarta-poi/src/scratchpad/src/org/apache/poi/hwpf/Range.java
Index: Range.java
===================================================================
RCS file: /home/cvs/jakarta-poi/src/scratchpad/src/org/apache/poi/hwpf/Range.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Range.java 11 Nov 2003 11:40:52 -0000 1.2
+++ Range.java 12 Nov 2003 01:33:30 -0000 1.3
@@ -134,26 +134,28 @@
public String text()
throws UnsupportedEncodingException
{
- if (!_textRangeFound)
- {
- int[] point = findRange(_text, _textStart, _start, _end);
- _textStart = point[0];
- _textEnd = point[1];
- _textRangeFound = true;
- }
+ initText();
StringBuffer sb = new StringBuffer();
int size = _text.size();
for (int x = 0; x < size; x++)
{
TextPiece tp = (TextPiece)_text.get(x);
- String encoding = "Cp1252";
- if (tp.usesUnicode())
+ StringBuffer pieceSb = (StringBuffer)tp.getCacheContents();
+ if (pieceSb == null)
{
- encoding = "UTF-16LE";
+ String encoding = "Cp1252";
+ if (tp.usesUnicode())
+ {
+ encoding = "UTF-16LE";
+ }
+ String str = new String(tp.getBuf(), encoding);
+ pieceSb = new StringBuffer(str);
+ tp.fillCache(pieceSb);
}
- String str = new String (tp.getBuf(), Math.max(_start, tp.getStart()), Math.min(_end, tp.getEnd()), encoding);
- sb.append(str);
+ int startIndex = Math.max(0, (tp.getStart() - _start));
+ int endIndex = Math.min(tp.getEnd() - startIndex, _end - startIndex);
+ sb.append(pieceSb.toString().substring(startIndex, endIndex));
}
return sb.toString();
}
@@ -177,7 +179,21 @@
}
public CharacterRange insertBefore(String text)
+ throws UnsupportedEncodingException
{
+ initAll();
+
+ TextPiece tp = (TextPiece)_text.get(_textStart);
+ StringBuffer sb = (StringBuffer)tp.getStringBuffer();
+
+ // Since this is the first item in our list, it is safe to assume that
+ // _start >= tp.getStart()
+ int insertIndex = _start - tp.getStart();
+ sb.insert(insertIndex, text);
+ int adjustedLength = _doc.getTextTable().adjustForInsert(_textStart, text.length());
+ _doc.getCharacterTable().adjustForInsert(_textStart, adjustedLength);
+ _doc.getParagraphTable().adjustForInsert(_textStart, adjustedLength);
+ _doc.getSectionTable().adjustForInsert(_textStart, adjustedLength);
return null;
}
@@ -295,6 +311,15 @@
return new CharacterRange(_start, _end, _doc);
}
+ private void initAll()
+ {
+ initText();
+ initCharacterRuns();
+ initParagraphs();
+ initSections();
+ }
+
+
private void initParagraphs()
{
if (!_parRangeFound)
@@ -314,6 +339,17 @@
_charStart = point[0];
_charEnd = point[1];
_charRangeFound = true;
+ }
+ }
+
+ private void initText()
+ {
+ if (!_textRangeFound)
+ {
+ int[] point = findRange(_text, _textStart, _start, _end);
+ _textStart = point[0];
+ _textEnd = point[1];
+ _textRangeFound = true;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org