All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.model.datastorage.BindingHyperlinkResolverForOpenAPI3 Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 11.4.11
Show newest version
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);
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy