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

org.docx4j.convert.in.xhtml.HeadingHandler Maven / Gradle / Ivy

There is a newer version: 11.4.8
Show newest version
package org.docx4j.convert.in.xhtml;

import java.util.HashMap;
import java.util.Map;

import org.docx4j.wml.Style;
import org.docx4j.wml.Styles;

public class HeadingHandler {
	
	
	/**
	 * HTML element (eg h1) to style ID (eg berschrift1) 
	 */
	private Map elementToStyleId = new HashMap(); 
	
	private static final String HEADING_NAME_PREFIX = "heading ";
	
	protected HeadingHandler(Styles styles) {
		
		/*  A style looks like:
		 * 
		 *   
		 *       
		 *       
		 *  In other words, irrespective of the language, the name
		 *  remains in English.
		 */
		
		for (Style s : styles.getStyle()) {
			
			if (s.getName().getVal().startsWith(HEADING_NAME_PREFIX)) {
				
				// We may wish to map this
				String styleName= s.getName().getVal();
				String suffix =  styleName.substring(HEADING_NAME_PREFIX.length());
				
				try {
					int lvl = Integer.parseInt(suffix);
					
					if (lvl>=1 && lvl <=9) {
						elementToStyleId.put("h"+lvl, s.getStyleId());
					}
					
				} catch (NumberFormatException nfe) {
					
				}
				
			}
		}
		
	}

	protected String getStyle(String localname) {
		return elementToStyleId.get(localname);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy