|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SaveTagHandler.java | 50% | 84.2% | 100% | 82.1% |
|
||||||||||||||
| 1 |
package junit.extensions.xml.elements;
|
|
| 2 |
|
|
| 3 |
import junit.extensions.xml.IXMLTestCase;
|
|
| 4 |
import junit.extensions.xml.XMLException;
|
|
| 5 |
|
|
| 6 |
import org.w3c.dom.Element;
|
|
| 7 |
|
|
| 8 |
import java.io.File;
|
|
| 9 |
import java.io.FileOutputStream;
|
|
| 10 |
import java.io.IOException;
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
/**
|
|
| 14 |
* <p>Title: class SaveTagHandler</p>
|
|
| 15 |
* <p>Description: This tag handler saves the XML tree to a filename given</p>
|
|
| 16 |
* <p>Copyright: Copyright (c) 2002</p>
|
|
| 17 |
* <p>Company: JFCUnit project</p>
|
|
| 18 |
* <H3>Tag Name</H3>
|
|
| 19 |
* save
|
|
| 20 |
* <H3>Required Attributes</H3>
|
|
| 21 |
* file - file name to be saved.
|
|
| 22 |
* <H3>Optional Attributes</H3>
|
|
| 23 |
* encoding - encoding to be used.
|
|
| 24 |
* <H3>Examples</H3>
|
|
| 25 |
* <save file="myfile.xml"/>
|
|
| 26 |
* @author Kevin Wilson
|
|
| 27 |
* @version 1.0
|
|
| 28 |
*/
|
|
| 29 |
public class SaveTagHandler extends AbstractTagHandler { |
|
| 30 |
/**
|
|
| 31 |
* Construct the Tag handler.
|
|
| 32 |
* @param element Element describing the save operation.
|
|
| 33 |
* @param testCase The test case.
|
|
| 34 |
*/
|
|
| 35 | 2 |
public SaveTagHandler(final Element element, final IXMLTestCase testCase) {
|
| 36 | 2 |
super(element, testCase);
|
| 37 |
} |
|
| 38 |
|
|
| 39 |
/**
|
|
| 40 |
* Save the data to the filename given.
|
|
| 41 |
* @throws XMLException is thrown if the element cannot be understood.
|
|
| 42 |
*/
|
|
| 43 | 1 |
public void processElement() throws XMLException { |
| 44 | 1 |
validateElement(); |
| 45 |
|
|
| 46 | 1 |
File f = new File(getFileName());
|
| 47 | 1 |
f.delete(); |
| 48 |
|
|
| 49 | 1 |
try {
|
| 50 | 1 |
f.createNewFile(); |
| 51 |
|
|
| 52 | 1 |
FileOutputStream out = new FileOutputStream(f);
|
| 53 | 1 |
junit.extensions.xml.XMLUtil.writeFile( |
| 54 |
getEncoding(), |
|
| 55 |
out, |
|
| 56 |
getElement().getOwnerDocument()); |
|
| 57 |
} catch (IOException ex) {
|
|
| 58 | 0 |
throw new XMLException("Could not write to file:" |
| 59 |
+ f.getAbsolutePath(), ex, |
|
| 60 |
getElement(), |
|
| 61 |
getXMLTestCase().getPropertyCache()); |
|
| 62 |
} |
|
| 63 |
} |
|
| 64 |
|
|
| 65 |
/**
|
|
| 66 |
* Insure that the file attribute is specified.
|
|
| 67 |
* @throws XMLException if the tag file attribute
|
|
| 68 |
* is not specified.
|
|
| 69 |
*/
|
|
| 70 | 1 |
public void validateElement() throws XMLException { |
| 71 |
// do the default validations from the super class
|
|
| 72 | 1 |
super.validateElement();
|
| 73 |
|
|
| 74 |
// check the element tag name
|
|
| 75 | 1 |
this.checkRequiredAttribute(FILE);
|
| 76 |
} |
|
| 77 |
|
|
| 78 |
/**
|
|
| 79 |
* Get the encoding to be used.
|
|
| 80 |
* @return Encoding to be used.
|
|
| 81 |
*/
|
|
| 82 | 1 |
private String getEncoding() {
|
| 83 | 1 |
String encoding = getString(ENCODING); |
| 84 |
|
|
| 85 | 1 |
if ((encoding == null) || (encoding.trim().length() == 0)) { |
| 86 | 1 |
return "UTF-8"; |
| 87 |
} |
|
| 88 |
|
|
| 89 | 0 |
return encoding;
|
| 90 |
} |
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* Return the file name stored in the XML Element.
|
|
| 94 |
* @return file name.
|
|
| 95 |
*/
|
|
| 96 | 1 |
private String getFileName() {
|
| 97 | 1 |
String file = getString(FILE).trim(); |
| 98 |
|
|
| 99 | 1 |
if ((file == null) || (file.length() == 0)) { |
| 100 | 0 |
return null; |
| 101 |
} |
|
| 102 |
|
|
| 103 | 1 |
return file;
|
| 104 |
} |
|
| 105 |
} |
|
| 106 |
|
|
||||||||||