throw new FileNotFoundException();
}
file.refreshLocal(1, new NullProgressMonitor());
IPath fullPath = file.getFullPath();
boolean dispose = false;
ITextFileBuffer fileBuffer = (ITextFileBuffer) FileBuffers
.getTextFileBufferManager().getTextFileBuffer(fullPath,
LocationKind.IFILE);
if (fileBuffer == null) {
FileBuffers.getTextFileBufferManager().connect(fullPath,
LocationKind.IFILE, monitor);
fileBuffer = (ITextFileBuffer) FileBuffers
.getTextFileBufferManager().getTextFileBuffer(fullPath,
LocationKind.IFILE);
dispose = true;
}
IDocument document = fileBuffer.getDocument();
try {
DocumentBuilderFactory newInstance = getBuilder();
DocumentBuilder newDocumentBuilder = newInstance
.newDocumentBuilder();
newDocumentBuilder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
Document parse = newDocumentBuilder.parse(conf.openStream());
Document myWebXML = newDocumentBuilder.parse(new InputSource(
new StringReader(document.get())));
NodeList childNodes = parse.getDocumentElement().getChildNodes();
ArrayList<Element> toAppend = new ArrayList<Element>();
for (int a = 0; a < childNodes.getLength(); a++) {
Node n = childNodes.item(a);
if (n instanceof Element) {
Element k = (Element) n;
Element el = find(myWebXML, k);
if (el == null) {
toAppend.add(k);
}
}
}
if (!toAppend.isEmpty()) {
NodeList childNodes2 = myWebXML.getDocumentElement()
.getChildNodes();
boolean inserted = false;
for (int a = 0; a < childNodes2.getLength(); a++) {
Node item = childNodes2.item(a);
if (item instanceof Comment) {
Comment mn = (Comment) item;
if (mn.getTextContent().contains(
ONPOSITIVE_COMMENTS_MARK)) {
for (Element e : toAppend) {
Node adoptNode = myWebXML.adoptNode(e
.cloneNode(true));
if (a != childNodes2.getLength() - 1) {
try {
myWebXML.getDocumentElement()
.insertBefore(adoptNode,
childNodes.item(a + 1));
} catch (DOMException ex) {
mn.getParentNode().appendChild(
adoptNode);
}
} else {
myWebXML.appendChild(adoptNode);
}
}
inserted = true;
}
}
}
if (!inserted) {
myWebXML.getDocumentElement().appendChild(
myWebXML.createComment(ONPOSITIVE_COMMENTS_MARK));
myWebXML.getDocumentElement().appendChild(
myWebXML.createTextNode("\n"));
for (Element e : toAppend) {
Node adoptNode = myWebXML.adoptNode(e.cloneNode(true));
myWebXML.getDocumentElement().appendChild(adoptNode);
}
}
}
StringWriter writer = new StringWriter();
Result outputTarget = new StreamResult(writer);
;
Transformer newTransformer = TransformerFactory.newInstance()
.newTransformer();
newTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
newTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
newTransformer.transform(new DOMSource(myWebXML), outputTarget);
document.set(writer.toString());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
fileBuffer.commit(monitor, true);
if (dispose) {
FileBuffers.getTextFileBufferManager().disconnect(fullPath,
LocationKind.IFILE, monitor);
}
}