下面列出了怎么用org.eclipse.emf.ecore.resource.impl.BinaryResourceImpl的API类实例代码及写法,或者点击链接到github查看源代码。
protected void readContents(StorageAwareResource resource, InputStream inputStream) throws IOException {
new BinaryResourceImpl.EObjectInputStream(inputStream, Collections.emptyMap()) {
@Override
public int readCompressedInt() throws IOException {
//HACK! null resource set, to avoid usage of resourceSet's package registry
resourceSet = null;
return super.readCompressedInt();
}
@Override
public InternalEObject loadEObject() throws IOException {
InternalEObject result = super.loadEObject();
handleLoadEObject(result, this);
return result;
}
}.loadResource(resource);
}
protected void writeContents(StorageAwareResource storageAwareResource, OutputStream outputStream)
throws IOException {
BinaryResourceImpl.EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(outputStream,
Collections.emptyMap()) {
@Override
public void writeURI(URI uri, String fragment) throws IOException {
URI fullURI = uri.appendFragment(fragment);
URI portableURI = storageAwareResource.getPortableURIs().toPortableURI(storageAwareResource, fullURI);
URI uriToWrite = portableURI == null ? fullURI : portableURI;
super.writeURI(uriToWrite.trimFragment(), uriToWrite.fragment());
}
@Override
public void saveEObject(InternalEObject internalEObject, BinaryResourceImpl.EObjectOutputStream.Check check)
throws IOException {
beforeSaveEObject(internalEObject, this);
super.saveEObject(internalEObject, check);
handleSaveEObject(internalEObject, this);
}
};
try {
out.saveResource(storageAwareResource);
} finally {
out.flush();
}
}
@Test public void testDeserializeAsLowerCase() throws IOException {
QualifiedName upperCase = QualifiedName.create("A", "B");
QualifiedName lowerCase = upperCase.toLowerCase();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
EObjectOutputStream out = new BinaryResourceImpl.EObjectOutputStream(bos, Collections.emptyMap());
upperCase.writeToStream(out);
lowerCase.writeToStream(out);
out.flush();
EObjectInputStream in = new BinaryResourceImpl.EObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), Collections.emptyMap());
QualifiedName readUpperCase = QualifiedName.createFromStream(in);
QualifiedName readLowerCase = QualifiedName.createFromStream(in);
assertEquals(QualifiedName.class.getName(), readUpperCase.getClass().getName());
assertEquals(QualifiedName.class.getName() + "$QualifiedNameLowerCase", readLowerCase.getClass().getName());
assertEquals(upperCase, readUpperCase);
assertEquals(lowerCase, readLowerCase);
}
@Override
protected Object beforeSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream writable) throws IOException {
JvmType _xblockexpression = null;
{
super.beforeSaveEObject(object, writable);
JvmType _xifexpression = null;
if ((object instanceof XComputedTypeReference)) {
_xifexpression = ((XComputedTypeReference)object).getType();
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
@Override
protected void handleSaveEObject(final InternalEObject object, final BinaryResourceImpl.EObjectOutputStream out) throws IOException {
super.handleSaveEObject(object, out);
DocumentationAdapter documentationAdapter = null;
JvmIdentifiableMetaData metaDataAdapter = null;
EList<Adapter> _eAdapters = object.eAdapters();
for (final Adapter adapter : _eAdapters) {
{
if ((adapter instanceof DocumentationAdapter)) {
documentationAdapter = ((DocumentationAdapter)adapter);
}
if ((adapter instanceof JvmIdentifiableMetaData)) {
metaDataAdapter = ((JvmIdentifiableMetaData)adapter);
}
}
}
if ((documentationAdapter != null)) {
out.writeBoolean(true);
out.writeString(documentationAdapter.getDocumentation());
} else {
out.writeBoolean(false);
}
if ((metaDataAdapter != null)) {
out.writeBoolean(true);
out.writeBoolean(metaDataAdapter.isSynthetic());
} else {
out.writeBoolean(false);
}
}
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
RuleNames.ensureAdapterInstalled(grammar);
super.generate(grammar, ctx);
final ResourceSaveIndicator isSaving = new ResourceSaveIndicator();
// create a defensive clone
Grammar copy = deepCopy(grammar, isSaving);
ResourceSet set = copy.eResource().getResourceSet();
// save grammar model
String path;
if (xmlVersion == null) {
path = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
} else {
log.warn("'xmlVersion' has been specified for this "
+ GrammarAccessFragment.class.getSimpleName()
+ ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
path = GrammarUtil.getClasspathRelativePathToXmi(copy);
}
URI uri = URI.createURI(ctx.getOutput().getOutlet(Generator.SRC_GEN).getPath() + "/" + path);
Resource resource = set.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
addAllGrammarsToResource(resource, copy, new HashSet<Grammar>());
isSaving.set(Boolean.TRUE);
Map<String, Object> saveOptions = Maps.newHashMap();
if (resource instanceof XMLResource) {
((XMLResource) resource).setXMLVersion(getXmlVersion());
} else if (resource instanceof BinaryResourceImpl){
saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.TRUE);
}
try {
resource.save(saveOptions);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
isSaving.set(Boolean.FALSE);
}
}
@Test public void testEncodeGuillemet() throws IOException {
String s = "�";
ByteArrayOutputStream out = new ByteArrayOutputStream();
new BinaryResourceImpl.EObjectOutputStream(out, null).writeString(s);
byte[] byteArray = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(byteArray);
String newS = new BinaryResourceImpl.EObjectInputStream(in, null) {
@Override
public String readString() throws IOException {
int length = readCompressedInt();
if (length == -1) {
return null;
} else {
if (characters == null || characters.length < length) {
characters = new char[length];
}
LOOP: for (int i = 0; i < length; ++i) {
byte value = readByte();
if (value == 0) {
do {
characters[i] = readChar();
} while (++i < length);
break LOOP;
} else {
// here's the fix
char charValue = (char) (value & 0xff);
characters[i] = charValue;
}
}
return new String(characters, 0, length);
}
}
}.readString();
assertEquals(s, newS);
}
@Override
protected void readContents(final StorageAwareResource resource, final InputStream inputStream) throws IOException {
// Implementation is copied over from org.eclipse.xtext.resource.persistence.ResourceStorageLoadable
// The only difference is that the stream overrides 'loadFeatureValue' to add some error logging
final BinaryResourceImpl.EObjectInputStream in = new BinaryResourceImpl.EObjectInputStream(inputStream, Collections.emptyMap()) {
@Override
public int readCompressedInt() throws IOException {
// HACK! null resource set, to avoid usage of resourceSet's package registry
resourceSet = null;
return super.readCompressedInt();
}
@Override
public InternalEObject loadEObject() throws IOException {
final InternalEObject result = super.loadEObject();
handleLoadEObject(result, this);
return result;
}
@Override
protected void loadFeatureValue(final InternalEObject internalEObject, final EStructuralFeatureData eStructuralFeatureData) throws IOException {
try {
super.loadFeatureValue(internalEObject, eStructuralFeatureData);
// CHECKSTYLE:OFF
} catch (Exception e) {
StringBuilder infoMessage = new StringBuilder(100);
// CHECKSTYLE:ON
infoMessage.append("Failed to load feature's value. Owner: ").append(internalEObject.eClass()); //$NON-NLS-1$
if (eStructuralFeatureData.eStructuralFeature != null) {
infoMessage.append(", feature name: ").append(eStructuralFeatureData.eStructuralFeature.getName()); //$NON-NLS-1$
}
LOG.info(infoMessage);
throw e;
}
}
};
in.loadResource(resource);
}
protected Object handleLoadEObject(InternalEObject loaded, BinaryResourceImpl.EObjectInputStream input)
throws IOException {
return null;
}
protected Object beforeSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream writable)
throws IOException {
return null;
}
protected void handleSaveEObject(InternalEObject object, BinaryResourceImpl.EObjectOutputStream out)
throws IOException {
}
protected void writeGrammar() {
final Wrapper<Boolean> isSaving = Wrapper.<Boolean>wrap(Boolean.valueOf(false));
final ResourceSet cloneInto = new ResourceSetImpl();
Map<String, Object> _extensionToFactoryMap = cloneInto.getResourceFactoryRegistry().getExtensionToFactoryMap();
FragmentFakingEcoreResource.FactoryImpl _factoryImpl = new FragmentFakingEcoreResource.FactoryImpl(isSaving);
_extensionToFactoryMap.put(
FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX, _factoryImpl);
final ResourceSet resourceSet = EcoreUtil2.<ResourceSet>clone(cloneInto, this.getLanguage().getGrammar().eResource().getResourceSet());
EObject _head = IterableExtensions.<EObject>head(resourceSet.getResource(this.getLanguage().getGrammar().eResource().getURI(), true).getContents());
final Grammar copy = ((Grammar) _head);
String _xifexpression = null;
if ((this.xmlVersion == null)) {
_xifexpression = GrammarUtil.getClasspathRelativePathToBinGrammar(copy);
} else {
String _xblockexpression = null;
{
String _simpleName = GrammarAccessFragment2.class.getSimpleName();
String _plus = ("The property \'xmlVersion\' has been specified for this " + _simpleName);
String _plus_1 = (_plus + ". Therefore, the grammar is persisted as XMI and not as binary. This can be a performance drawback.");
GrammarAccessFragment2.LOG.warn(_plus_1);
_xblockexpression = GrammarUtil.getClasspathRelativePathToXmi(copy);
}
_xifexpression = _xblockexpression;
}
final String path = _xifexpression;
final URI uri = this.getProjectConfig().getRuntime().getSrcGen().getURI(path);
final Resource resource = resourceSet.createResource(uri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
HashSet<Grammar> _hashSet = new HashSet<Grammar>();
this.addAllGrammarsToResource(resource, copy, _hashSet);
isSaving.set(Boolean.valueOf(true));
final Map<String, Object> saveOptions = CollectionLiterals.<String, Object>newHashMap();
if ((resource instanceof XMLResource)) {
String _elvis = null;
if (this.xmlVersion != null) {
_elvis = this.xmlVersion;
} else {
_elvis = "1.0";
}
((XMLResource)resource).setXMLVersion(_elvis);
} else {
if ((resource instanceof BinaryResourceImpl)) {
saveOptions.put(BinaryResourceImpl.OPTION_VERSION, BinaryResourceImpl.BinaryIO.Version.VERSION_1_1);
saveOptions.put(BinaryResourceImpl.OPTION_STYLE_DATA_CONVERTER, Boolean.valueOf(true));
}
}
try {
resource.save(saveOptions);
} catch (final Throwable _t) {
if (_t instanceof IOException) {
final IOException e = (IOException)_t;
GrammarAccessFragment2.LOG.error(e.getMessage(), e);
} else {
throw Exceptions.sneakyThrow(_t);
}
} finally {
isSaving.set(Boolean.valueOf(false));
}
}