org.docx4j.model.datastorage.BindingHyperlinkResolverForOpenAPI3 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j-core Show documentation
Show all versions of docx4j-core Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
package org.docx4j.model.datastorage;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.wml.P.Hyperlink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BindingHyperlinkResolverForOpenAPI3 extends BindingHyperlinkResolver {
/*
In OpenAPI 3, a Reference Object is a simple object to allow referencing
other components in the specification, internally and externally.
The Reference Object is defined by JSON Reference and follows the same structure,
behavior and rules; reference resolution is accomplished as defined by the
JSON Reference specification
Here we add a hyperlink if the contents starts with "#/components".
So this class is OpenAPI specific (not for general JSON References).
*/
private static Logger log = LoggerFactory.getLogger(BindingHyperlinkResolver.class);
private static final String COMPONENT_REF = "#/components";
/**
* Return the index of the position you wish to treat as a hyperlink.
* The characters from there to the first whitespace following will be treated as
* the URL
*
* @param text
* @return
*/
public int getIndexOfURL(String text) {
/* For OpenAPI, treat a string starting with:
*
* #/components
*
* as a hyperlink to a bookmark.
*/
if (text.startsWith(COMPONENT_REF)) return 0;
return super.getIndexOfURL(text);
}
/**
* @param bookmarkName
* @param url
* @return
* @throws JAXBException
*/
public Hyperlink generateHyperlink(String bookmarkName, String url) throws JAXBException {
if (url.startsWith(COMPONENT_REF)) {
String hpl = "" +
"" +
"" +
" " +
" " +
"" + url + " " +
" " +
" ";
return (Hyperlink)XmlUtils.unmarshalString(hpl);
} else {
return super.generateHyperlink(bookmarkName /* actually, relId here */, url);
}
}
}