Revision 2661

tmp/org.txm.core/src/java/org/txm/xml/Hook.java (revision 2661)
12 12
	
13 13
	protected T parentParser = null;
14 14
	
15
	protected HookActivator activator = null;
15
	protected HookActivator<T> activator = null;
16 16
	
17 17
	protected String name;
18 18
	
......
31 31
	 * @throws IOException
32 32
	 * @throws XMLStreamException
33 33
	 */
34
	public Hook(String name, HookActivator activator, T parentParser) throws IOException, XMLStreamException {
34
	public Hook(String name, HookActivator<T> activator, T parentParser) throws IOException, XMLStreamException {
35 35
		this.parentParser = parentParser;
36 36
		this.name = name;
37 37
		setActivator(activator); // set the activator
......
39 39
		this.parser = parentParser.parser;
40 40
	}
41 41
	
42
	public void setActivator(HookActivator activator) {
42
	public void setActivator(HookActivator<T> activator) {
43 43
		this.activator = activator;
44
		if (activator != null) {
44
		if (activator.getHook() != this) {
45 45
			activator.setHook(this);
46 46
		}
47 47
	}
......
176 176
	public String getLocalName() {
177 177
		return parentParser.localname;
178 178
	}
179
	
180
	public HookActivator<T> getActivator() {
181
		return activator;
182
	}
179 183
}
tmp/org.txm.core/src/java/org/txm/xml/XPathHookActivator.java (revision 2661)
16 16
 * @author mdecorde
17 17
 *
18 18
 */
19
public class XPathHookActivator extends HookActivator {
19
public class XPathHookActivator<T extends XMLParser> extends HookActivator<T> {
20 20
	
21 21
	String xpath;
22 22
	
......
30 30
	/**
31 31
	 * @param localname the element localname to match
32 32
	 */
33
	public XPathHookActivator(Hook hook, String xpath) {
33
	public XPathHookActivator(Hook<T> hook, String xpath) {
34
		super(hook);
34 35
		this.xpath = xpath;
35 36
		String regex = xpath;
36 37
		
tmp/org.txm.core/src/java/org/txm/xml/XPathsHookActivator.java (revision 2661)
21 21
 * @author mdecorde
22 22
 *
23 23
 */
24
public class XPathsHookActivator extends XPathHookActivator {
24
public class XPathsHookActivator<T extends XMLParser> extends XPathHookActivator<T> {
25 25
	
26 26
	int currentXPath = 0;
27
	
27 28
	List<String> xpaths;
28 29
	
29 30
	/**
30 31
	 * @param localname the element localname to match
31 32
	 */
32
	public XPathsHookActivator(Hook hook, List<String> xpaths) {
33
	public XPathsHookActivator(Hook<T> hook, List<String> xpaths) {
33 34
		super(hook, xpaths.get(0));
34 35
		this.xpaths = xpaths;
35 36
		
......
95 96
		
96 97
		try {
97 98
			this.p = Pattern.compile(regex);
98
			if (debug) System.out.println("P="+p);
99
		} catch(PatternSyntaxException e) {
100
			String m = NLS.bind("Pattern not compiled correctly with: {0} at {1}.", xpath+" ("+currentXPath+")", hook.getLocation());
99
			if (debug) System.out.println("P=" + p);
100
		}
101
		catch (PatternSyntaxException e) {
102
			String m = NLS.bind("Pattern not compiled correctly with: {0} at {1}.", xpath + " (" + currentXPath + ")", hook.getLocation());
101 103
			Log.severe(m);
102
			Log.severe("Pattern error: "+e);
104
			Log.severe("Pattern error: " + e);
103 105
			Log.printStackTrace(e);
104 106
			p = null;
105 107
			return false;
......
127 129
	}
128 130
	
129 131
	public static void main(String[] args) {
130
		new XPathsHookActivator(null, 
132
		new XPathsHookActivator(null,
131 133
				Arrays.asList("//text//p[@name='value']", "//text//p[@name='value']"));
132 134
	}
133 135
}
tmp/org.txm.core/src/java/org/txm/xml/LocalNameHookActivator.java (revision 2661)
6 6
 * @author mdecorde
7 7
 *
8 8
 */
9
public class LocalNameHookActivator extends HookActivator {
9
public class LocalNameHookActivator<T extends XMLParser> extends HookActivator<T> {
10 10
	
11 11
	String localname;
12 12
	
13 13
	/**
14 14
	 * @param localname the element localname to match
15 15
	 */
16
	public LocalNameHookActivator(Hook hook, String localname) {
16
	public LocalNameHookActivator(String localname) {
17
		this(null, localname);
18
	}
19
	
20
	
21
	/**
22
	 * @param localname the element localname to match
23
	 */
24
	public LocalNameHookActivator(Hook<T> hook, String localname) {
25
		super(hook);
17 26
		this.localname = localname;
18 27
	}
19 28
	
tmp/org.txm.core/src/java/org/txm/xml/DOMIdentityHook.java (revision 2661)
10 10

  
11 11
public class DOMIdentityHook extends Hook<XMLProcessor> {
12 12
	
13
	Element dom;
13
	/**
14
	 * The constructed DOM element
15
	 */
16
	protected Element dom;
14 17
	
15
	public DOMIdentityHook(String name, HookActivator activator, XMLProcessor processor) throws IOException, XMLStreamException {
18
	public DOMIdentityHook(String name, HookActivator<XMLProcessor> activator, XMLProcessor processor) throws IOException, XMLStreamException {
16 19
		super(name, activator, processor);
17 20
	}
18 21
	
......
63 66
	}
64 67
	
65 68
	private void writeDOM(Node node) throws XMLStreamException {
69
		if (node == null) return; // nothing to write
70
		
66 71
		if (node.getNodeType() == Node.ELEMENT_NODE) {
67 72
			Element e = (Element) node;
68 73
			NodeList children = e.getChildNodes();
......
75 80
			
76 81
			for (int i = 0; i < e.getAttributes().getLength(); i++) {
77 82
				Node att = e.getAttributes().item(i);
78
				parentParser.writer.writeAttribute(att.getNamespaceURI(), att.getNodeName(), att.getNodeValue());
83
				if (att.getNamespaceURI() != null) {
84
					parentParser.writer.writeAttribute(att.getNamespaceURI(), att.getNodeName(), att.getNodeValue());
85
				}
86
				else {
87
					parentParser.writer.writeAttribute(att.getNodeName(), att.getNodeValue());
88
				}
79 89
			}
80 90
			
81 91
			for (int c = 0; c < children.getLength(); c++) {
tmp/org.txm.core/src/java/org/txm/xml/LocalNamesHookActivator.java (revision 2661)
10 10
 * @author mdecorde
11 11
 *
12 12
 */
13
public class LocalNamesHookActivator extends HookActivator {
13
public class LocalNamesHookActivator<T extends XMLParser> extends HookActivator<T> {
14 14
	
15 15
	HashSet<String> localnames;
16 16
	
17 17
	/**
18 18
	 * @param localnames the element localnames to match
19 19
	 */
20
	public LocalNamesHookActivator(Hook hook, List<String> localnames) {
21
		super();
20
	public LocalNamesHookActivator(List<String> localnames) {
21
		this(null, localnames);
22
	}
23
	
24
	/**
25
	 * @param localnames the element localnames to match
26
	 */
27
	public LocalNamesHookActivator(Hook<T> hook, List<String> localnames) {
28
		super(hook);
22 29
		this.localnames = new HashSet<>();
23 30
		this.localnames.addAll(localnames);
24 31
	}
......
26 33
	/**
27 34
	 * @param localnames the element localnames to match
28 35
	 */
29
	public LocalNamesHookActivator(Hook hook, String... localnames) {
36
	public LocalNamesHookActivator(Hook<T> hook, String... localnames) {
30 37
		this(hook, Arrays.asList(localnames));
31 38
	}
32 39
	
tmp/org.txm.core/src/java/org/txm/xml/HookActivator.java (revision 2661)
6 6
 * @author mdecorde
7 7
 *
8 8
 */
9
public abstract class HookActivator {
9
public abstract class HookActivator<T extends XMLParser> {
10 10
	
11 11
	/**
12 12
	 * to access to current parsing informations
13 13
	 */
14
	Hook hook;
14
	Hook<T> hook;
15 15
	
16 16
	/**
17 17
	 * 
18 18
	 * The Hook will be associated in the Hook constructor
19
	 * 
20
	 * @param hook if the hook is already defined. you can associated it that way (or call hook.setActivator() && activator.setHook(
19 21
	 */
20
	public HookActivator() {}
22
	public HookActivator(Hook<T> hook) {
23
		this.hook = hook;
24
		if (hook != null) {
25
			hook.setActivator(this);
26
		}
27
	}
21 28
	
22 29
	/**
23 30
	 * 
......
35 42
	 * 
36 43
	 * @param hook this activators hook
37 44
	 */
38
	public void setHook(Hook hook) {
45
	public void setHook(Hook<T> hook) {
39 46
		this.hook = hook;
47
		if (hook.getActivator() != this) {
48
			hook.setActivator(this);
49
		}
40 50
	}
51
	
52
	public Hook<T> getHook() {
53
		return hook;
54
	}
41 55
}

Also available in: Unified diff