Java Code Examples for org.apache.poi.util.StringUtil#hasMultibyte()

The following examples show how to use org.apache.poi.util.StringUtil#hasMultibyte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void serialize(LittleEndianOutput out) {

		out.writeShort(getFontHeight());
		out.writeShort(getAttributes());
		out.writeShort(getColorPaletteIndex());
		out.writeShort(getBoldWeight());
		out.writeShort(getSuperSubScript());
		out.writeByte(getUnderline());
		out.writeByte(getFamily());
		out.writeByte(getCharset());
		out.writeByte(field_9_zero);
		int fontNameLen = field_11_font_name.length();
		out.writeByte(fontNameLen);
		boolean hasMultibyte = StringUtil.hasMultibyte(field_11_font_name);
		out.writeByte(hasMultibyte ? 0x01 : 0x00);
		if (fontNameLen > 0) {
			if (hasMultibyte) {
			   StringUtil.putUnicodeLE(field_11_font_name, out);
			} else {
				StringUtil.putCompressedUnicode(field_11_font_name, out);
			}
		}
	}
 
Example 2
Source File: NameCommentRecord.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void serialize(final LittleEndianOutput out) {
  final int field_4_name_length = field_6_name_text.length();
  final int field_5_comment_length = field_7_comment_text.length();

  out.writeShort(field_1_record_type);
  out.writeShort(field_2_frt_cell_ref_flag);
  out.writeLong(field_3_reserved);
  out.writeShort(field_4_name_length);
  out.writeShort(field_5_comment_length);

  boolean isNameMultiByte = StringUtil.hasMultibyte(field_6_name_text);
  out.writeByte(isNameMultiByte ? 1 : 0);
  if (isNameMultiByte) {
      StringUtil.putUnicodeLE(field_6_name_text, out);
  } else {
      StringUtil.putCompressedUnicode(field_6_name_text, out);
  }
  boolean isCommentMultiByte = StringUtil.hasMultibyte(field_7_comment_text);
  out.writeByte(isCommentMultiByte ? 1 : 0);
  if (isCommentMultiByte) {
      StringUtil.putUnicodeLE(field_7_comment_text, out);
  } else {
      StringUtil.putCompressedUnicode(field_7_comment_text, out);
  }
}
 
Example 3
Source File: StringPtg.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a StringPtg from a string representation of the number Number
 * format is not checked, it is expected to be validated in the parser that
 * calls this method.
 * 
 * @param value :
 *            String representation of a floating point number
 */
public StringPtg(String value) {
    if (value.length() > 255) {
        throw new IllegalArgumentException(
                "String literals in formulas can't be bigger than 255 characters ASCII");
    }
    _is16bitUnicode = StringUtil.hasMultibyte(value);
    field_3_string = value;
}
 
Example 4
Source File: SeriesTextRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the text field for the SeriesText record.
 */
public void setText(String text) {
	if (text.length() > MAX_LEN) {
		throw new IllegalArgumentException("Text is too long ("
				+ text.length() + ">" + MAX_LEN + ")");
	}
	field_4_text = text;
	is16bit = StringUtil.hasMultibyte(text);
}
 
Example 5
Source File: ViewFieldsRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected int getDataSize() {
	if (_name == null) {
		return BASE_SIZE;
	}
	return BASE_SIZE 
		+ 1 // unicode flag 
		+ _name.length() * (StringUtil.hasMultibyte(_name) ? 2 : 1);
}
 
Example 6
Source File: ContinuableRecordOutput.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes a unicode string complete with header and character data.  This includes:
 * <ul>
 * <li>ushort length</li>
 * <li>byte optionFlags</li>
 * <li>ushort numberOfRichTextRuns (optional)</li>
 * <li>ushort extendedDataSize (optional)</li>
 * <li>encoded character data (in "ISO-8859-1" or "UTF-16LE" encoding)</li>
 * </ul>
 *
 * The following bits of the 'optionFlags' byte will be set as appropriate:
 * <table border='1'>
 * <tr><th>Mask</th><th>Description</th></tr>
 * <tr><td>0x01</td><td>is16bitEncoded</td></tr>
 * <tr><td>0x04</td><td>hasExtendedData</td></tr>
 * <tr><td>0x08</td><td>isRichText</td></tr>
 * </table>
 * Notes:
 * <ul>
 * <li>The value of the 'is16bitEncoded' flag is determined by the actual character data
 * of <tt>text</tt></li>
 * <li>The string header fields are never separated (by a {@link ContinueRecord}) from the
 * first chunk of character data (i.e. the first character is always encoded in the same
 * record as the string header).</li>
 * </ul>
 */
public void writeString(String text, int numberOfRichTextRuns, int extendedDataSize) {
	boolean is16bitEncoded = StringUtil.hasMultibyte(text);
	// calculate total size of the header and first encoded char
	int keepTogetherSize = 2 + 1 + 1; // ushort len, byte optionFlags, at least one character byte
	int optionFlags = 0x00;
	if (is16bitEncoded) {
		optionFlags |= 0x01;
		keepTogetherSize += 1; // one extra byte for first char
	}
	if (numberOfRichTextRuns > 0) {
		optionFlags |= 0x08;
		keepTogetherSize += 2;
	}
	if (extendedDataSize > 0) {
		optionFlags |= 0x04;
		keepTogetherSize += 4;
	}
	writeContinueIfRequired(keepTogetherSize);
	writeShort(text.length());
	writeByte(optionFlags);
	if (numberOfRichTextRuns > 0) {
		writeShort(numberOfRichTextRuns);
	}
	if (extendedDataSize > 0) {
		writeInt(extendedDataSize);
	}
	writeCharacterData(text, is16bitEncoded);
}
 
Example 7
Source File: WriteAccessRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * set the username for the user that created the report. HSSF uses the
 * logged in user.
 * 
 * @param username of the user who is logged in (probably "tomcat" or "apache")
 */
public void setUsername(String username) {
	boolean is16bit = StringUtil.hasMultibyte(username);
	int encodedByteCount = 3 + username.length() * (is16bit ? 2 : 1);
	int paddingSize = DATA_SIZE - encodedByteCount;
	if (paddingSize < 0) {
		throw new IllegalArgumentException("Name is too long: " + username);
	}

	field_1_username = username;
}
 
Example 8
Source File: WriteAccessRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void serialize(LittleEndianOutput out) {
	String username = getUsername();
	boolean is16bit = StringUtil.hasMultibyte(username);

	out.writeShort(username.length());
	out.writeByte(is16bit ? 0x01 : 0x00);
	if (is16bit) {
		StringUtil.putUnicodeLE(username, out);
	} else {
		StringUtil.putCompressedUnicode(username, out);
	}
	int encodedByteCount = 3 + username.length() * (is16bit ? 2 : 1);
	int paddingSize = DATA_SIZE - encodedByteCount;
	out.write(PADDING, 0, paddingSize);
}
 
Example 9
Source File: FontRecord.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected int getDataSize() {
	int size = 16; // 5 shorts + 6 bytes
	int fontNameLen = field_11_font_name.length();
	if (fontNameLen < 1) {
		return size;
	}

	boolean hasMultibyte = StringUtil.hasMultibyte(field_11_font_name);
	return size + fontNameLen * (hasMultibyte ? 2 : 1);
}
 
Example 10
Source File: HeaderFooterBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * set the footer string
 *
 * @param text string to display
 */
public final void setText(String text) {
	if (text == null) {
		throw new IllegalArgumentException("text must not be null");
	}
	field_2_hasMultibyte = StringUtil.hasMultibyte(text);
	field_3_text = text;

	// Check it'll fit into the space in the record
	if (getDataSize() > RecordInputStream.MAX_RECORD_DATA_SIZE) {
		throw new IllegalArgumentException("Header/Footer string too long (limit is "
				+ RecordInputStream.MAX_RECORD_DATA_SIZE + " bytes)");
	}
}
 
Example 11
Source File: StyleRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * set the style's name
 * @param name of the style
 */
public void setName(String name) {
	field_4_name = name;
	field_3_stringHasMultibyte = StringUtil.hasMultibyte(name);
	field_1_xf_index = isBuiltinFlag.clear(field_1_xf_index);
}
 
Example 12
Source File: StringRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Sets the string represented by this record.
 */
public void setString(String string) {
    _text = string;
    _is16bitUnicode = StringUtil.hasMultibyte(string);        
}
 
Example 13
Source File: FormatRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public FormatRecord(int indexCode, String fs) {
    field_1_index_code = indexCode;
    field_4_formatstring = fs;
    field_3_hasMultibyte = StringUtil.hasMultibyte(fs);
}
 
Example 14
Source File: NameCommentRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getDataSize() {
  return 18 // 4 shorts + 1 long + 2 spurious 'nul's
       + (StringUtil.hasMultibyte(field_6_name_text) ? field_6_name_text.length()*2 : field_6_name_text.length())
       + (StringUtil.hasMultibyte(field_7_comment_text) ? field_7_comment_text.length()*2 : field_7_comment_text.length());
}
 
Example 15
Source File: DVRecord.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static int getUnicodeStringSize(UnicodeString us) {
	String str = us.getString();
	return 3 + str.length() * (StringUtil.hasMultibyte(str) ? 2 : 1);
}
 
Example 16
Source File: ContinuableRecordOutput.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Writes the 'optionFlags' byte and encoded character data of a unicode string.  This includes:
 * <ul>
 * <li>byte optionFlags</li>
 * <li>encoded character data (in "ISO-8859-1" or "UTF-16LE" encoding)</li>
 * </ul>
 *
 * Notes:
 * <ul>
 * <li>The value of the 'is16bitEncoded' flag is determined by the actual character data
 * of <tt>text</tt></li>
 * <li>The string options flag is never separated (by a {@link ContinueRecord}) from the
 * first chunk of character data it refers to.</li>
 * <li>The 'ushort length' field is assumed to have been explicitly written earlier.  Hence,
 * there may be an intervening {@link ContinueRecord}</li>
 * </ul>
 */
public void writeStringData(String text) {
	boolean is16bitEncoded = StringUtil.hasMultibyte(text);
	// calculate total size of the header and first encoded char
	int keepTogetherSize = 1 + 1; // ushort len, at least one character byte
	int optionFlags = 0x00;
	if (is16bitEncoded) {
		optionFlags |= 0x01;
		keepTogetherSize += 1; // one extra byte for first char
	}
	writeContinueIfRequired(keepTogetherSize);
	writeByte(optionFlags);
	writeCharacterData(text, is16bitEncoded);
}
 
Example 17
Source File: BoundSheetRecord.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
 * @param sheetName the name of the sheet
 * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)
 *      for a safe way to create valid names
 * @throws IllegalArgumentException if sheet name will cause excel to crash.
 */
public void setSheetname(String sheetName) {

	WorkbookUtil.validateSheetName(sheetName);
	field_5_sheetname = sheetName;
	field_4_isMultibyteUnicode = StringUtil.hasMultibyte(sheetName) ?  1 : 0;
}
 
Example 18
Source File: NoteRecord.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Name of the original comment author
 *
 * @param author the name of the original author of the comment
 */
public void setAuthor(String author) {
	field_6_author = author;
     field_5_hasMultibyte = StringUtil.hasMultibyte(author);
}