Autoboxing tweaks from bug #51175, and make the paragraph/table finding code generic

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1102691 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-05-13 12:22:24 +00:00
parent 333af414e0
commit f09e137649
9 changed files with 90 additions and 89 deletions

View File

@ -96,16 +96,15 @@ public enum BreakClear {
private static Map<Integer, BreakClear> imap = new HashMap<Integer, BreakClear>(); private static Map<Integer, BreakClear> imap = new HashMap<Integer, BreakClear>();
static { static {
for (BreakClear p : values()) { for (BreakClear p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static BreakClear valueOf(int type) { public static BreakClear valueOf(int type) {
BreakClear bType = imap.get(type); BreakClear bType = imap.get(new Integer(type));
if (bType == null) if (bType == null)
throw new IllegalArgumentException("Unknown break clear type: " throw new IllegalArgumentException("Unknown break clear type: "
+ type); + type);
return bType; return bType;
} }
} }

View File

@ -69,16 +69,15 @@ public enum BreakType {
private static Map<Integer, BreakType> imap = new HashMap<Integer, BreakType>(); private static Map<Integer, BreakType> imap = new HashMap<Integer, BreakType>();
static { static {
for (BreakType p : values()) { for (BreakType p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static BreakType valueOf(int type) { public static BreakType valueOf(int type) {
BreakType bType = imap.get(type); BreakType bType = imap.get(new Integer(type));
if (bType == null) if (bType == null)
throw new IllegalArgumentException("Unknown break type: " throw new IllegalArgumentException("Unknown break type: "
+ type); + type);
return bType; return bType;
} }
} }

View File

@ -62,15 +62,14 @@ public enum LineSpacingRule {
private static Map<Integer, LineSpacingRule> imap = new HashMap<Integer, LineSpacingRule>(); private static Map<Integer, LineSpacingRule> imap = new HashMap<Integer, LineSpacingRule>();
static { static {
for (LineSpacingRule p : values()) { for (LineSpacingRule p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static LineSpacingRule valueOf(int type) { public static LineSpacingRule valueOf(int type) {
LineSpacingRule lineType = imap.get(type); LineSpacingRule lineType = imap.get(new Integer(type));
if (lineType == null) if (lineType == null)
throw new IllegalArgumentException("Unknown line type: " + type); throw new IllegalArgumentException("Unknown line type: " + type);
return lineType; return lineType;
} }
} }

View File

@ -53,12 +53,12 @@ public enum ParagraphAlignment {
private static Map<Integer, ParagraphAlignment> imap = new HashMap<Integer, ParagraphAlignment>(); private static Map<Integer, ParagraphAlignment> imap = new HashMap<Integer, ParagraphAlignment>();
static{ static{
for (ParagraphAlignment p : values()) { for (ParagraphAlignment p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static ParagraphAlignment valueOf(int type){ public static ParagraphAlignment valueOf(int type){
ParagraphAlignment err = imap.get(type); ParagraphAlignment err = imap.get(new Integer(type));
if(err == null) throw new IllegalArgumentException("Unknown paragraph alignment: " + type); if(err == null) throw new IllegalArgumentException("Unknown paragraph alignment: " + type);
return err; return err;
} }

View File

@ -65,14 +65,13 @@ public enum TextAlignment {
private static Map<Integer, TextAlignment> imap = new HashMap<Integer, TextAlignment>(); private static Map<Integer, TextAlignment> imap = new HashMap<Integer, TextAlignment>();
static{ static{
for (TextAlignment p : values()) { for (TextAlignment p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static TextAlignment valueOf(int type){ public static TextAlignment valueOf(int type){
TextAlignment align = imap.get(type); TextAlignment align = imap.get(new Integer(type));
if(align == null) throw new IllegalArgumentException("Unknown text alignment: " + type); if(align == null) throw new IllegalArgumentException("Unknown text alignment: " + type);
return align; return align;
} }
} }

View File

@ -148,16 +148,15 @@ public enum UnderlinePatterns {
private static Map<Integer, UnderlinePatterns> imap = new HashMap<Integer, UnderlinePatterns>(); private static Map<Integer, UnderlinePatterns> imap = new HashMap<Integer, UnderlinePatterns>();
static { static {
for (UnderlinePatterns p : values()) { for (UnderlinePatterns p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static UnderlinePatterns valueOf(int type) { public static UnderlinePatterns valueOf(int type) {
UnderlinePatterns align = imap.get(type); UnderlinePatterns align = imap.get(new Integer(type));
if (align == null) if (align == null)
throw new IllegalArgumentException("Unknown underline pattern: " throw new IllegalArgumentException("Unknown underline pattern: "
+ type); + type);
return align; return align;
} }
} }

View File

@ -60,16 +60,15 @@ public enum VerticalAlign {
private static Map<Integer, VerticalAlign> imap = new HashMap<Integer, VerticalAlign>(); private static Map<Integer, VerticalAlign> imap = new HashMap<Integer, VerticalAlign>();
static { static {
for (VerticalAlign p : values()) { for (VerticalAlign p : values()) {
imap.put(p.getValue(), p); imap.put(new Integer(p.getValue()), p);
} }
} }
public static VerticalAlign valueOf(int type) { public static VerticalAlign valueOf(int type) {
VerticalAlign align = imap.get(type); VerticalAlign align = imap.get(new Integer(type));
if (align == null) if (align == null)
throw new IllegalArgumentException("Unknown vertical alignment: " throw new IllegalArgumentException("Unknown vertical alignment: "
+ type); + type);
return align; return align;
} }
} }

View File

@ -584,34 +584,38 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
} }
/** private int getPosOfBodyElement(IBodyElement needle) {
* get position of the paragraph BodyElementType type = needle.getElementType();
* @param p IBodyElement current;
*/ for(int i=0; i<bodyElements.size(); i++) {
public Integer getPosOfParagraph(XWPFParagraph p){ current = bodyElements.get(i);
int i, pos = 0; if(current.getElementType() == type) {
for (i = 0 ; i < bodyElements.size() ; i++) { if(current.equals(needle)) {
if (bodyElements.get(i) instanceof XWPFParagraph){ return i;
if (bodyElements.get(i).equals(p)){
return pos;
}
pos++;
} }
} }
return null; }
return -1;
} }
public Integer getPosOfTable(XWPFTable t){ /**
int i, pos = 0; * Get the position of the paragraph, within the list
for(i = 0; i < bodyElements.size(); i++){ * of all the body elements.
if(bodyElements.get(i).getElementType() == BodyElementType.TABLE){ * @param p The paragraph to find
if (bodyElements.get(i) == t){ * @return The location, or -1 if the paragraph couldn't be found
return pos; */
public int getPosOfParagraph(XWPFParagraph p){
return getPosOfBodyElement(p);
} }
pos++;
} /**
} * Get the position of the table, within the list of
return null; * all the body elements.
* @param t The table to find
* @return The location, or -1 if the table couldn't be found
*/
public int getPosOfTable(XWPFTable t){
return getPosOfBodyElement(t);
} }
/** /**
@ -728,7 +732,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
String parStyle = par.getStyle(); String parStyle = par.getStyle();
if (parStyle != null && parStyle.substring(0, 7).equals("Heading")) { if (parStyle != null && parStyle.substring(0, 7).equals("Heading")) {
try { try {
int level = Integer.valueOf(parStyle.substring("Heading".length())); int level = Integer.valueOf(parStyle.substring("Heading".length())).intValue();
toc.addRow(level, par.getText(), 1, "112723803"); toc.addRow(level, par.getText(), 1, "112723803");
} }
catch (NumberFormatException e) { catch (NumberFormatException e) {

View File

@ -106,6 +106,9 @@ public final class TestXWPFDocument extends TestCase {
assertEquals(p, doc.getParagraphs().get(3)); assertEquals(p, doc.getParagraphs().get(3));
assertEquals(4, doc.getParagraphs().size()); assertEquals(4, doc.getParagraphs().size());
assertEquals(3, doc.getParagraphPos(3));
assertEquals(3, doc.getPosOfParagraph(p));
CTP ctp = p.getCTP(); CTP ctp = p.getCTP();
XWPFParagraph newP = doc.getParagraph(ctp); XWPFParagraph newP = doc.getParagraph(ctp);
assertSame(p, newP); assertSame(p, newP);