| 1 |
/** |
| 2 |
* Copyright (c) 2002-2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.test.xml.rss; |
| 12 |
|
| 13 |
|
| 14 |
import static org.junit.Assert.assertTrue; |
| 15 |
|
| 16 |
import java.io.ByteArrayInputStream; |
| 17 |
import java.io.ByteArrayOutputStream; |
| 18 |
import java.util.Collection; |
| 19 |
import java.util.HashMap; |
| 20 |
import java.util.Iterator; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
import javax.xml.parsers.DocumentBuilder; |
| 24 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 25 |
|
| 26 |
import org.eclipse.emf.common.util.URI; |
| 27 |
import org.eclipse.emf.ecore.EPackage; |
| 28 |
import org.eclipse.emf.ecore.EPackage.Registry; |
| 29 |
import org.eclipse.emf.ecore.resource.Resource; |
| 30 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 31 |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 32 |
import org.eclipse.emf.ecore.util.BasicExtendedMetaData; |
| 33 |
import org.eclipse.emf.ecore.util.ExtendedMetaData; |
| 34 |
import org.eclipse.emf.ecore.xmi.XMLResource; |
| 35 |
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl; |
| 36 |
import org.eclipse.emf.ecore.xmi.util.XMLProcessor; |
| 37 |
import org.eclipse.emf.test.common.TestUtil; |
| 38 |
import org.eclipse.emf.test.xml.AllSuites; |
| 39 |
import org.eclipse.emf.test.xml.xmi.CompareXML; |
| 40 |
import org.eclipse.xsd.XSDDiagnostic; |
| 41 |
import org.eclipse.xsd.XSDDiagnosticSeverity; |
| 42 |
import org.eclipse.xsd.ecore.XSDEcoreBuilder; |
| 43 |
import org.eclipse.xsd.impl.XSDDiagnosticImpl; |
| 44 |
import org.eclipse.xsd.util.XSDResourceFactoryImpl; |
| 45 |
import org.junit.Before; |
| 46 |
import org.junit.Test; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* General tests for RSS reading/writing |
| 52 |
*/ |
| 53 |
public class RSSTests |
| 54 |
{ |
| 55 |
final static String BASE_RSS_URI = "file:///" + TestUtil.getPluginDirectory(AllSuites.PLUGIN_ID) + "/data/rss/"; |
| 56 |
|
| 57 |
protected XSDEcoreBuilder xsdEcoreBuilder; |
| 58 |
protected URI schemaURI; |
| 59 |
protected DocumentBuilder builder; |
| 60 |
|
| 61 |
@Before |
| 62 |
public void setUp() throws Exception |
| 63 |
{ |
| 64 |
xsdEcoreBuilder = new XSDEcoreBuilder(); |
| 65 |
xsdEcoreBuilder.setValidate(true); |
| 66 |
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", new XSDResourceFactoryImpl()); |
| 67 |
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMLResourceFactoryImpl()); |
| 68 |
|
| 69 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 70 |
factory.setNamespaceAware(true); |
| 71 |
builder = factory.newDocumentBuilder(); |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Bug #81512 |
| 76 |
*/ |
| 77 |
@Test |
| 78 |
public void testLoadEMF21_AtomRSS10v11() throws Exception |
| 79 |
{ |
| 80 |
String schema = "atom10v11_simple.xsd"; |
| 81 |
validateSchema(schema); |
| 82 |
loadAndSaveEMF21(schema, "atom10v11_sample.xml"); |
| 83 |
} |
| 84 |
/* |
| 85 |
* Bug #81512 |
| 86 |
*/ |
| 87 |
@Test |
| 88 |
public void testLoadEMF22_XMLProcessor_AtomRSS10v11() throws Exception |
| 89 |
{ |
| 90 |
String schema = "atom10v11_simple.xsd"; |
| 91 |
schemaURI = URI.createURI(BASE_RSS_URI + schema); |
| 92 |
loadAndSaveEMF22_XMLProcessor(schema, "atom10v11_sample.xml"); |
| 93 |
} |
| 94 |
|
| 95 |
protected void validateSchema(String schema) throws Exception { |
| 96 |
// get schema |
| 97 |
schemaURI = URI.createURI(BASE_RSS_URI + schema); |
| 98 |
assertTrue("Schema file is not available - " + BASE_RSS_URI + schema, schemaURI.isFile()); |
| 99 |
|
| 100 |
// this is still the EMF2.1 way |
| 101 |
xsdEcoreBuilder.generate(schemaURI); |
| 102 |
List<XSDDiagnostic> results = xsdEcoreBuilder.getDiagnostics(); |
| 103 |
for (Iterator<XSDDiagnostic> iter = results.iterator(); iter.hasNext();) { |
| 104 |
assertTrue(getXSDDiagnosticMessage((XSDDiagnosticImpl)iter.next()).toString(),false); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// EMF 2.1 way of loading an xml doc using schema |
| 109 |
protected void loadAndSaveEMF21(String schema, String rss) throws Exception |
| 110 |
{ |
| 111 |
HashMap<String, Object> options = new HashMap<String, Object>(); |
| 112 |
ResourceSet rs = new ResourceSetImpl(); |
| 113 |
Registry packageRegistry = rs.getPackageRegistry(); |
| 114 |
@SuppressWarnings("unchecked") Collection<EPackage> packageList = (Collection<EPackage>)(Collection<?>)xsdEcoreBuilder.generate(schemaURI); |
| 115 |
for (EPackage epackage : packageList) |
| 116 |
{ |
| 117 |
String nsURI = epackage.getNsURI(); |
| 118 |
packageRegistry.put(nsURI, epackage); |
| 119 |
} |
| 120 |
ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(packageRegistry); |
| 121 |
options.put(XMLResource.OPTION_EXTENDED_META_DATA, extendedMetaData); |
| 122 |
|
| 123 |
// Must turn on this option when using EMF21 synatax; turned on automatically for generated code and XMLProcessor |
| 124 |
// The option allows <link href="..."> to be serialized w/o losing the href="..." part |
| 125 |
options.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); |
| 126 |
|
| 127 |
//load XML |
| 128 |
Resource resource = rs.createResource(URI.createURI(BASE_RSS_URI + rss)); |
| 129 |
resource.load(options); |
| 130 |
|
| 131 |
// compare with CompareXML |
| 132 |
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064); |
| 133 |
resource.save(outputstream, options); |
| 134 |
CompareXML.compareFiles(builder, BASE_RSS_URI + rss, new ByteArrayInputStream(outputstream.toByteArray())); |
| 135 |
} |
| 136 |
|
| 137 |
// EMF 2.2 way, using XMLProcessor |
| 138 |
protected void loadAndSaveEMF22_XMLProcessor(String schema, String rss) throws Exception |
| 139 |
{ |
| 140 |
// load schema, then XML |
| 141 |
XMLProcessor processor = new XMLProcessor(schemaURI); |
| 142 |
Resource resource = processor.load(BASE_RSS_URI + rss, null); |
| 143 |
|
| 144 |
// compare with CompareXML |
| 145 |
ByteArrayOutputStream outputstream = new ByteArrayOutputStream(2064); |
| 146 |
processor.save(outputstream, resource, null); |
| 147 |
CompareXML.compareFiles(builder, BASE_RSS_URI + rss, new ByteArrayInputStream(outputstream.toByteArray())); |
| 148 |
} |
| 149 |
|
| 150 |
private static StringBuffer getXSDDiagnosticMessage(XSDDiagnosticImpl diagnostic) |
| 151 |
{ |
| 152 |
StringBuffer message = new StringBuffer(); |
| 153 |
XSDDiagnosticSeverity sev = diagnostic.getSeverity(); |
| 154 |
switch (sev.getValue()) |
| 155 |
{ |
| 156 |
case XSDDiagnosticSeverity.WARNING: |
| 157 |
message.append("Warning: "); |
| 158 |
break; |
| 159 |
case XSDDiagnosticSeverity.ERROR: |
| 160 |
message.append("Error: "); |
| 161 |
break; |
| 162 |
case XSDDiagnosticSeverity.FATAL: |
| 163 |
message.append("Fatal Error: "); |
| 164 |
break; |
| 165 |
} |
| 166 |
message.append("Line "); |
| 167 |
message.append(diagnostic.getLine()); |
| 168 |
message.append(" - "); |
| 169 |
message.append(diagnostic.getMessage()); |
| 170 |
return message; |
| 171 |
} |
| 172 |
} |