Package com.independentsoft.office
Class Util
java.lang.Object
com.independentsoft.office.Util
The Class Util.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringNormalises a colour to the eight hex digits SpreadsheetML's rgb attribute is typed as (hexBinary, four bytes: ARGB).static StringconvertColumnIndex(int column)Convert column index.static intconvertColumnIndex(String column)Convert column index.static StringInjects xmlns declarations on a verbatim fragment's root element for every prefix that appears in a Requires / mc:Ignorable attribute value but is not declared in the fragment itself.static StringdecodeEscapeCharacters(String input)Decode escape characters.static StringencodeEscapeCharacters(String input)Encode escape characters.static StringEncode escape characters with special characters.static StringformatVmlNumber(double value)Formats a VML numeric value, omitting a redundant trailing ".0" for whole numbers (e.g.static StringCaptures the prefixed attributes of the current element that the object model does not itself model (anything outside the default, r: and mc: namespaces), together with an xmlns declaration for each distinct prefix, so they can be re-emitted verbatim on the element's start tag.static intGets the next random.static StringgetRelationshipFilePath(String filePath)Gets the relationship file path.static StringgetRootExtraNamespacesAndIgnorable(XMLStreamReader reader, Set<String> skipPrefixes)Captures, from a part's root start element, the namespace declarations (excluding the default namespace and any prefix inskipPrefixesthe caller already emits) plus anmc:Ignorableattribute, as a string to append to the re-emitted root tag.static StringhashPassword(String password)Hash password.static StringNormalises a colour to ST_HexColor - six hex digits, or the literal "auto" - which is the only form WordprocessingML accepts.static Stringmd5(byte[] data)static Stringstatic booleanparseBoolean(String input)Parses the boolean.static DateParses the date.static DateParses the date.static intParses an integer attribute value tolerantly: tries a plain integer first, then falls back to parsing a decimal (e.g.static intParses an integer attribute value in the given radix (e.g.static intparseInteger(String input)Parses the integer.static intparseIntegerPercent(String input)Parses the integer percent.static StringprotectionPassword(String input)Converts a protection password to the short hex hash these attributes are typed as.static StringreplaceFileNameEscapeCharacters(String fileName)Replace file name escape characters.static StringresolvePartPath(String baseFolder, String target)Resolves a relationship target against the folder of the part that declares it, yielding a package-root-relative path.static StringtoLocalTime(Date date)To local time.static StringtoUniversalTime(Date date)To universal time.static StringWrites an xsd:dateTime value in UTC, with the trailing "Z" that says so.static StringTrim.
-
Field Details
-
S
-
W
-
P
-
P14
-
MC
-
-
Constructor Details
-
Util
public Util()
-
-
Method Details
-
formatVmlNumber
Formats a VML numeric value, omitting a redundant trailing ".0" for whole numbers (e.g. the integer-valued o:spt shape-type id).Double.toStringwould render 202 as "202.0", which does not round-trip.- Parameters:
value- the value- Returns:
- the formatted value
-
getRootExtraNamespacesAndIgnorable
public static String getRootExtraNamespacesAndIgnorable(XMLStreamReader reader, Set<String> skipPrefixes)Captures, from a part's root start element, the namespace declarations (excluding the default namespace and any prefix inskipPrefixesthe caller already emits) plus anmc:Ignorableattribute, as a string to append to the re-emitted root tag. Round-trips MCE markers (e.g.mc:Ignorable="x14ac") together with the namespaces they reference, so attributes such asx14ac:knownFonts/x14ac:dyDescentstay well-formed.- Parameters:
reader- positioned on the root start elementskipPrefixes- prefixes already declared on the re-emitted root (may be null)- Returns:
- the extra attribute string (each token has a leading space), or "" if none
-
resolvePartPath
Resolves a relationship target against the folder of the part that declares it, yielding a package-root-relative path.An absolute target (leading "/") is package-root-relative already; a relative one is appended to
baseFolder. "." and ".." segments are normalised away, so e.g. base "xl/drawings" + target "../media/image1.png" resolves to "xl/media/image1.png".- Parameters:
baseFolder- the folder of the declaring part (may be null or empty)target- the relationship target- Returns:
- the resolved package-root-relative path, or null if target is null
-
getExtraNamespacedAttributes
Captures the prefixed attributes of the current element that the object model does not itself model (anything outside the default, r: and mc: namespaces), together with an xmlns declaration for each distinct prefix, so they can be re-emitted verbatim on the element's start tag.- Parameters:
reader- positioned on the start element- Returns:
- the extra attribute string (each token has a leading space), or "" if none
-
parseInt
Parses an integer attribute value tolerantly: tries a plain integer first, then falls back to parsing a decimal (e.g. fractional twips/EMU such as "11905.511811023622") and truncating toward zero, returning 0 when neither parses. Mirrors the C# Util.ParseInt (invariant-culture). Java's Integer/Double parsing is already locale-independent.- Parameters:
value- the attribute value- Returns:
- the parsed integer, or 0 if it cannot be parsed
-
parseInt
Parses an integer attribute value in the given radix (e.g. radix 16 for hex theme shade/tint), returning 0 when it cannot be parsed. Tolerant counterpart of Integer.parseInt(value, radix).- Parameters:
value- the attribute valueradix- the radix- Returns:
- the parsed integer, or 0 if it cannot be parsed
-
hexColor
Normalises a colour to ST_HexColor - six hex digits, or the literal "auto" - which is the only form WordprocessingML accepts. Developers reasonably write CSS-style "#FF0000", and the published examples do exactly that; the '#' makes the attribute invalid and Word offers to repair the file. Normalising once here beats repeating it in every writer that emits a colour. An eight-digit ARGB value loses its alpha, for which WordprocessingML has no place. Anything unrecognised is escaped and returned unchanged: guessing would hide the mistake.- Parameters:
input- the colour as the caller wrote it- Returns:
- the colour as the attribute must carry it
-
argbColor
Normalises a colour to the eight hex digits SpreadsheetML's rgb attribute is typed as (hexBinary, four bytes: ARGB). Six digits is a perfectly good colour and an invalid attribute, so it gains an opaque alpha here - which is what a caller writing "FF0000" means.- Parameters:
input- the colour as the caller wrote it- Returns:
- the colour as the attribute must carry it
-
protectionPassword
Converts a protection password to the short hex hash these attributes are typed as. Writing the password itself produces a file Excel refuses - the attribute is hexBinary, not a string - and puts the password in the document in clear, which is worse than the schema error. A value that is already four hex digits is passed through: that is what a document loaded from disk carries, and re-hashing it on every save would silently change the password. The algorithm is the one ECMA-376 defines for these legacy attributes; it is obfuscation, not security.- Parameters:
input- the password as the caller wrote it- Returns:
- the hash as the attribute must carry it
-
encodeEscapeCharacters
Encode escape characters.- Parameters:
input- the input- Returns:
- the string
-
replaceFileNameEscapeCharacters
Replace file name escape characters.- Parameters:
fileName- the file name- Returns:
- the string
-
encodeEscapeCharactersWithSpecialCharacters
Encode escape characters with special characters.- Parameters:
input- the input- Returns:
- the string
-
decodeEscapeCharacters
Decode escape characters.- Parameters:
input- the input- Returns:
- the string
-
convertColumnIndex
Convert column index.- Parameters:
column- the column- Returns:
- the string
-
convertColumnIndex
Convert column index.- Parameters:
column- the column- Returns:
- the int
-
trim
Trim.- Parameters:
input- the inputcharacter- the character- Returns:
- the string
-
parseDate
Parses the date.- Parameters:
utcDate- the utc date- Returns:
- the date
-
parseDate
Parses the date.- Parameters:
utcDate- the utc dateformat- the format- Returns:
- the date
-
toUniversalTime
To universal time.- Parameters:
date- the date- Returns:
- the string
-
toLocalTime
To local time.- Parameters:
date- the date- Returns:
- the string
-
toUtcTime
Writes an xsd:dateTime value in UTC, with the trailing "Z" that says so. parseDate reads such a value as a UTC instant, so writing it back through toLocalTime renders it in the JVM's own zone and drops the "Z" - the timestamp then shifts by the machine's offset on every save, and the same document produces different output on different machines. Use this for any attribute the format defines as UTC.- Parameters:
date- the date- Returns:
- the string
-
getNextRandom
public static int getNextRandom()Gets the next random.- Returns:
- the next random
-
getRelationshipFilePath
Gets the relationship file path.- Parameters:
filePath- the file path- Returns:
- the relationship file path
-
hashPassword
Hash password.- Parameters:
password- the password- Returns:
- the string
-
parseBoolean
Parses the boolean.- Parameters:
input- the input- Returns:
- true, if successful
-
parseInteger
Parses the integer.- Parameters:
input- the input- Returns:
- the int
-
parseIntegerPercent
Parses the integer percent.- Parameters:
input- the input- Returns:
- the int
-
md5
-
md5
-
declareCompatibilityPrefixes
Injects xmlns declarations on a verbatim fragment's root element for every prefix that appears in a Requires / mc:Ignorable attribute value but is not declared in the fragment itself. A prefix named only inside such an attribute value is not a namespace usage the XML reader can resolve, so the captured markup would otherwise be invalid once re-emitted into another part.- Parameters:
xml- the captured fragment- Returns:
- the fragment with the missing declarations added
-