將 RTF 文字屬性轉換為簡單文字屬性
使用諸如 getLongDescriptiongetLongProposedSolutiongetLongReason 等 API 來分別擷取 longDescriptionlongProposedSolutionlongReason 等具有標示標籤的 RTF 內容。
使用諸如 getDescriptiongetProposedSolutiongetReason 等 API 來擷取描述、提議的解決方案與原因的純文字值。
欲將 RTF 內容 (具有標示標籤) 轉換為簡單的純文字,請使用下列程式碼:
String richText = ChangeOrder2(Instance).getLongDescription()
HTMLText htmlAttr = HTMLText.newHTMLText(richText);
plainText = htmlAttr.getPlainText();
針對複製封裝將 RTF 屬性轉換為簡單文字屬性
將任何變更物件封裝匯出至 Windchill 時,如果排除諸如 longDescriptionlongProposedSolutionlongReason 等 RTF 文字屬性,您可以自訂來源系統來將從來源匯出的 RTF 文字屬性轉換為簡單文字屬性。自訂會移除現有屬性:descriptionproposedSolutionreason,以及轉換屬性:longDescription 轉換為 descriptionlongReason 轉換為 reason 以及 longProposedSolution 轉換為 proposedSolution。因此, RTF 文字值會轉換為簡單文字值。
針對自訂執行下列步驟︰
1. 根據變更物件的類型,從以下路徑存取各自的檔案︰<WT_HOME>\codebase\registry\XSLRepo\11.0.M030
WTChangeActivity2.xsl
WTChangeIssue.xsl
WTChangeOrder2.xsl
WTChangeRequest2.xsl
WTVariance.xsl
* 
在此範例中,我們會使用 WTChangeIssue.xsl 檔案。
2. 在檔案中新增下列代碼行:
a. 欲移除現有的 description 屬性︰
<xsl:template match="WTChangeIssue/description"></xsl:template>
b. 欲將 longDescription 屬性取代為 description
<xsl:template match="WTChangeIssue/longDescription">
<description>
<xsl:call-template name="plainTextFilter" />
</description>
</xsl:template>
* 
代碼也會呼叫名為 plainTextFilter 的 XSL 範本,之後會定義該範本。
c. 欲匯入 plainTextFilter 範本︰
<xsl:import href="plainTextFilter.xsl" />
3. 在以下位置建立名為 plainTextFilter.xsl 的檔案︰<WT_HOME>\codebase\registry\XSLRepo\11.0.M030,然後在該檔案中新增下列程式碼行︰
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:filter="java:com.custom.text.conversion.PlainTextConverter" >
<xsl:template name="plainTextFilter">
<xsl:value-of select="filter:convertToPlainText(.)" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
這樣做會呼叫自訂類別 convertToPlainTextcom.custom.text.conversion.PlainTextConverter 方法。此方法會包含 RTF 文字字串值作為輸入參數,並傳回簡單的文字字串值。
4. 建立名為 PlainTextConverter 的類別來定義上述自訂類別:
package com.custom.text.conversion;
import org.apache.commons.lang3.StringEscapeUtils;
import com.ptc.core.richtext.HTMLText;
public class PlainTextConverter {
public static String convertToPlainText(String rawText) {
HTMLText htmlAttr;
String plainText = " ((null))";
try {
if (null != rawText && !rawText.isEmpty()) {
htmlAttr = HTMLText.newHTMLText(rawText);
plainText = htmlAttr.getPlainText();
plainText = StringEscapeUtils.escapeXml(plainText);
}
} catch (Exception exp) {
exp.printStackTrace();
}
return plainText;
}
}
* 
該類別應位於 Windchill 類別路徑中;例如,<WT_HOME>\codebase\com\custom\text\conversion
完成上述步驟之後,建立的封裝中將包含具有從 RTF 文字轉換而來的簡單文字值的 descriptionproposedSolutionreason 屬性。
針對所有變更物件與要轉換的 RTF 文字屬性重複步驟 2。
這是否有幫助?