public LookupNamespace ( String prefix ) : String | ||
prefix | String | |
return | String |
private void ParseStateVarXml(String evented, String multicasted, XmlTextReader XMLDoc) { // if (XML=="") // { // return; // } string ComplexType = ""; string ComplexTypeNS = ""; UPnPComplexType CT = null; DText P = new DText(); P.ATTRMARK = ":"; string minval = null; string maxval = null; string stepval = null; bool HasDef = false; string name = ""; string DataType = ""; string DefaultValue = null; ArrayList allowedValueList = new ArrayList(); string LocalName = ""; string lname2 = ""; // StringReader MyString = new StringReader(XML); // XmlTextReader XMLDoc = new XmlTextReader(MyString); // XMLDoc.Read(); // XMLDoc.MoveToContent(); bool done = false; while (!done && XMLDoc.Read()) { switch (XMLDoc.NodeType) { case XmlNodeType.Element: LocalName = XMLDoc.LocalName; switch (XMLDoc.LocalName) { case "dataType": if (XMLDoc.HasAttributes) { for (int i = 0; i < XMLDoc.AttributeCount; i++) { XMLDoc.MoveToAttribute(i); if (XMLDoc.LocalName == "type") { P[0] = XMLDoc.Value; if (P.DCOUNT() == 1) { ComplexType = P[1]; } else { ComplexType = P[2]; ComplexTypeNS = P[1]; } CT = (UPnPComplexType)ComplexTypeTable[ComplexType + ":" + XMLDoc.LookupNamespace(ComplexTypeNS)]; } } } break; case "allowedValueList": bool done2 = false; while (!done2 && XMLDoc.Read()) { switch (XMLDoc.NodeType) { case XmlNodeType.Element: lname2 = XMLDoc.LocalName; break; case XmlNodeType.EndElement: if (XMLDoc.LocalName == "allowedValueList") { done2 = true; } break; case XmlNodeType.Text: if (lname2 == "allowedValue") { allowedValueList.Add(XMLDoc.Value); } break; } } break; case "allowedValueRange": bool done3 = false; while (!done3 && XMLDoc.Read()) { switch (XMLDoc.NodeType) { case XmlNodeType.Element: lname2 = XMLDoc.LocalName; break; case XmlNodeType.EndElement: if (XMLDoc.LocalName == "allowedValueRange") { done3 = true; } break; case XmlNodeType.Text: switch (lname2) { case "minimum": minval = XMLDoc.Value; break; case "maximum": maxval = XMLDoc.Value; break; case "step": stepval = XMLDoc.Value; break; } break; } } break; } break; case XmlNodeType.EndElement: if (XMLDoc.LocalName == "stateVariable") { done = true; } break; case XmlNodeType.Text: switch (LocalName) { case "name": name = XMLDoc.Value.Trim(); break; case "dataType": DataType = XMLDoc.Value.Trim(); break; case "defaultValue": DefaultValue = XMLDoc.Value; HasDef = true; break; } break; } } UPnPStateVariable var; if (CT == null) { var = new UPnPStateVariable(name); } else { var = new UPnPStateVariable(name, CT); } var.ParentService = this; if (evented == "yes") { var.SendEvent = true; } if (multicasted == "yes") { var.MulticastEvent = true; } var.VarType = DataType; if (allowedValueList.Count > 0) { var.AllowedStringValues = (string[])allowedValueList.ToArray(typeof(string)); } if (HasDef) { var.DefaultValue = UPnPService.CreateObjectInstance(var.GetNetType(), DefaultValue); } if ((minval != null) && (maxval != null)) { Object stepobj = null; if (stepval != null) { try { stepobj = UPnPService.CreateObjectInstance(var.GetNetType(), stepval); } catch (Exception) { stepobj = null; } } object MIN; object MAX; try { MIN = UPnPService.CreateObjectInstance(var.GetNetType(), minval); } catch (Exception) { FieldInfo mi = var.GetNetType().GetField("MinValue"); MIN = mi.GetValue(null); } try { MAX = UPnPService.CreateObjectInstance(var.GetNetType(), maxval); } catch (Exception) { FieldInfo mi = var.GetNetType().GetField("MaxValue"); MAX = mi.GetValue(null); } var.SetRange(MIN, MAX, stepobj); } //StateVariables.Add(name,var); StateVariables[name] = var; // TempPatch Only /* IDictionaryEnumerator en = RemoteMethods.GetEnumerator(); UPnPAction a; while(en.MoveNext()) { a = (UPnPAction)en.Value; for(int x =0;x<a.ArgumentList.Length;++x) { if (a.ArgumentList[x].RelatedStateVar.Name == name) { a.ArgumentList[x].RelatedStateVar.VarType = DataType; a.ArgumentList[x].RelatedStateVar.AllowedStringValues = var.AllowedStringValues; a.ArgumentList[x].RelatedStateVar.DefaultValue = DefaultValue; } } }*/ }
private static void ParseDevice(String XML, int startLine, ref UPnPDevice RetVal) { string TempString; UPnPService service; int embeddedLine; DText p = new DText(); TextReader MyString = new StringReader(XML); XmlTextReader XMLDoc = new XmlTextReader(MyString); try { XMLDoc.Read(); XMLDoc.MoveToContent(); if (XMLDoc.LocalName == "device") { if (XMLDoc.AttributeCount > 0) { for (int ax = 0; ax < XMLDoc.AttributeCount; ++ax) { XMLDoc.MoveToAttribute(ax); if (XMLDoc.LocalName == "MaxVersion") { RetVal.SetVersion(XMLDoc.Value); } } XMLDoc.MoveToContent(); XMLDoc.Read(); } else { XMLDoc.Read(); XMLDoc.MoveToContent(); } while (XMLDoc.LocalName != "device" && !XMLDoc.EOF) { switch (XMLDoc.LocalName) { case "deviceList": embeddedLine = XMLDoc.LineNumber; ParseDeviceList(XMLDoc.ReadOuterXml(), startLine + embeddedLine - 1, ref RetVal); break; case "URLBase": // Here, tport is a hack to make Windows Home Server visible. WHS does no set the port in the BaseURL and so, you need to keep it from the SSDP message. int tport = 80; if (RetVal.BaseURL != null) tport = RetVal.BaseURL.Port; RetVal.BaseURL = new Uri(XMLDoc.ReadString()); if (RetVal.BaseURL.Port == 80 && RetVal.BaseURL.Port != tport) RetVal.BaseURL = new Uri(RetVal.BaseURL.Scheme + "://" + RetVal.BaseURL.Host + ":" + tport + RetVal.BaseURL.AbsolutePath); break; case "deviceType": RetVal.DeviceURN = XMLDoc.ReadString(); break; case "friendlyName": RetVal.FriendlyName = XMLDoc.ReadString(); break; case "manufacturer": RetVal.Manufacturer = XMLDoc.ReadString(); break; case "manufacturerURL": RetVal.ManufacturerURL = XMLDoc.ReadString(); break; case "modelDescription": RetVal.ModelDescription = XMLDoc.ReadString(); break; case "modelName": RetVal.ModelName = XMLDoc.ReadString(); break; case "modelNumber": RetVal.ModelNumber = XMLDoc.ReadString(); break; case "modelURL": try { string u = XMLDoc.ReadString(); if (Uri.TryCreate(u, UriKind.Absolute, out RetVal.ModelURL) == false) { Uri.TryCreate("http://" + u, UriKind.Absolute, out RetVal.ModelURL); } } catch (Exception ex) { OpenSource.Utilities.EventLogger.Log(ex); } break; case "serialNumber": RetVal.SerialNumber = XMLDoc.ReadString(); break; case "UDN": TempString = XMLDoc.ReadString(); RetVal.UniqueDeviceName = TempString.Substring(5); break; case "UPC": RetVal.ProductCode = XMLDoc.ReadString(); break; case "presentationURL": RetVal.HasPresentation = true; RetVal.PresentationURL = XMLDoc.ReadString(); break; case "serviceList": if (XMLDoc.IsEmptyElement) break; XMLDoc.Read(); XMLDoc.MoveToContent(); while (XMLDoc.LocalName != "serviceList") { if (XMLDoc.LocalName == "service") { embeddedLine = XMLDoc.LineNumber; service = UPnPService.Parse(XMLDoc.ReadOuterXml(), embeddedLine - 1 + startLine); RetVal.AddService(service); } if (!XMLDoc.IsStartElement()) { if (XMLDoc.LocalName != "serviceList") { XMLDoc.Read(); XMLDoc.MoveToContent(); } } } break; /* case "iconList": bool finishedIconList = false; while (!finishedIconList && XMLDoc.Read()) { switch (XMLDoc.NodeType) { case XmlNodeType.Element: if (XMLDoc.LocalName == "icon") { embeddedLine = XMLDoc.LineNumber; ParseIconXML(RetVal, startLine + embeddedLine-1, XMLDoc.ReadOuterXml()); if (XMLDoc.NodeType == XmlNodeType.EndElement && XMLDoc.LocalName == "iconList") { finishedIconList = true; } } break; case XmlNodeType.EndElement: if (XMLDoc.LocalName == "iconList") { finishedIconList = true; } break; } } break; */ default: if (XMLDoc.LocalName != "") { string customPrefix = XMLDoc.Prefix; string customFieldName = XMLDoc.LocalName; string customFieldNamespace = XMLDoc.LookupNamespace(customPrefix); string customFieldVal = XMLDoc.ReadInnerXml(); RetVal.AddCustomFieldInDescription(customFieldName, customFieldVal, customFieldNamespace); } else { XMLDoc.Skip(); } continue; } XMLDoc.Read(); //XMLDoc.MoveToContent(); } } } catch (XMLParsingException ex) { throw ex; } catch (Exception ex) { throw new XMLParsingException("Invalid Device XML", startLine + XMLDoc.LineNumber, XMLDoc.LinePosition, ex); } }
public void EmptyElementInNamespace () { string xml = @"<foo:bar xmlns:foo='http://foo/' />"; XmlReader xmlReader = new XmlTextReader (new StringReader (xml)); AssertStartDocument (xmlReader); AssertNode ( xmlReader, // xmlReader XmlNodeType.Element, // nodeType 0, // depth true, // isEmptyElement "foo:bar", // name "foo", // prefix "bar", // localName "http://foo/", // namespaceURI String.Empty, // value 1 // attributeCount ); AssertAttribute ( xmlReader, // xmlReader "xmlns:foo", // name "xmlns", // prefix "foo", // localName "http://www.w3.org/2000/xmlns/", // namespaceURI "http://foo/" // value ); AssertEquals ("http://foo/", xmlReader.LookupNamespace ("foo")); AssertEndDocument (xmlReader); }
private static string ExpandNamespace( XmlTextReader reader ) { string propName = reader.Name; string propNamespace = reader.LookupNamespace( reader.Prefix ); int namespaceBreak = propName.IndexOf( XmlNamespaceSeperator ); string expandedName; if( namespaceBreak < 0 ) { expandedName = propName; } else { expandedName = propNamespace + propName.Substring( namespaceBreak + 1 ); } return( expandedName ); }
// This method validates xsi:type="dcterms:W3CDTF" // The valude of xsi:type is a qualified name. It should have a prefix that matches // the xml namespace (ns) within the scope and the name that matches name // The comparisons should be case-sensitive comparisons internal static void ValidateXsiType(XmlTextReader reader, Object ns, string name) { // Get the value of xsi;type String typeValue = reader.GetAttribute(PackageXmlStringTable.GetXmlString(PackageXmlEnum.Type), PackageXmlStringTable.GetXmlString(PackageXmlEnum.XmlSchemaInstanceNamespace)); // Missing xsi:type if (typeValue == null) { throw new XmlException(SR.Get(SRID.UnknownDCDateTimeXsiType, reader.Name), null, reader.LineNumber, reader.LinePosition); } int index = typeValue.IndexOf(':'); // The valude of xsi:type is not a qualified name if (index == -1) { throw new XmlException(SR.Get(SRID.UnknownDCDateTimeXsiType, reader.Name), null, reader.LineNumber, reader.LinePosition); } // Check the following conditions // The namespace of the prefix (string before ":") matches "ns" // The name (string after ":") matches "name" if (!Object.ReferenceEquals(ns, reader.LookupNamespace(typeValue.Substring(0, index))) || String.CompareOrdinal(name, typeValue.Substring(index + 1, typeValue.Length - index - 1)) != 0) { throw new XmlException(SR.Get(SRID.UnknownDCDateTimeXsiType, reader.Name), null, reader.LineNumber, reader.LinePosition); } }
private static ItemCollection ParseComplexType_SequenceChoice(XmlTextReader X) { bool done = false; ItemCollection RetVal = null; string elementName = X.LocalName; DText p = new DText(); p.ATTRMARK = ":"; if (X.LocalName == "choice") { RetVal = new Choice(); } else { RetVal = new Sequence(); } if (X.HasAttributes) { for (int i = 0; i < X.AttributeCount; i++) { X.MoveToAttribute(i); switch (X.LocalName) { case "minOccurs": RetVal.MinOccurs = X.Value; break; case "maxOccurs": RetVal.MaxOccurs = X.Value; break; } } X.MoveToElement(); } X.Read(); do { switch (X.NodeType) { case XmlNodeType.Element: switch (X.LocalName) { case "group": if (X.HasAttributes) { for (int i = 0; i < X.AttributeCount; i++) { X.MoveToAttribute(i); switch (X.LocalName) { case "ref": string sample = X.Value; break; } } X.MoveToElement(); } break; case "sequence": case "choice": RetVal.AddCollection(ParseComplexType_SequenceChoice(X)); break; case "element": RetVal.AddContentItem(new Element()); if (X.HasAttributes) { for (int i = 0; i < X.AttributeCount; i++) { X.MoveToAttribute(i); switch (X.LocalName) { case "name": RetVal.CurrentItem.Name = X.Value; break; case "type": p[0] = X.Value; if (p.DCOUNT() == 1) { RetVal.CurrentItem.Type = X.Value; RetVal.CurrentItem.TypeNS = X.LookupNamespace(""); } else { RetVal.CurrentItem.Type = p[2]; RetVal.CurrentItem.TypeNS = X.LookupNamespace(p[1]); } break; case "minOccurs": RetVal.CurrentItem.MinOccurs = X.Value; break; case "maxOccurs": RetVal.CurrentItem.MaxOccurs = X.Value; break; } } X.MoveToElement(); } break; case "attribute": break; } break; case XmlNodeType.EndElement: if (X.LocalName == elementName) { done = true; } break; case XmlNodeType.Text: break; } } while(!done && X.Read()); return(RetVal); }
private static UPnPComplexType ParseComplexType(XmlTextReader X, UPnPComplexType RetVal) { string elementName = X.LocalName; int count = 0; bool done = false; DText P = new DText(); P.ATTRMARK = ":"; RetVal.AddContainer(new GenericContainer()); do { switch (X.NodeType) { case XmlNodeType.Element: switch (X.LocalName) { case "complexType": case "group": ++count; if (X.HasAttributes) { for (int i = 0; i < X.AttributeCount; i++) { X.MoveToAttribute(i); if (X.Name == "name") { P[0] = X.Value; if (P.DCOUNT() == 1) { RetVal.LocalName = X.Value; RetVal.NameSpace = X.LookupNamespace(""); } else { RetVal.LocalName = P[2]; RetVal.NameSpace = X.LookupNamespace(P[1]); } } else if (X.Name == "ref") { // NOP } } X.MoveToElement(); } break; case "sequence": case "choice": RetVal.CurrentContainer.AddCollection(ParseComplexType_SequenceChoice(X)); //ParseComplexType_Sequence(X,RetVal); break; case "complexContent": RetVal.AddContainer(new ComplexContent()); break; case "simpleContent": RetVal.AddContainer(new SimpleContent()); break; case "restriction": Restriction r = new Restriction(); if (RetVal.CurrentContainer.GetType() == typeof(ComplexContent)) { ((ComplexContent)RetVal.CurrentContainer).RestExt = r; } else if (RetVal.CurrentContainer.GetType() == typeof(SimpleContent)) { ((SimpleContent)RetVal.CurrentContainer).RestExt = r; } if (X.HasAttributes) { for (int i = 0; i < X.AttributeCount; i++) { X.MoveToAttribute(i); if (X.Name == "base") { P[0] = X.Value; if (P.DCOUNT() == 1) { r.baseType = X.Value; r.baseTypeNS = X.LookupNamespace(""); } else { r.baseType = P[2]; r.baseTypeNS = X.LookupNamespace(P[1]); } } } X.MoveToElement(); } break; } break; case XmlNodeType.EndElement: if (X.LocalName == elementName) { --count; if (count == 0) { done = true; } } break; case XmlNodeType.Text: break; } } while(!done && X.Read()); return(RetVal); }
// Resolve a namespace in the scope of the current element. public override String LookupNamespace(String prefix) { return(reader.LookupNamespace(prefix)); }
/// <summary> /// Parses an xml to a UPnP device (either a root or an embedded device) /// </summary> /// <param name="XML">xml containing the device description</param> /// <param name="RetVal">the UPnP device into which the xml data will be parsed</param> private static void ParseDevice(String XML, ref UPnPDevice RetVal) { string TempString; UPnPService service; DText p = new DText(); TextReader MyString = new StringReader(XML); XmlTextReader XMLDoc = new XmlTextReader(MyString); XMLDoc.Read(); XMLDoc.MoveToContent(); if (XMLDoc.LocalName == "device") { if (XMLDoc.AttributeCount > 0) { for (int ax = 0; ax < XMLDoc.AttributeCount; ++ax) { XMLDoc.MoveToAttribute(ax); if (XMLDoc.LocalName == "MaxVersion") { RetVal.SetVersion(XMLDoc.Value); } } XMLDoc.MoveToContent(); XMLDoc.Read(); } else { XMLDoc.Read(); XMLDoc.MoveToContent(); } while (XMLDoc.LocalName != "device" && !XMLDoc.EOF) { switch (XMLDoc.LocalName) { case "deviceList": ParseDeviceList(XMLDoc.ReadOuterXml(), ref RetVal); break; case "URLBase": // Here, tport is a hack to make Windows Home Server visible. WHS does no set the port in the BaseURL and so, you need to keep it from the SSDP message. int tport = 80; if (RetVal.BaseURL != null) tport = RetVal.BaseURL.Port; RetVal.BaseURL = new Uri(XMLDoc.ReadString()); if (RetVal.BaseURL.Port == 80 && RetVal.BaseURL.Port != tport) RetVal.BaseURL = new Uri(RetVal.BaseURL.Scheme + "://" + RetVal.BaseURL.Host + ":" + tport + RetVal.BaseURL.AbsolutePath); break; case "deviceType": RetVal.DeviceURN = XMLDoc.ReadString(); break; case "friendlyName": RetVal.FriendlyName = XMLDoc.ReadString(); break; case "manufacturer": RetVal.Manufacturer = XMLDoc.ReadString(); break; case "manufacturerURL": RetVal.ManufacturerURL = XMLDoc.ReadString(); break; case "modelDescription": RetVal.ModelDescription = XMLDoc.ReadString(); break; case "modelName": RetVal.ModelName = XMLDoc.ReadString(); break; case "modelNumber": RetVal.ModelNumber = XMLDoc.ReadString(); break; case "modelURL": try { string u = XMLDoc.ReadString(); if (Uri.TryCreate(u, UriKind.Absolute, out RetVal.ModelURL) == false) { Uri.TryCreate("http://" + u, UriKind.Absolute, out RetVal.ModelURL); } } catch { } break; case "serialNumber": RetVal.SerialNumber = XMLDoc.ReadString(); break; case "UDN": TempString = XMLDoc.ReadString(); RetVal.UniqueDeviceName = TempString.Substring(5); break; case "UPC": RetVal.ProductCode = XMLDoc.ReadString(); break; case "presentationURL": RetVal.HasPresentation = true; RetVal.PresentationURL = XMLDoc.ReadString(); break; case "serviceList": if (XMLDoc.IsEmptyElement) break; XMLDoc.Read(); XMLDoc.MoveToContent(); while (XMLDoc.LocalName != "serviceList") { if (XMLDoc.LocalName == "service") { // TODO: DONE Resilience case 5a - wrap in try/catch block string servicexml = "Failed to read service xml element from device xml"; try { servicexml = XMLDoc.ReadOuterXml(); service = UPnPService.Parse(servicexml); RetVal.AddService(service); } catch (Exception e) { OpenSource.Utilities.EventLogger.Log(null, System.Diagnostics.EventLogEntryType.Error, "Invalid Service element within Device XML"); OpenSource.Utilities.EventLogger.Log(e, "XML content: \r\n" + servicexml); OpenSource.Utilities.EventLogger.Log(null, System.Diagnostics.EventLogEntryType.Warning, "Dropping failed Service and commencing parsing remainder of device"); } } if (!XMLDoc.IsStartElement()) { if (XMLDoc.LocalName != "serviceList") { XMLDoc.Read(); XMLDoc.MoveToContent(); } } } break; case "iconList": bool finishedIconList = false; while (!finishedIconList && XMLDoc.Read()) { switch (XMLDoc.NodeType) { case XmlNodeType.Element: if (XMLDoc.LocalName == "icon") { ParseIconXML(RetVal, XMLDoc.ReadOuterXml()); if (XMLDoc.NodeType == XmlNodeType.EndElement && XMLDoc.LocalName == "iconList") { finishedIconList = true; } } break; case XmlNodeType.EndElement: if (XMLDoc.LocalName == "iconList") { finishedIconList = true; } break; } } break; default: if (XMLDoc.LocalName != "") { string customPrefix = XMLDoc.Prefix; string customFieldName = XMLDoc.LocalName; string customFieldNamespace = XMLDoc.LookupNamespace(customPrefix); string customFieldVal = XMLDoc.ReadInnerXml(); RetVal.AddCustomFieldInDescription(customFieldName, customFieldVal, customFieldNamespace); } else { XMLDoc.Skip(); } continue; } XMLDoc.Read(); //XMLDoc.MoveToContent(); } } }
/// <summary> /// Reads the data type from an XML stream. /// </summary> private System.Type ReadType(XmlTextReader reader, string type) { bool array = false; string parsedType = type; string typeNamespace = null; // extract namespace. int index = parsedType.IndexOf(":"); if (index != -1) { typeNamespace = reader.LookupNamespace(parsedType.Substring(0, index)); parsedType = parsedType.Substring(index+1); } // remove array prefix. if (type.StartsWith(ARRAY_OF)) { parsedType = type.Substring(ARRAY_OF.Length, 1).ToLower() + type.Substring(ARRAY_OF.Length+1); array = true; } // check for standard types. if (parsedType == "byte") return (array)?typeof(sbyte[]):typeof(sbyte); if (parsedType == "unsignedByte") return (array)?typeof(byte[]):typeof(byte); if (parsedType == "short") return (array)?typeof(short[]):typeof(short); if (parsedType == "unsignedShort") return (array)?typeof(ushort[]):typeof(ushort); if (parsedType == "int") return (array)?typeof(int[]):typeof(int); if (parsedType == "unsignedInt") return (array)?typeof(uint[]):typeof(uint); if (parsedType == "long") return (array)?typeof(long[]):typeof(long); if (parsedType == "unsignedLong") return (array)?typeof(ulong[]):typeof(ulong); if (parsedType == "float") return (array)?typeof(float[]):typeof(float); if (parsedType == "double") return (array)?typeof(double[]):typeof(double); if (parsedType == "decimal") return (array)?typeof(decimal[]):typeof(decimal); if (parsedType == "string") return (array)?typeof(string[]):typeof(string); if (parsedType == "boolean") return (array)?typeof(bool[]):typeof(bool); if (parsedType == "dateTime") return (array)?typeof(DateTime[]):typeof(DateTime); if (parsedType == "anyType") return (array)?typeof(object[]):typeof(object); if (parsedType == "hexBinary") return (array)?typeof(string[]):typeof(string); // unsupported xml schema defined data type. if (typeNamespace == Opc.Namespace.XML_SCHEMA) { return null; } // must be complex type. return typeof(object[]); }