1 package org.tinyjee.maven.dim.utils; 2 3 import org.w3c.dom.Document; 4 import org.w3c.dom.Node; 5 6 import java.io.Reader; 7 import java.net.URL; 8 9 /** 10 * Base class to {@link Document} builders that add element location information into the generated element nodes. 11 * 12 * @author Juergen_Kellerer, 2011-10-13 13 */ 14 public abstract class AbstractPositioningDocumentBuilder { 15 /** 16 * Is a user-data key (for {@link org.w3c.dom.Node#getUserData(String)}) that may be used to return the line number of an element node. 17 */ 18 public static final String KEY_LINE_NUMBER = "lineNumber"; 19 /** 20 * Is a user-data key (for {@link org.w3c.dom.Node#getUserData(String)}) that may be used to return the column number of an element node. 21 */ 22 public static final String KEY_COLUMN_NUMBER = "columnNumber"; 23 24 /** 25 * Returns the line number of the specified node if known, -1 otherwise. 26 * 27 * @param node a node that was created using this builder. 28 * @return the line number of the specified node if known, -1 otherwise. 29 */ 30 public static int getLineNumber(Node node) { 31 return extractNumber(node, KEY_LINE_NUMBER); 32 } 33 34 /** 35 * Returns the column number of the specified node if known, -1 otherwise. 36 * 37 * @param node a node that was created using this builder. 38 * @return the column number of the specified node if known, -1 otherwise. 39 */ 40 public static int getColumnNumber(Node node) { 41 return extractNumber(node, KEY_COLUMN_NUMBER); 42 } 43 44 private static int extractNumber(Node node, String key) { 45 Object data = node.getUserData(key); 46 return data == null ? -1 : (Integer) data; 47 } 48 49 /** 50 * Parses the given URL to a {@link Document}. 51 * 52 * @param systemId the URL of the document to parse. 53 * @return the parsed document. 54 * @throws Exception In case of parsing failed. 55 */ 56 public abstract Document parse(URL systemId) throws Exception; 57 58 /** 59 * Parses the given URL to a {@link Document}. 60 * 61 * @param systemId the URL of the document to parse. 62 * @param reader a reader containing the content to parse. 63 * @return the parsed document. 64 * @throws Exception In case of parsing failed. 65 */ 66 public abstract Document parse(URL systemId, Reader reader) throws Exception; 67 }