Class Util

java.lang.Object
com.independentsoft.office.Util

public class Util extends Object
The Class Util.
  • Field Details

  • Constructor Details

    • Util

      public Util()
  • Method Details

    • formatVmlNumber

      public static String formatVmlNumber(double value)
      Formats a VML numeric value, omitting a redundant trailing ".0" for whole numbers (e.g. the integer-valued o:spt shape-type id). Double.toString would 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 in skipPrefixes the caller already emits) plus an mc:Ignorable attribute, 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 as x14ac:knownFonts / x14ac:dyDescent stay well-formed.
      Parameters:
      reader - positioned on the root start element
      skipPrefixes - 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

      public static String resolvePartPath(String baseFolder, String target)
      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

      public static String getExtraNamespacedAttributes(XMLStreamReader reader)
      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

      public static int parseInt(String value)
      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

      public static int parseInt(String value, int radix)
      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 value
      radix - the radix
      Returns:
      the parsed integer, or 0 if it cannot be parsed
    • hexColor

      public static String hexColor(String input)
      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

      public static String argbColor(String input)
      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

      public static String protectionPassword(String input)
      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

      public static String encodeEscapeCharacters(String input)
      Encode escape characters.
      Parameters:
      input - the input
      Returns:
      the string
    • replaceFileNameEscapeCharacters

      public static String replaceFileNameEscapeCharacters(String fileName)
      Replace file name escape characters.
      Parameters:
      fileName - the file name
      Returns:
      the string
    • encodeEscapeCharactersWithSpecialCharacters

      public static String encodeEscapeCharactersWithSpecialCharacters(String input)
      Encode escape characters with special characters.
      Parameters:
      input - the input
      Returns:
      the string
    • decodeEscapeCharacters

      public static String decodeEscapeCharacters(String input)
      Decode escape characters.
      Parameters:
      input - the input
      Returns:
      the string
    • convertColumnIndex

      public static String convertColumnIndex(int column)
      Convert column index.
      Parameters:
      column - the column
      Returns:
      the string
    • convertColumnIndex

      public static int convertColumnIndex(String column)
      Convert column index.
      Parameters:
      column - the column
      Returns:
      the int
    • trim

      public static String trim(String input, String character)
      Trim.
      Parameters:
      input - the input
      character - the character
      Returns:
      the string
    • parseDate

      public static Date parseDate(String utcDate)
      Parses the date.
      Parameters:
      utcDate - the utc date
      Returns:
      the date
    • parseDate

      public static Date parseDate(String utcDate, String format)
      Parses the date.
      Parameters:
      utcDate - the utc date
      format - the format
      Returns:
      the date
    • toUniversalTime

      public static String toUniversalTime(Date date)
      To universal time.
      Parameters:
      date - the date
      Returns:
      the string
    • toLocalTime

      public static String toLocalTime(Date date)
      To local time.
      Parameters:
      date - the date
      Returns:
      the string
    • toUtcTime

      public static String toUtcTime(Date date)
      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

      public static String getRelationshipFilePath(String filePath)
      Gets the relationship file path.
      Parameters:
      filePath - the file path
      Returns:
      the relationship file path
    • hashPassword

      public static String hashPassword(String password)
      Hash password.
      Parameters:
      password - the password
      Returns:
      the string
    • parseBoolean

      public static boolean parseBoolean(String input)
      Parses the boolean.
      Parameters:
      input - the input
      Returns:
      true, if successful
    • parseInteger

      public static int parseInteger(String input)
      Parses the integer.
      Parameters:
      input - the input
      Returns:
      the int
    • parseIntegerPercent

      public static int parseIntegerPercent(String input)
      Parses the integer percent.
      Parameters:
      input - the input
      Returns:
      the int
    • md5

      public static String md5(String content)
    • md5

      public static String md5(byte[] data)
    • declareCompatibilityPrefixes

      public static String declareCompatibilityPrefixes(String xml)
      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