【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“XMItoXSIType.java”一文,供大家参考学习!
/*
* IBM Confidential OCO Source Material
* 5639-D57 (C) COPYRIGHT International Business Machines Corp. 2002.
* The source code for this program is not published or otherwise divested
* of its trade secrets, irrespective of what has been deposited with the
* U.S. Copyright Office.
*/
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;
/**
* This class converts a configuration document in XML to an appropriate format
* for schema validation. It performs two following tasks.
* 1) In the root element, <xmi:XMI>, add new attribute, xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* 2) Replace all xmi:type attribute name to xsi:type.
*/
public class XMItoXSIType {
private DOMParser parser = new DOMParser();
private String xmlFileName;
private String outputFileName;
private String ATTR_XMLNS_XSI = "xmlns:xsi";
private String ATTRVAL_XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance";
private String ATTR_XMI_TYPE = "xmi:type";
private String ATTR_XSI_TYPE = "xsi:type";
private Document doc;
/**
* Constructor.
*/
public XMItoXSIType(String
xmlFileName) {
this.xmlFileName = xmlFileName;
} // Constructor
/**
* Perform the conversion operation.
*
* @return A converted DOM document. Null if conversion's failed.
*/
public Document convert() {
try {
parser.parse(xmlFileName);
}
catch (Exception e) {
System.err.println("XMItoXSIType: Failed to parse \"" + xmlFileName + "\"");
return null;
}
doc = parser.getDocument();
convertIt(doc);
return doc;
} // convert
/**
* Recursive function.
*/
private void convertIt(Node node) {
if (node == null) {
return;
}
short type = node.getNodeType();
if (type == Node.ELEMENT_NODE) {
Element element = (Element)node;
NamedNodeMap attrs = node.getAttributes();
int noOfAttr = attrs.getLength();
if (element.getTagName().equals("xmi:XMI")) {
boolean foundIt = false;
for (int i=0; i<noOfAttr; i++) {
Attr attr = (Attr)attrs.item(i);
if (attr.getName().equals(ATTR_XMLNS_XSI)) {
foundIt = true;
break;
}
} // for
if (foundIt == false) {
element.setAttribute(ATTR_XMLNS_XSI, ATTRVAL_XMLNS_XSI);
}
}
else {
// Replace all xmi:type to xsi:type.
for (int i=0; i<noOfAttr; i++) {
Attr attr = (Attr)attrs.item(i);
if (attr.getName().equals(ATTR_XMI_TYPE)) {
String attrValue = attr.getValue();
element.removeAttribute(ATTR_XMI_TYPE);
element.setAttribute(ATTR_XSI_TYPE, attrValue);
}
} // for
}
}
Node child = node.getFirstChild();
while (child != null) {
convertIt(child);
child = child.getNextSibling();
} // while
} // convertIt
/**
* Get the converted XML file name.
*/
public String getOutputFileName() {
if (outputFileName == null) {
return null;
}
return new String(outputFileName);
} // getOutputFileName
/**
* Specify the output file name for converted XML file.
*/
public void setOutputFileName(String outputFileName) {
if (outputFileName != null) {
this.outputFileName = new String(outputFileName);
}
} // setOutputFileName
/**
* Serialize DOM to a file.
*/
public void save() throws FileNotFoundException, IOException {
if (doc == null) {
return;
}
if (outputFileName == null) {
outputFileName = xmlFileName + ".tmp";
}
PrintWriter writer = new PrintWriter(new FileOutputStream(outputFileName));
OutputFormat format = new OutputFormat(doc);
XMLSerializer serial = new XMLSerializer(writer, format);
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize(doc.getDocumentElement());
} // save
/**
* Standalone test program.
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java XMItoXSIType <xml file>");
System.exit(-1);
}
XMItoXSIType converter = new XMItoXSIType(args[0]);
converter.convert();
converter.save();
}
} // XMItoXSIType