blob: 6c94d3f94ca7f71799b3c9a4fb2c878c10aa67fd [file] [log] [blame]
package org.eclipse.epp.sfx.archive.ui.internal.model;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Archive {
public static final int PROP_SOURCE_ROOT = 1;
public static final int PROP_COMMAND_LINE = 2;
public static final int PROP_DESTINATION_FILE = 3;
public static final int PROP_ENTRIES = 4;
public static final int PROP_ENTRIES_UNPACK = 5;
private SFXArchiveModel fModel;
private String fSourceRoot;
private String fCommandLine;
private String fDestinationFile;
private List fEntries;
public Archive(SFXArchiveModel model) {
fModel = model;
fEntries = new ArrayList();
}
public void parse(Element element) {
NodeList childs = element.getChildNodes();
for (int i=0; i<childs.getLength(); i++) {
Node child = childs.item(i);
if (child instanceof Element) {
String name = child.getNodeName();
if ("source".equals(name)) {
parseSource((Element) child);
}
else if ("destination".equals(name)) {
parseDestination((Element) child);
}
}
}
}
private void parseDestination(Element element) {
setDestinationFile(element.getAttribute("path"));
setCommandLine(element.getAttribute("cmd"));
}
private void parseSource(Element element) {
setSourceRoot(element.getAttribute("path"));
List newEntries = new ArrayList();
NodeList childs = element.getChildNodes();
for (int i=0; i<childs.getLength(); i++) {
Node child = childs.item(i);
if (child instanceof Element) {
String name = child.getNodeName();
if ("file".equals(name) || "dir".equals(name)) {
Entry entry = new Entry(fModel);
entry.parse((Element) child);
newEntries.add(entry);
}
}
}
setEntries(newEntries);
}
public String getSourceRoot() {
return fSourceRoot;
}
public void setSourceRoot(String sourceRoot) {
fSourceRoot = sourceRoot;
fModel.notifyModelListeners(new ModelChangedEvent(
ModelChangedEvent.TYPE_CHANGE, this, PROP_SOURCE_ROOT));
}
public String getCommandLine() {
return fCommandLine;
}
public void setCommandLine(String commandLine) {
fCommandLine = commandLine;
fModel.notifyModelListeners(new ModelChangedEvent(
ModelChangedEvent.TYPE_CHANGE, this, PROP_COMMAND_LINE));
}
public String getDestinationFile() {
return fDestinationFile;
}
public void setDestinationFile(String destinationFile) {
fDestinationFile = destinationFile;
fModel.notifyModelListeners(new ModelChangedEvent(
ModelChangedEvent.TYPE_CHANGE, this, PROP_DESTINATION_FILE));
}
public List getEntries() {
return fEntries;
}
public void setEntries(List entries) {
fEntries = entries;
fModel.notifyModelListeners(new ModelChangedEvent(
ModelChangedEvent.TYPE_CHANGE, this, PROP_ENTRIES));
}
public void write(StringBuffer buffer) {
buffer.append("<sfxArchive>\n");
boolean notEmptySourceRoot = (fSourceRoot != null && !"".equals(fSourceRoot));
boolean notEmptyDestinationFile = (fDestinationFile != null && !"".equals(fDestinationFile));
boolean notEmptyCommandLine = (fCommandLine != null && !"".equals(fCommandLine));
if (notEmptySourceRoot || !fEntries.isEmpty()) {
buffer.append("\t<source");
if (notEmptySourceRoot)
buffer.append(" path=\"" + fSourceRoot + "\"");
buffer.append(">\n");
for (Iterator it = fEntries.iterator(); it.hasNext(); ) {
((Entry) it.next()).write(buffer);
}
buffer.append("\t</source>\n");
}
if (notEmptyDestinationFile || notEmptyCommandLine) {
if (notEmptySourceRoot || !fEntries.isEmpty())
buffer.append("\n");
buffer.append("\t<destination");
if (notEmptyDestinationFile)
buffer.append(" path=\"" + fDestinationFile + "\"");
if (notEmptyCommandLine)
buffer.append(" cmd=\"" + fCommandLine + "\"");
buffer.append("/>\n");
}
buffer.append("</sfxArchive>\n");
}
public SFXArchiveModel getModel() {
return fModel;
}
}