public void endTransformingElement(String uri,
String name,
String raw) throws IOException, SAXException, ProcessingException {
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("BEGIN endTransformingElement uri=" + uri +
", name=" + name +
", raw=" + raw);
}
if (name.equals(INSERT_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_OUTSIDE;
// get the information from the stack
String tag;
String fileName = null;
DocumentFragment fragment = null;
String path = null;
String replacePath = null;
String reinsert = null;
do {
tag = (String)stack.pop();
if (tag.equals("PATH") == true) {
path = (String)stack.pop();
} else if (tag.equals("FILE") == true) {
fileName = (String)stack.pop();
} else if (tag.equals("FRAGMENT") == true) {
fragment = (DocumentFragment)stack.pop();
} else if (tag.equals("REPLACE") == true) {
replacePath = (String)stack.pop();
} else if (tag.equals("REINSERT") == true) {
reinsert = (String)stack.pop();
}
} while (tag.equals("INSERT") == false);
final boolean overwrite = stack.pop().equals("true");
final boolean create = stack.pop().equals("true");
this.insertFragment(fileName,
path,
fragment,
replacePath,
create,
overwrite,
reinsert);
// Element: file
} else if (name.equals(FILE_ELEMENT) == true && this.state == InsertTransformer.STATE_FILE) {
state = InsertTransformer.STATE_INSERT;
stack.push(this.endTextRecording());
stack.push("FILE");
// Element: path
} else if (name.equals(PATH_ELEMENT) == true && this.state == InsertTransformer.STATE_PATH) {
state = InsertTransformer.STATE_INSERT;
stack.push(this.endTextRecording());
stack.push("PATH");
// Element: replace
} else if (name.equals(REPLACE_ELEMENT) == true && this.state == InsertTransformer.STATE_REPLACE) {
state = InsertTransformer.STATE_INSERT;
stack.push(this.endTextRecording());
stack.push("REPLACE");
// Element: fragment
} else if (name.equals(FRAGMENT_ELEMENT) == true && this.state == InsertTransformer.STATE_FRAGMENT) {
state = InsertTransformer.STATE_INSERT;
stack.push(this.endRecording());
stack.push("FRAGMENT");
// Element: reinsert
} else if (name.equals(REINSERT_ELEMENT) == true
&& this.state == InsertTransformer.STATE_REINSERT) {
state = InsertTransformer.STATE_INSERT;
stack.push(this.endTextRecording());
stack.push("REINSERT");
// default
} else {
super.endTransformingElement(uri, name, raw);
}
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("END endTransformingElement");
}
}
|
public void insertFragment(String fileName,
String path,
DocumentFragment fragment,
String replacePath,
boolean create,
boolean overwrite,
String reinsertPath) throws IOException, SAXException, ProcessingException {
// no sync req
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("BEGIN insertFragment fileName="+fileName+
", path="+path+
", replace="+replacePath+
", create="+create+
", overwrite="+overwrite+
", resinsert="+reinsertPath+
", fragment="+(fragment == null ? "null" : XMLUtils.serializeNodeToXML(fragment)));
}
// test parameter
if (fileName == null) {
throw new ProcessingException("insertFragment: file name is required.");
}
if (path == null) {
throw new ProcessingException("insertFragment: path is required.");
}
if (fragment == null) {
throw new ProcessingException("insertFragment: fragment is required.");
}
Source fileSource = null;
String systemId = null;
try {
fileSource = this.resolver.resolve( fileName );
systemId = fileSource.getSystemId();
if (systemId.startsWith("file:") == false) {
throw new ProcessingException("insertFragment: this is not a file: " + systemId);
}
} finally {
if (fileSource != null) fileSource.recycle();
}
if (path.startsWith("/") == true) path = path.substring(1);
File file = new File(systemId.substring(5));
DocumentFragment resource = null;
if (file.exists() == true) {
resource = this.getResourceConnector().loadXML(ResourceConnector.RESOURCE_TYPE_FILE, null,
fileName, null);
// import the fragment
Node importNode = resource.getOwnerDocument().importNode(fragment, true);
// get the node
Node parent = XMLUtil.selectSingleNode(resource, path);
// replace?
if (replacePath != null) {
try {
Node replaceNode = XMLUtil.getSingleNode(parent, replacePath);
// now get the parent of this node until it is the parent node for insertion
while (replaceNode != null && replaceNode.getParentNode().equals(parent) == false) {
replaceNode = replaceNode.getParentNode();
}
if (replaceNode != null) {
if (overwrite == true) {
parent.replaceChild(importNode, replaceNode);
if (reinsertPath != null) {
Node insertAt = XMLUtil.getSingleNode(importNode, reinsertPath);
if (insertAt != null) {
while (replaceNode.hasChildNodes() == true) {
insertAt.appendChild(replaceNode.getFirstChild());
}
}
}
}
} else {
parent.appendChild(importNode);
}
} catch (javax.xml.transform.TransformerException sax) {
throw new ProcessingException("TransformerException: " + sax, sax);
}
} else { // no replace
parent.appendChild(importNode);
}
} else {
if (create == true) {
Document doc = XMLUtil.createDocument();
resource = doc.createDocumentFragment();
// import the fragment
Node importNode = resource.getOwnerDocument().importNode(fragment, true);
// get the node
Node parent = XMLUtil.selectSingleNode(resource, path);
// add fragment
parent.appendChild(importNode);
}
}
if (resource != null) {
// finally: save resource
resource.normalize();
this.getResourceConnector().saveXML(ResourceConnector.RESOURCE_TYPE_FILE, null,
fileName, null,
resource);
}
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("END insertFragment");
}
}
Insert a fragment into a file.
The file is loaded by the resource connector. |
public void startTransformingElement(String uri,
String name,
String raw,
Attributes attr) throws IOException, SAXException, ProcessingException {
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("BEGIN startTransformingElement uri=" + uri +
", name=" + name + ", raw=" + raw + ", attr=" + attr);
}
// Element: insert
if (name.equals(INSERT_ELEMENT) == true && this.state == InsertTransformer.STATE_OUTSIDE) {
state = InsertTransformer.STATE_INSERT;
if (attr.getValue(InsertTransformer.INSERT_CREATE_ATTRIBUTE) != null
&& attr.getValue(InsertTransformer.INSERT_CREATE_ATTRIBUTE).equals("false") == true) {
stack.push("false");
} else {
stack.push("true");
}
if (attr.getValue(InsertTransformer.INSERT_OVERWRITE_ATTRIBUTE) != null
&& attr.getValue(InsertTransformer.INSERT_OVERWRITE_ATTRIBUTE).equals("false") == true) {
stack.push("false");
} else {
stack.push("true");
}
stack.push("INSERT");
// Element: file
} else if (name.equals(FILE_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_FILE;
this.startTextRecording();
// Element: path
} else if (name.equals(PATH_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_PATH;
this.startTextRecording();
// Element: replace
} else if (name.equals(REPLACE_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_REPLACE;
this.startTextRecording();
// Element: fragment
} else if (name.equals(FRAGMENT_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_FRAGMENT;
this.startRecording();
// Element: reinsert
} else if (name.equals(REINSERT_ELEMENT) == true
&& this.state == InsertTransformer.STATE_INSERT) {
state = InsertTransformer.STATE_REINSERT;
this.startTextRecording();
// default
} else {
super.startTransformingElement(uri, name, raw, attr);
}
if (this.getLogger().isDebugEnabled() == true) {
this.getLogger().debug("END startTransformingElement");
}
}
This is the real implementation of the startElement event for the Insert Transformer
The event is checked for a valid element and the corresponding command
is executed. |