/
SetAttributes.java
141 lines (117 loc) · 4.38 KB
/
SetAttributes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* SetAttributes.java
*
* Copyright 2008 Mark Logic Corporation.
* Portions Copyright 2007 Sun Microsystems, Inc.
* All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* https://xproc.dev.java.net/public/CDDL+GPL.html or
* docs/CDDL+GPL.txt in the distribution. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at docs/CDDL+GPL.txt.
*/
package com.xmlcalabash.library;
import java.util.Map;
import java.util.HashSet;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.util.ProcessMatchingNodes;
import com.xmlcalabash.util.ProcessMatch;
import com.xmlcalabash.util.S9apiUtils;
import com.xmlcalabash.io.ReadablePipe;
import com.xmlcalabash.io.WritablePipe;
import com.xmlcalabash.model.RuntimeValue;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmSequenceIterator;
import net.sf.saxon.s9api.Axis;
import com.xmlcalabash.runtime.XAtomicStep;
/**
*
* @author ndw
*/
public class SetAttributes extends DefaultStep implements ProcessMatchingNodes {
private static final QName _match = new QName("match");
private ReadablePipe attributes = null;
private ReadablePipe source = null;
private WritablePipe result = null;
private Map<QName, RuntimeValue> inScopeOptions = null;
private ProcessMatch matcher = null;
private XdmNode root = null;
private HashSet<QName> attrs = null;
/** Creates a new instance of SetAttributes */
public SetAttributes(XProcRuntime runtime, XAtomicStep step) {
super(runtime,step);
}
public void setInput(String port, ReadablePipe pipe) {
if ("source".equals(port)) {
source = pipe;
} else {
attributes = pipe;
}
}
public void setOutput(String port, WritablePipe pipe) {
result = pipe;
}
public void reset() {
source.resetReader();
result.resetWriter();
}
public void run() throws SaxonApiException {
super.run();
root = S9apiUtils.getDocumentElement(attributes.read());
attrs = new HashSet<QName> ();
XdmSequenceIterator iter = root.axisIterator(Axis.ATTRIBUTE);
while (iter.hasNext()) {
XdmNode attr = (XdmNode) iter.next();
attrs.add(attr.getNodeName());
}
matcher = new ProcessMatch(runtime, this);
matcher.match(source.read(), getOption(_match));
if (source.moreDocuments()) {
throw XProcException.dynamicError(6, "Reading source on " + getStep().getName());
}
result.write(matcher.getResult());
}
public boolean processStartDocument(XdmNode node) throws SaxonApiException {
throw XProcException.stepError(23);
}
public void processEndDocument(XdmNode node) throws SaxonApiException {
// nop
}
public boolean processStartElement(XdmNode node) throws SaxonApiException {
matcher.addStartElement(node);
XdmSequenceIterator iter = node.axisIterator(Axis.ATTRIBUTE);
while (iter.hasNext()) {
XdmNode attr = (XdmNode) iter.next();
if (!attrs.contains(attr.getNodeName())) {
matcher.addAttribute(attr);
}
}
matcher.addAttributes(root);
matcher.startContent();
return true;
}
public void processAttribute(XdmNode node) throws SaxonApiException {
throw XProcException.stepError(23);
}
public void processEndElement(XdmNode node) throws SaxonApiException {
matcher.addEndElement();
}
public void processText(XdmNode node) throws SaxonApiException {
throw XProcException.stepError(23);
}
public void processComment(XdmNode node) throws SaxonApiException {
throw XProcException.stepError(23);
}
public void processPI(XdmNode node) throws SaxonApiException {
throw XProcException.stepError(23);
}
}