diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-07-17 08:45:38 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-07-17 08:45:38 +0000 |
commit | 50c40b61f72903693f5304767fdd337d7e685f50 (patch) | |
tree | 8836fa01b52b94599237af67595ea15cf426c57c /poi-scratchpad | |
parent | cd02f739eb2c2fe6978ea0672396c20c748f73e4 (diff) | |
download | poi-50c40b61f72903693f5304767fdd337d7e685f50.tar.gz poi-50c40b61f72903693f5304767fdd337d7e685f50.zip |
[github-354] Add extra methods to StringUtil and refactor some code to use them. Thanks to XenoAmess. This closes #354
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902801 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-scratchpad')
7 files changed, 53 insertions, 48 deletions
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Chunk.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Chunk.java index 5073771fc3..43d2fb65ea 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Chunk.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Chunk.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.util.Locale; import org.apache.poi.hsmf.datatypes.Types.MAPIType; +import org.apache.poi.util.StringUtil; public abstract class Chunk { public static final String DEFAULT_NAME_PREFIX = "__substg1.0_"; @@ -62,13 +63,14 @@ public abstract class Chunk { public String getEntryName() { String type = this.type.asFileEnding(); - String chunkId = Integer.toHexString(this.chunkId); - while (chunkId.length() < 4) { - chunkId = "0" + chunkId; + StringBuilder chunkId = new StringBuilder(Integer.toHexString(this.chunkId)); + int need0count = 4 - chunkId.length(); + if (need0count > 0) { + chunkId.insert(0, StringUtil.repeat('0', need0count)); } return this.namePrefix - + chunkId.toUpperCase(Locale.ROOT) + + chunkId.toString().toUpperCase(Locale.ROOT) + type.toUpperCase(Locale.ROOT); } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/MAPIProperty.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/MAPIProperty.java index 81c431b2b7..b09e39cc7a 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/MAPIProperty.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/MAPIProperty.java @@ -17,6 +17,15 @@ package org.apache.poi.hsmf.datatypes; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import org.apache.poi.hsmf.datatypes.Types.MAPIType; +import org.apache.poi.util.StringUtil; + import static org.apache.poi.hsmf.datatypes.Types.ASCII_STRING; import static org.apache.poi.hsmf.datatypes.Types.BINARY; import static org.apache.poi.hsmf.datatypes.Types.BOOLEAN; @@ -27,14 +36,6 @@ import static org.apache.poi.hsmf.datatypes.Types.LONG_LONG; import static org.apache.poi.hsmf.datatypes.Types.SHORT; import static org.apache.poi.hsmf.datatypes.Types.TIME; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import org.apache.poi.hsmf.datatypes.Types.MAPIType; - /** * Holds the list of MAPI Attributes, and allows lookup by friendly name, ID and * MAPI Property Name. @@ -1069,9 +1070,10 @@ public class MAPIProperty { } public String asFileName() { - String str = Integer.toHexString(id).toUpperCase(Locale.ROOT); - while (str.length() < 4) { - str = "0" + str; + StringBuilder str = new StringBuilder(Integer.toHexString(id).toUpperCase(Locale.ROOT)); + int need0count = 4 - str.length(); + if (need0count > 0) { + str.insert(0, StringUtil.repeat('0', need0count)); } return str + usualType.asFileEnding(); } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/PropertiesChunk.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/PropertiesChunk.java index de28c70bbb..5d5cde36ec 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/PropertiesChunk.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/PropertiesChunk.java @@ -17,8 +17,6 @@ package org.apache.poi.hsmf.datatypes; -import static org.apache.logging.log4j.util.Unbox.box; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -48,6 +46,9 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian.BufferUnderrunException; +import org.apache.poi.util.StringUtil; + +import static org.apache.logging.log4j.util.Unbox.box; /** * <p> @@ -419,9 +420,10 @@ public abstract class PropertiesChunk extends Chunk { } private String getFileName(MAPIProperty property, MAPIType actualType) { - String str = Integer.toHexString(property.id).toUpperCase(Locale.ROOT); - while (str.length() < 4) { - str = "0" + str; + StringBuilder str = new StringBuilder(Integer.toHexString(property.id).toUpperCase(Locale.ROOT)); + int need0count = 4 - str.length(); + if (need0count > 0) { + str.insert(0, StringUtil.repeat('0', need0count)); } MAPIType type = getTypeMapping(actualType); return str + type.asFileEnding(); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Types.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Types.java index 4c92da4f0f..d11f3489da 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Types.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/datatypes/Types.java @@ -21,6 +21,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import org.apache.poi.util.StringUtil; + /** * The types list and details are available from * http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.contenttypes.tnef.tnefpropertytype%28v=EXCHG.140%29.aspx @@ -148,11 +150,12 @@ public final class Types { } public static String asFileEnding(int type) { - String str = Integer.toHexString(type).toUpperCase(Locale.ROOT); - while (str.length() < 4) { - str = "0" + str; + StringBuilder str = new StringBuilder(Integer.toHexString(type).toUpperCase(Locale.ROOT)); + int need0count = 4 - str.length(); + if (need0count > 0) { + str.insert(0, StringUtil.repeat('0', need0count)); } - return str; + return str.toString(); } public static String asName(int typeId) { diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/dev/TypesLister.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/dev/TypesLister.java index 6cb05b30fb..d29ffc2b94 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/dev/TypesLister.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/dev/TypesLister.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Comparator; import org.apache.poi.hsmf.datatypes.MAPIProperty; +import org.apache.poi.util.StringUtil; /** * Lists the different MAPI types @@ -49,8 +50,11 @@ public class TypesLister { } private void list(ArrayList<MAPIProperty> list, PrintStream out) { for(MAPIProperty attr : list) { - String id = Integer.toHexString(attr.id); - while(id.length() < 4) { id = "0"+id; } + StringBuilder id = new StringBuilder(Integer.toHexString(attr.id)); + int need0count = 4 - id.length(); + if (need0count > 0) { + id.insert(0, StringUtil.repeat('0', need0count)); + } int typeId = attr.usualType.getId(); String typeIdStr = Integer.toString(typeId); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java index 484d367788..6bda01ee31 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java @@ -29,7 +29,7 @@ public class WordToHtmlUtils extends AbstractWordUtils { public static void addBold( final boolean bold, StringBuilder style ) { - style.append( "font-weight:" + ( bold ? "bold" : "normal" ) + ";" ); + style.append("font-weight:").append(bold ? "bold" : "normal").append(";"); } public static void addBorder( BorderCode borderCode, String where, @@ -71,13 +71,11 @@ public class WordToHtmlUtils extends AbstractWordUtils } if ( characterRun.getIco24() != -1 ) { - style.append( "color:" + getColor24( characterRun.getIco24() ) - + ";" ); + style.append("color:").append(getColor24(characterRun.getIco24())).append(";"); } if ( characterRun.isHighlighted() ) { - style.append( "background-color:" - + getColor( characterRun.getHighlightedColor() ) + ";" ); + style.append("background-color:").append(getColor(characterRun.getHighlightedColor())).append(";"); } if ( characterRun.isStrikeThrough() ) { @@ -85,8 +83,7 @@ public class WordToHtmlUtils extends AbstractWordUtils } if ( characterRun.isShadowed() ) { - style.append( "text-shadow:" + characterRun.getFontSize() / 24 - + "pt;" ); + style.append("text-shadow:").append(characterRun.getFontSize() / 24).append("pt;"); } if ( characterRun.isSmallCaps() ) { @@ -118,12 +115,12 @@ public class WordToHtmlUtils extends AbstractWordUtils if ( isEmpty( fontFamily ) ) return; - style.append( "font-family:" + fontFamily + ";" ); + style.append("font-family:").append(fontFamily).append(";"); } public static void addFontSize( final int fontSize, StringBuilder style ) { - style.append( "font-size:" + fontSize + "pt;" ); + style.append("font-size:").append(fontSize).append("pt;"); } public static void addIndent( Paragraph paragraph, StringBuilder style ) @@ -143,7 +140,7 @@ public class WordToHtmlUtils extends AbstractWordUtils if ( twipsValue == 0 ) return; - style.append( cssName + ":" + ( twipsValue / TWIPS_PER_INCH ) + "in;" ); + style.append(cssName).append(":").append(twipsValue / TWIPS_PER_INCH).append("in;"); } public static void addJustification( Paragraph paragraph, @@ -151,7 +148,7 @@ public class WordToHtmlUtils extends AbstractWordUtils { String justification = getJustification( paragraph.getJustification() ); if ( isNotEmpty( justification ) ) - style.append( "text-align:" + justification + ";" ); + style.append("text-align:").append(justification).append(";"); } public static void addParagraphProperties( Paragraph paragraph, @@ -170,8 +167,7 @@ public class WordToHtmlUtils extends AbstractWordUtils style.append( "break-before:page;" ); } - style.append( "hyphenate:" - + ( paragraph.isAutoHyphenated() ? "auto" : "none" ) + ";" ); + style.append("hyphenate:").append(paragraph.isAutoHyphenated() ? "auto" : "none").append(";"); if ( paragraph.keepOnPage() ) { @@ -188,12 +184,9 @@ public class WordToHtmlUtils extends AbstractWordUtils TableCell tableCell, boolean toppest, boolean bottomest, boolean leftest, boolean rightest, StringBuilder style ) { - style.append( "width:" + ( tableCell.getWidth() / TWIPS_PER_INCH ) - + "in;" ); - style.append( "padding-start:" - + ( tableRow.getGapHalf() / TWIPS_PER_INCH ) + "in;" ); - style.append( "padding-end:" - + ( tableRow.getGapHalf() / TWIPS_PER_INCH ) + "in;" ); + style.append("width:").append(tableCell.getWidth() / TWIPS_PER_INCH).append("in;"); + style.append("padding-start:").append(tableRow.getGapHalf() / TWIPS_PER_INCH).append("in;"); + style.append("padding-end:").append(tableRow.getGapHalf() / TWIPS_PER_INCH).append("in;"); BorderCode top = tableCell.getBrcTop() != null && tableCell.getBrcTop().getBorderType() != 0 ? tableCell @@ -224,8 +217,7 @@ public class WordToHtmlUtils extends AbstractWordUtils { if ( tableRow.getRowHeight() > 0 ) { - style.append( "height:" - + ( tableRow.getRowHeight() / TWIPS_PER_INCH ) + "in;" ); + style.append("height:").append(tableRow.getRowHeight() / TWIPS_PER_INCH).append("in;"); } if ( !tableRow.cantSplit() ) { diff --git a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/TestFieldsTables.java b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/TestFieldsTables.java index 1e1b5b3841..6ee6537648 100644 --- a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/TestFieldsTables.java +++ b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/TestFieldsTables.java @@ -83,7 +83,7 @@ public class TestFieldsTables extends HWPFTestCase { private String dumpPlexes(ArrayList<PlexOfField> fieldsPlexes) { StringBuilder dump = new StringBuilder(); for (PlexOfField flds : fieldsPlexes) { - dump.append(flds + "\n"); + dump.append(flds).append("\n"); } return dump.toString(); } |