use of org.bimserver.plugins.deserializers.DeserializeException in project BIMserver by opensourceBIM.
the class SharedJsonDeserializer method read.
@SuppressWarnings("rawtypes")
public IfcModelInterface read(InputStream in, IfcModelInterface model, boolean checkWaitingList) throws DeserializeException {
if (model.getPackageMetaData().getSchemaDefinition() == null) {
throw new DeserializeException("No SchemaDefinition available");
}
WaitingList<Long> waitingList = new WaitingList<Long>();
final boolean log = false;
if (log) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
IOUtils.copy(in, baos);
File file = new File("debug.json");
System.out.println(file.getAbsolutePath());
FileUtils.writeByteArrayToFile(file, baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
in = new ByteArrayInputStream(baos.toByteArray());
}
JsonReader jsonReader = new JsonReader(new InputStreamReader(in, Charsets.UTF_8));
int nrObjects = 0;
try {
JsonToken peek = jsonReader.peek();
if (peek != null && peek == JsonToken.BEGIN_OBJECT) {
jsonReader.beginObject();
peek = jsonReader.peek();
while (peek == JsonToken.NAME) {
String nextName = jsonReader.nextName();
if (nextName.equals("objects")) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
nrObjects++;
processObject(model, waitingList, jsonReader, null);
}
jsonReader.endArray();
} else if (nextName.equals("header")) {
IfcHeader ifcHeader = (IfcHeader) processObject(model, waitingList, jsonReader, StorePackage.eINSTANCE.getIfcHeader());
model.getModelMetaData().setIfcHeader(ifcHeader);
}
peek = jsonReader.peek();
}
jsonReader.endObject();
}
} catch (IOException e) {
LOGGER.error("", e);
} catch (IfcModelInterfaceException e) {
LOGGER.error("", e);
} finally {
LOGGER.debug("# Objects: " + nrObjects);
try {
jsonReader.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
boolean checkUnique = false;
if (checkUnique) {
for (IdEObject idEObject : model.getValues()) {
for (EStructuralFeature eStructuralFeature : idEObject.eClass().getEAllStructuralFeatures()) {
Object value = idEObject.eGet(eStructuralFeature);
if (eStructuralFeature instanceof EReference) {
if (eStructuralFeature.isMany()) {
List list = (List) value;
if (eStructuralFeature.isUnique()) {
Set<Object> t = new HashSet<>();
for (Object v : list) {
if (t.contains(v)) {
// LOGGER.error("NOT UNIQUE " + idEObject.eClass().getName() + "." + eStructuralFeature.getName());
}
t.add(v);
}
}
}
}
}
}
}
if (checkWaitingList && waitingList.size() > 0) {
try {
waitingList.dumpIfNotEmpty();
} catch (BimServerClientException e) {
e.printStackTrace();
}
throw new DeserializeException("Waitinglist should be empty (" + waitingList.size() + ")");
}
return model;
}
use of org.bimserver.plugins.deserializers.DeserializeException in project BIMserver by opensourceBIM.
the class SharedJsonDeserializer method processObject.
@SuppressWarnings({ "rawtypes", "unchecked" })
private IdEObject processObject(IfcModelInterface model, WaitingList<Long> waitingList, JsonReader jsonReader, EClass eClass) throws IOException, DeserializeException, IfcModelInterfaceException {
IdEObjectImpl object = null;
jsonReader.beginObject();
if (jsonReader.nextName().equals("_i")) {
long oid = jsonReader.nextLong();
if (jsonReader.nextName().equals("_t")) {
String type = jsonReader.nextString();
if (eClass == null) {
eClass = model.getPackageMetaData().getEClassIncludingDependencies(type);
}
if (eClass == null) {
throw new DeserializeException("No class found with name " + type);
}
if (model.containsNoFetch(oid)) {
object = (IdEObjectImpl) model.getNoFetch(oid);
} else {
if (eClass == StorePackage.eINSTANCE.getIfcHeader()) {
object = (IdEObjectImpl) StoreFactory.eINSTANCE.createIfcHeader();
} else {
object = (IdEObjectImpl) model.create(eClass, oid);
}
}
if (jsonReader.nextName().equals("_s")) {
int state = jsonReader.nextInt();
if (state == 1) {
object.setLoadingState(State.LOADING);
while (jsonReader.hasNext()) {
String featureName = jsonReader.nextName();
boolean embedded = false;
if (featureName.startsWith("_r")) {
featureName = featureName.substring(2);
} else if (featureName.startsWith("_e")) {
embedded = true;
featureName = featureName.substring(2);
}
EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(featureName);
if (eStructuralFeature == null) {
throw new DeserializeException("Unknown field (" + featureName + ") on class " + eClass.getName());
}
if (eStructuralFeature.isMany()) {
jsonReader.beginArray();
if (eStructuralFeature instanceof EAttribute) {
List list = (List) object.eGet(eStructuralFeature);
List<String> stringList = null;
if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject() || eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
EStructuralFeature asStringFeature = eClass.getEStructuralFeature(eStructuralFeature.getName() + "AsString");
stringList = (List<String>) object.eGet(asStringFeature);
}
while (jsonReader.hasNext()) {
Object e = readPrimitive(jsonReader, eStructuralFeature);
list.add(e);
if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
double val = (Double) e;
// TODO
stringList.add("" + val);
// this
// is
// losing
// precision,
// maybe
// also
// send
// the
// string
// value?
}
}
} else if (eStructuralFeature instanceof EReference) {
int index = 0;
AbstractEList list = (AbstractEList) object.eGet(eStructuralFeature);
while (jsonReader.hasNext()) {
if (embedded) {
JsonToken peek = jsonReader.peek();
if (peek == JsonToken.NUMBER) {
long refOid = jsonReader.nextLong();
processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
} else {
// TODO: another case for two-dimensional arrays
jsonReader.beginObject();
String nextName = jsonReader.nextName();
if (nextName.equals("_t")) {
String t = jsonReader.nextString();
IdEObject wrappedObject = (IdEObject) model.create(model.getPackageMetaData().getEClass(t), -1);
if (jsonReader.nextName().equals("_v")) {
EStructuralFeature wv = wrappedObject.eClass().getEStructuralFeature("wrappedValue");
wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
list.add(wrappedObject);
} else {
throw new DeserializeException("Expected _v");
}
} else if (nextName.equals("_i")) {
// Not all are embedded...
long refOid = jsonReader.nextLong();
if (jsonReader.nextName().equals("_t")) {
String refType = jsonReader.nextString();
IdEObject refObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(refType), refOid);
((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
model.add(refObject.getOid(), refObject);
addToList(eStructuralFeature, index, list, refObject);
((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
} else {
processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
}
}
jsonReader.endObject();
}
} else {
jsonReader.beginObject();
if (jsonReader.nextName().equals("_i")) {
long refOid = jsonReader.nextLong();
if (jsonReader.nextName().equals("_t")) {
String refType = jsonReader.nextString();
EClass referenceEClass = model.getPackageMetaData().getEClassIncludingDependencies(refType);
if (model.getNoFetch(refOid) != null) {
processRef(model, waitingList, object, eStructuralFeature, index, list, refOid);
} else {
IdEObject refObject = (IdEObject) model.create(referenceEClass, refOid);
((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
model.add(refObject.getOid(), refObject);
addToList(eStructuralFeature, index, list, refObject);
((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
}
}
}
jsonReader.endObject();
}
index++;
}
}
jsonReader.endArray();
} else {
if (eStructuralFeature instanceof EAttribute) {
Object x = readPrimitive(jsonReader, eStructuralFeature);
if (eStructuralFeature.getEType() == EcorePackage.eINSTANCE.getEDouble()) {
EStructuralFeature asStringFeature = object.eClass().getEStructuralFeature(eStructuralFeature.getName() + "AsString");
if (asStringFeature != null) {
// TODO
object.eSet(asStringFeature, "" + x);
}
// this
// is
// losing
// precision,
// maybe
// also
// send
// the
// string
// value?
}
object.eSet(eStructuralFeature, x);
} else if (eStructuralFeature instanceof EReference) {
if (eStructuralFeature.getName().equals("GlobalId")) {
IfcGloballyUniqueId globallyUniqueId = Ifc2x3tc1Factory.eINSTANCE.createIfcGloballyUniqueId();
globallyUniqueId.setWrappedValue(jsonReader.nextString());
object.eSet(eStructuralFeature, globallyUniqueId);
} else if (embedded) {
jsonReader.beginObject();
if (jsonReader.nextName().equals("_t")) {
String t = jsonReader.nextString();
IdEObject wrappedObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(t), -1);
((IdEObjectImpl) wrappedObject).setLoadingState(State.LOADING);
if (eStructuralFeature.getEAnnotation("dbembed") != null) {
for (EStructuralFeature eStructuralFeature2 : wrappedObject.eClass().getEAllStructuralFeatures()) {
String fn = jsonReader.nextName();
if (fn.equals(eStructuralFeature2.getName())) {
wrappedObject.eSet(eStructuralFeature2, readPrimitive(jsonReader, eStructuralFeature2));
} else {
throw new DeserializeException(fn + " / " + eStructuralFeature2.getName());
}
}
object.eSet(eStructuralFeature, wrappedObject);
} else {
if (jsonReader.nextName().equals("_v")) {
EStructuralFeature wv = wrappedObject.eClass().getEStructuralFeature("wrappedValue");
wrappedObject.eSet(wv, readPrimitive(jsonReader, wv));
object.eSet(eStructuralFeature, wrappedObject);
}
}
((IdEObjectImpl) wrappedObject).setLoadingState(State.LOADED);
}
jsonReader.endObject();
} else {
jsonReader.beginObject();
if (jsonReader.nextName().equals("_i")) {
long refOid = jsonReader.nextLong();
if (jsonReader.nextName().equals("_t")) {
String refType = jsonReader.nextString();
boolean isInverse = false;
EntityDefinition entityBN = model.getPackageMetaData().getSchemaDefinition().getEntityBN(object.eClass().getName());
if (entityBN != null) {
// Some entities like GeometryInfo/Data are not in IFC schema, but valid
Attribute attributeBN = entityBN.getAttributeBNWithSuper(eStructuralFeature.getName());
if (attributeBN != null) {
if (attributeBN instanceof InverseAttribute) {
isInverse = true;
}
}
}
// if (!isInverse) {
if (model.getNoFetch(refOid) != null) {
object.eSet(eStructuralFeature, model.getNoFetch(refOid));
} else {
IdEObject refObject = (IdEObject) model.create(model.getPackageMetaData().getEClassIncludingDependencies(refType), refOid);
((IdEObjectImpl) refObject).setLoadingState(State.OPPOSITE_SETTING);
model.add(refObject.getOid(), refObject);
object.eSet(eStructuralFeature, refObject);
((IdEObjectImpl) refObject).setLoadingState(State.TO_BE_LOADED);
}
// }
}
}
jsonReader.endObject();
}
}
}
}
object.setLoadingState(State.LOADED);
} else {
// state not_loaded
object.setLoadingState(State.TO_BE_LOADED);
}
if (waitingList.containsKey(oid)) {
waitingList.updateNode(oid, eClass, object);
}
model.add(object.getOid(), object);
} else {
LOGGER.info("_s expected");
}
} else {
LOGGER.info("_t expected");
}
} else {
LOGGER.info("_i expected");
}
jsonReader.endObject();
return object;
}
use of org.bimserver.plugins.deserializers.DeserializeException in project BIMserver by opensourceBIM.
the class ExtractFurniture method main.
public static void main(String[] args) {
try {
Path home = Paths.get("home");
PluginManager pluginManager = LocalDevPluginLoader.createPluginManager(home);
DeserializerPlugin deserializerPlugin = pluginManager.getFirstDeserializer("ifc", Schema.IFC2X3TC1, true);
Deserializer deserializer = deserializerPlugin.createDeserializer(null);
MetaDataManager metaDataManager = new MetaDataManager(home.resolve("tmp"));
PackageMetaData packageMetaData = metaDataManager.getPackageMetaData("ifc2x3tc1");
deserializer.init(packageMetaData);
IfcModelInterface model = DeserializerUtils.readFromFile(deserializer, Paths.get("../TestData/data/ADT-FZK-Haus-2005-2006.ifc"));
model.fixOids(new IncrementingOidProvider());
IfcFurnishingElement picknick = (IfcFurnishingElement) model.getByName(Ifc2x3tc1Package.eINSTANCE.getIfcFurnishingElement(), "Picknik Bank");
IfcModelInterface newModel = new BasicIfcModel(packageMetaData, null);
ModelHelper modelHelper = new ModelHelper(pluginManager.getMetaDataManager(), new HideAllInversesObjectIDM(CollectionUtils.singleSet(Ifc2x3tc1Package.eINSTANCE), pluginManager.getMetaDataManager().getPackageMetaData("ifc2x3tc1")), newModel);
modelHelper.copy(picknick, false);
SerializerPlugin serializerPlugin = pluginManager.getSerializerPlugin("org.bimserver.ifc.step.serializer.IfcStepSerializerPlugin", true);
Serializer serializer = serializerPlugin.createSerializer(null);
serializer.init(newModel, null, true);
SerializerUtils.writeToFile(serializer, Paths.get("test.ifc"));
} catch (PluginException e) {
e.printStackTrace();
} catch (DeserializeException e) {
e.printStackTrace();
} catch (IfcModelInterfaceException e) {
e.printStackTrace();
} catch (SerializerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations