Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.poi.xwpf.usermodel.XWPFNum;
import org.apache.poi.xwpf.usermodel.XWPFNumbering;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
Expand Down Expand Up @@ -2154,23 +2155,39 @@ private void makeHyperlink(XWPFParagraph para, XmlCursor cursor) throws DocxGene

// Set the appropriate target:

XWPFHyperlinkRun hyperlinkRun;

if (href.startsWith("#")) {
// Just a fragment ID, must be to a bookmark
String bookmarkName = href.substring(1);
hyperlink.setAnchor(bookmarkName);
cursor.push();
hyperlinkRun = makeHyperlinkRun(hyperlink, cursor, para);
cursor.pop();
} else {
// Create a relationship that targets the href and use the
// relationship's ID on the hyperlink
// It's not yet clear from the POI API how to create a new relationship for
// use by an external hyperlink.
// throw new NotImplementedException("Links to external resources not yet implemented.");
}
// Add the link as External relationship
String id = para.getDocument().getPackagePart().addExternalRelationship(href, XWPFRelation.HYPERLINK.getRelation()).getId();

cursor.push();
XWPFHyperlinkRun hyperlinkRun = makeHyperlinkRun(hyperlink, cursor, para);
cursor.pop();
para.addRun(hyperlinkRun);
// Append the link and bind it to the relationship
hyperlink.setId(id);

// Create the linked text
String linkedText = cursor.getTextValue();
CTText ctText = CTText.Factory.newInstance();
ctText.setStringValue(linkedText);
CTR ctr = CTR.Factory.newInstance();
ctr.setTArray(new CTText[]{ctText});

// Create the formatting
CTRPr rpr = ctr.addNewRPr();
rpr.addNewRStyle().setVal("Hyperlink");

// Insert the linked text into the link
hyperlink.setRArray(new CTR[]{ctr});

hyperlinkRun = new XWPFHyperlinkRun(hyperlink, CTR.Factory.newInstance(), para);
}
para.addRun(hyperlinkRun);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/main/xsl/html2docx/baseProcessing.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,8 @@

<xsl:template match="xhtml:a[@href] | a[@href]">
<xsl:param name="doDebug" as="xs:boolean" tunnel="yes" select="false()"/>

<xsl:variable name="targetId" as="xs:string?" select="tokenize(@href, '#')[last()]"/>

<wp:hyperlink href="#{$targetId}">
<xsl:variable name="targetId" as="xs:string?" select="@href"/>
<wp:hyperlink href="{$targetId}">
<xsl:apply-templates>
<xsl:with-param name="doDebug" as="xs:boolean" tunnel="yes" select="$doDebug"/>
</xsl:apply-templates>
Expand Down
9 changes: 8 additions & 1 deletion src/main/xsl/html2docx/get-style-name.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@
<xsl:variable name="level" as="xs:string"
select="if ($level eq '1') then '' else $level"
/>
<xsl:sequence select="'List ' || $level"/>
<xsl:choose>
<xsl:when test="$list-type = 'ol'">
<xsl:sequence select="'List Number ' || $level"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="'List ' || $level"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template mode="get-style-name" match="*" priority="-1" as="xs:string?">
Expand Down