private void WriteEntity(SEntity o) { // sanity check if (this.m_indent > 100) { return; } if (o == null) { return; } Type t = o.GetType(); string hyperlink = "../../schema/" + t.Namespace.ToLower() + "/lexical/" + t.Name.ToLower() + ".htm"; this.WriteStartElementEntity(t.Name, hyperlink); bool close = this.WriteEntityAttributes(o); if (close) { this.WriteEndElementEntity(t.Name); } else { this.WriteCloseElementEntity(); } }
private void WriteReference(XmlWriter writer, SEntity r) { writer.WriteStartElement(r.GetType().Name); writer.WriteStartAttribute("xsi", "nil", null); writer.WriteValue(true); writer.WriteEndAttribute(); writer.WriteStartAttribute("ref"); writer.WriteValue("i" + r.OID.ToString()); writer.WriteEndAttribute(); writer.WriteEndElement(); }
public string FormatData(DocPublication docPublication, DocExchangeDefinition docExchange, Dictionary <string, DocObject> map, Dictionary <long, SEntity> instances) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); if (instances.Count > 0) { SEntity rootproject = null; foreach (SEntity ent in instances.Values) { if (ent.GetType().Name.Equals("IfcProject")) { rootproject = ent; break; } } if (rootproject != null) { Type type = rootproject.GetType(); DataContractJsonSerializer contract = new DataContractJsonSerializer(type); try { contract.WriteObject(stream, rootproject); } catch (Exception xx) { //... xx.ToString(); } } } stream.Position = 0; System.IO.TextReader reader = new System.IO.StreamReader(stream); string content = reader.ReadToEnd(); return(content); }
/// <summary> /// Gets value referenced by path. /// </summary> /// <param name="target">The relative object to retrieve the value.</param> /// <param name="parameters">Optional parameters for substitution.</param> /// <returns>The value on the object along the expression path.</returns> public object GetValue(SEntity target, Dictionary<string, SEntity> parameters) { if (target == null) throw new ArgumentNullException("target"); if (this.m_type == null) { return target.GetType(); } if (!this.m_type.IsInstanceOfType(target)) return null; // doesn't apply if (this.m_property == null) { return target; // for general case, if no attribute specified, then return object itself } object value = null; if (this.m_property.PropertyType.IsGenericType && typeof(System.Collections.IList).IsAssignableFrom(this.m_property.PropertyType))// && // (typeof(SEntity).IsAssignableFrom(this.m_property.PropertyType.GetGenericArguments()[0]) || this.m_property.PropertyType.GetGenericArguments()[0].IsInterface)) { System.Collections.IList list = (System.Collections.IList)this.m_property.GetValue(target, null); // if expecting array, then return it. if (this.m_vector || (this.m_identifier == null && this.m_inner == null)) { return list; } else if (this.m_identifier != null && this.m_identifier.StartsWith("@") && parameters == null) { // return filtered list based on expected type -- may be none if no compatible types -- e.g. COBie properties only return IfcPropertyEnumeratedValue if (this.InnerPath != null && this.InnerPath.Type != null) { List<SEntity> listFilter = null; foreach (SEntity ent in list) { if (this.InnerPath.Type.IsInstanceOfType(ent)) { if (listFilter == null) { listFilter = new List<SEntity>(); } listFilter.Add(ent); } } return listFilter; } else { return list; } } if (list != null) { int listindex = 0; // identify by 1-based numeric index within list if(!String.IsNullOrEmpty(this.m_identifier) && Int32.TryParse(this.m_identifier, out listindex) && listindex > 0 && listindex <= list.Count) { object eachelem = list[listindex - 1]; if (this.m_inner != null && eachelem is SEntity) { object eachvalue = this.m_inner.GetValue((SEntity)eachelem, parameters); if (eachvalue != null) { return eachvalue; } } else { return eachelem; } } foreach (object eachelem in list) { // derived class may have its own specific property (e.g. IfcSIUnit, IfcConversionBasedUnit) if (!String.IsNullOrEmpty(this.m_identifier)) { Type eachtype = eachelem.GetType(); // special cases for properties and quantities if (eachtype.Name.Equals("IfcRelDefinesByProperties")) { FieldInfo fieldRelatingPropertyDefinition = eachtype.GetField("RelatingPropertyDefinition"); object ifcPropertySet = fieldRelatingPropertyDefinition.GetValue(eachelem); if (ifcPropertySet != null) { Type typePropertySet = ifcPropertySet.GetType(); FieldInfo fieldName = typePropertySet.GetField("Name"); object ifcLabel = fieldName.GetValue(ifcPropertySet); if (ifcLabel != null) { FieldInfo fieldValue = ifcLabel.GetType().GetField("Value"); if (fieldValue != null) { string sval = fieldValue.GetValue(ifcLabel) as string; if (this.m_identifier.Equals(sval)) { // matches! if (this.m_inner != null) { object eachvalue = this.m_inner.GetValue((SEntity)eachelem, parameters); if (eachvalue != null) { return eachvalue; } } else { return eachelem; } } } } } } else { // fall back on Name field for properties or quantities FieldInfo fieldName = eachtype.GetField("Name"); if (fieldName != null) { object ifcLabel = fieldName.GetValue(eachelem); if (ifcLabel != null) { FieldInfo fieldValue = ifcLabel.GetType().GetField("Value"); if (fieldValue != null) { string sval = fieldValue.GetValue(ifcLabel) as string; if (this.m_identifier.Equals(sval)) { // matches! if (this.m_inner != null) { object eachvalue = this.m_inner.GetValue((SEntity)eachelem, parameters); if (eachvalue != null) { return eachvalue; } } else { return eachelem; } } } } } } } else { // use first non-null item within inner reference if (this.m_inner != null && eachelem is SEntity) { object eachvalue = this.m_inner.GetValue((SEntity)eachelem, parameters); if (eachvalue != null) { return eachvalue; } } else { return eachelem; } } } return null; // not found } } else if (this.m_inner != null) { value = this.m_property.GetValue(target, null); if (value is SEntity) { value = this.m_inner.GetValue((SEntity)value, parameters); if (this.m_identifier != null && value != null) { // qualify the value Type eachtype = value.GetType(); DefaultMemberAttribute[] attrs = (DefaultMemberAttribute[])eachtype.GetCustomAttributes(typeof(DefaultMemberAttribute), true); PropertyInfo propElem = null; if (attrs.Length > 0) { propElem = eachtype.GetProperty(attrs[0].MemberName); } else { propElem = eachtype.GetProperty("Name"); } if (propElem != null) { object name = propElem.GetValue(value, null); if (name == null || !this.m_identifier.Equals(name.ToString())) { return null; } } } } } else { value = this.m_property.GetValue(target, null); } return value; }
/// <summary> /// Returns true if any elements written (requiring closing tag); or false if not /// </summary> /// <param name="o"></param> /// <returns></returns> private bool WriteEntityAttributes(SEntity o) { Type t = o.GetType(); long oid = 0; if (this.m_saved.Contains(o)) { // give it an ID if needed (first pass) if (!this.m_idmap.TryGetValue(o, out oid)) { this.m_nextID++; this.m_idmap[o] = this.m_nextID; } // reference existing; return this.WriteReference(oid); return(false); } // mark as saved this.m_saved.Add(o); if (this.m_idmap.TryGetValue(o, out oid)) { this.WriteIdentifier(oid); } bool previousattribute = false; // write fields as attributes bool haselements = false; IList <FieldInfo> fields = SEntity.GetFieldsAll(t); foreach (FieldInfo f in fields) { DocXsdFormat xsdformat = this.GetXsdFormat(f); if (f.IsDefined(typeof(DataMemberAttribute)) && (xsdformat == null || (xsdformat.XsdFormat != DocXsdFormatEnum.Element && xsdformat.XsdFormat != DocXsdFormatEnum.Attribute))) { // direct field Type ft = f.FieldType; bool isvaluelist = (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].IsValueType); bool isvaluelistlist = (ft.IsGenericType && // e.g. IfcTriangulatedFaceSet.Normals ft.GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].IsGenericType && ft.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].GetGenericArguments()[0].IsValueType); if (isvaluelistlist || isvaluelist || ft.IsValueType) { object v = f.GetValue(o); if (v != null) { if (previousattribute) { this.WriteAttributeDelimiter(); } previousattribute = true; this.WriteStartAttribute(f.Name); if (isvaluelistlist) { ft = ft.GetGenericArguments()[0].GetGenericArguments()[0]; FieldInfo fieldValue = ft.GetField("Value"); System.Collections.IList list = (System.Collections.IList)v; for (int i = 0; i < list.Count; i++) { System.Collections.IList listInner = (System.Collections.IList)list[i]; for (int j = 0; j < listInner.Count; j++) { if (i > 0 || j > 0) { this.m_writer.Write(" "); } object elem = listInner[j]; if (elem != null) // should never be null, but be safe { elem = fieldValue.GetValue(elem); string encodedvalue = System.Security.SecurityElement.Escape(elem.ToString()); this.m_writer.Write(encodedvalue); } } } } else if (isvaluelist) { ft = ft.GetGenericArguments()[0]; FieldInfo fieldValue = ft.GetField("Value"); System.Collections.IList list = (System.Collections.IList)v; for (int i = 0; i < list.Count; i++) { if (i > 0) { this.m_writer.Write(" "); } object elem = list[i]; if (elem != null) // should never be null, but be safe { elem = fieldValue.GetValue(elem); if (elem is byte[]) { // IfcPixelTexture.Pixels byte[] bytes = (byte[])elem; char[] s_hexchar = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; StringBuilder sb = new StringBuilder(bytes.Length * 2); for (int z = 0; z < bytes.Length; z++) { byte b = bytes[z]; sb.Append(s_hexchar[b / 0x10]); sb.Append(s_hexchar[b % 0x10]); } v = sb.ToString(); this.m_writer.Write(v); } else { string encodedvalue = System.Security.SecurityElement.Escape(elem.ToString()); this.m_writer.Write(encodedvalue); } } } } else { if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(Nullable <>)) { // special case for Nullable types ft = ft.GetGenericArguments()[0]; } Type typewrap = null; while (ft.IsValueType && !ft.IsPrimitive) { FieldInfo fieldValue = ft.GetField("Value"); if (fieldValue != null) { v = fieldValue.GetValue(v); if (typewrap == null) { typewrap = ft; } ft = fieldValue.FieldType; } else { break; } } if (ft.IsEnum || ft == typeof(bool)) { v = v.ToString().ToLowerInvariant(); } if (v is System.Collections.IList) { // IfcCompoundPlaneAngleMeasure System.Collections.IList list = (System.Collections.IList)v; for (int i = 0; i < list.Count; i++) { if (i > 0) { this.m_writer.Write(" "); } object elem = list[i]; if (elem != null) // should never be null, but be safe { string encodedvalue = System.Security.SecurityElement.Escape(elem.ToString()); this.m_writer.Write(encodedvalue); } } } else if (v != null) { string encodedvalue = System.Security.SecurityElement.Escape(v.ToString()); m_writer.Write(encodedvalue); //... escape it } } this.WriteEndAttribute(); } } else { haselements = true; } } else { // inverse haselements = true; } } if (haselements) { bool open = false; // write direct object references and lists foreach (FieldInfo f in fields) { DocXsdFormat format = GetXsdFormat(f); if (f.IsDefined(typeof(DataMemberAttribute)) && (format == null || (format.XsdFormat != DocXsdFormatEnum.Element && format.XsdFormat != DocXsdFormatEnum.Attribute))) { Type ft = f.FieldType; bool isvaluelist = (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].IsValueType); bool isvaluelistlist = (ft.IsGenericType && // e.g. IfcTriangulatedFaceSet.Normals ft.GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].IsGenericType && ft.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(List <>) && ft.GetGenericArguments()[0].GetGenericArguments()[0].IsValueType); // hide fields where inverse attribute used instead if (!f.FieldType.IsValueType && !isvaluelist && !isvaluelistlist && (format == null || (format.XsdFormat != DocXsdFormatEnum.Hidden))) { object value = f.GetValue(o); if (value != null) { if (!open) { WriteOpenElement(); open = true; } if (previousattribute) { this.WriteAttributeDelimiter(); } previousattribute = true; WriteAttribute(o, f, format); } } } else if (format != null && (format.XsdFormat == DocXsdFormatEnum.Element || format.XsdFormat == DocXsdFormatEnum.Attribute)) { object value = f.GetValue(o); if (value != null) { if (!open) { WriteOpenElement(); open = true; } if (previousattribute) { this.WriteAttributeDelimiter(); } previousattribute = true; WriteAttribute(o, f, format); } } else { object value = f.GetValue(o); // inverse // record it for downstream serialization if (value is System.Collections.IList) { System.Collections.IList invlist = (System.Collections.IList)value; foreach (SEntity invobj in invlist) { if (!this.m_saved.Contains(invobj)) { this.m_queue.Enqueue(invobj); } } } } } this.WriteAttributeTerminator(); return(open); } else { this.WriteAttributeTerminator(); return(false); } }
private void WriteEntity(SEntity o) { // sanity check if (this.m_indent > 100) { return; } Type t = o.GetType(); string hyperlink = "../../schema/" + t.Namespace.ToLower() + "/lexical/" + t.Name.ToLower() + ".htm"; this.WriteStartElement(t.Name, hyperlink); #if false // id if (gen != null) { bool firstTime; long id = gen.GetId(o, out firstTime); writer.WriteAttributeString("id", "i" + id.ToString()); } #endif /* * writer.WriteStartAttribute("id"); * writer.WriteValue("i" + id.ToString()); * writer.WriteEndAttribute();*/ // write fields as attributes bool haselements = false; IList <FieldInfo> fields = SEntity.GetFieldsOrdered(t); foreach (FieldInfo f in fields) { object v = f.GetValue(o); if (v != null) { if (f.FieldType.IsValueType) { Type ft = f.FieldType; if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(Nullable <>)) { // special case for Nullable types ft = ft.GetGenericArguments()[0]; } Type typewrap = null; while (ft.IsValueType && !ft.IsPrimitive) { FieldInfo fieldValue = ft.GetField("Value"); if (fieldValue != null) { v = fieldValue.GetValue(v); if (typewrap == null) { typewrap = ft; } ft = fieldValue.FieldType; } else { break; } } if (v != null) { string encodedvalue = System.Security.SecurityElement.Escape(v.ToString()); m_writer.Write(" "); m_writer.Write(f.Name); m_writer.Write("=\""); m_writer.Write(encodedvalue); //... escape it m_writer.Write("\""); } } else { haselements = true; } } } IList <FieldInfo> inverses = SEntity.GetFieldsInverse(t); if (haselements || inverses.Count > 0) { WriteOpenElement(); // write direct object references and lists foreach (FieldInfo f in fields) { DocXsdFormat format = GetXsdFormat(f); // hide fields where inverse attribute used instead if (!f.FieldType.IsValueType && (format == null || (format.XsdFormat != DocXsdFormatEnum.Hidden))) { WriteAttribute(o, f); } } // write inverse object references and lists foreach (FieldInfo f in inverses) { DocXsdFormat format = GetXsdFormat(f); if (format != null && format.XsdFormat == DocXsdFormatEnum.Element) //... check this { WriteAttribute(o, f); //... careful - don't write back-references... } } WriteEndElement(t.Name); } else { // close opening tag if (this.m_markup) { this.m_writer.WriteLine("/><br/>"); } else { this.m_writer.WriteLine("/>"); } this.m_indent--; } }