Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Print / Reach / PrintConfig / PrtTicket_Editor.cs / 1 / PrtTicket_Editor.cs
/*++ Copyright (C) 2003 Microsoft Corporation All rights reserved. Module Name: PrtTicket_Editor.cs Abstract: Definition and implementation of the internal PrintTicketEditor and XmlDocQName classes. Author: [....] ([....]) 8/9/2003 --*/ using System; using System.Xml; using System.IO; using System.Collections; using System.Globalization; using System.Runtime.InteropServices; using System.Printing; using MS.Internal.Printing.Configuration; namespace MS.Internal.Printing.Configuration { internal class PrintTicketEditor { #region Constructors private PrintTicketEditor() {} #endregion Constructors #region Public Methods ////// Verifies if the PrintTicket is well-formed /// ////// The PrintTicket is not well-formed. /// public static void CheckIsWellFormedPrintTicket(InternalPrintTicket pt) { XmlElement root = pt.XmlDoc.DocumentElement; // Root element should be in our standard namespace and should be. if ((root.NamespaceURI != PrintSchemaNamespaces.Framework) || (root.LocalName != PrintSchemaTags.Framework.PrintTicketRoot)) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.InvalidRootElement"), root.NamespaceURI, root.LocalName)); } string version = root.GetAttribute(PrintSchemaTags.Framework.RootVersionAttr, PrintSchemaNamespaces.FrameworkAttrForXmlDOM); // Root element should have the "version" attribute // (XmlElement.GetAttribute returns empty string when the attribute is not found, but // (XmlTextReader.GetAttribute returns null when the attribute is not found) if ((version == null) || (version.Length == 0)) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.RootMissingAttribute"), PrintSchemaTags.Framework.RootVersionAttr)); } decimal versionNum; try { versionNum = XmlConvertHelper.ConvertStringToDecimal(version); } catch (FormatException e) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.RootInvalidAttribute"), PrintSchemaTags.Framework.RootVersionAttr, version), e); } // and the "version" attribute value should be what we support if (versionNum != PrintSchemaTags.Framework.SchemaVersion) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.VersionNotSupported"), versionNum)); } // Now go through each root child element and verify they are valid children XmlNode rootChild = root.FirstChild; // It's recommended that traversing the node in forward-only movement by using NextSibling // is best for XmlDocument performance. This is because the list is not double-linked. while (rootChild != null) { // If the root child doesn't live in our standard namespace, we should ignore it // rather than rejecting it since it's acceptable to have private elements under // the root. if (rootChild.NamespaceURI == PrintSchemaNamespaces.Framework) { // For root, our Framework schema only allow these children elements: // if ((rootChild.NodeType != XmlNodeType.Element) || ((rootChild.LocalName != PrintSchemaTags.Framework.Feature) && (rootChild.LocalName != PrintSchemaTags.Framework.ParameterInit) && (rootChild.LocalName != PrintSchemaTags.Framework.Property))) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.RootInvalidChildElement"), rootChild.Name)); } string childName = ((XmlElement)rootChild).GetAttribute(PrintSchemaTags.Framework.NameAttr, PrintSchemaNamespaces.FrameworkAttrForXmlDOM); // All the recognized root child element should have an XML attribut "name" if ((childName == null) || (childName.Length == 0)) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.RootChildMissingAttribute"), rootChild.Name, PrintSchemaTags.Framework.NameAttr)); } } rootChild = rootChild.NextSibling; } // We will end the verification at the root child level here. Instead of traversing the whole tree // to find violations in this construtor, we will delay detecting violations under root child level // until information of an individual feature/property subtree is requested. } /// /// For PrintTicket XML supplied by client, it may not have all the standard namespace declarations we will need /// in future PrintTicket operations. This function will check and add any missing standard namespace declarations /// to the PrintTicket XML root element. /// ////// This function should be called after the PrintTicket XML has passed CheckIsWellFormedPrintTicket() validation, /// so it's operating on a well-formed PrintTicket XML. /// ////// Unable to add standard namespace declaration at root element. /// public static void CheckAndAddMissingStdNamespaces(InternalPrintTicket pt) { XmlElement root = pt.XmlDoc.DocumentElement; bool hasPSK = false, hasXSI = false, hasXSD = false; // Print Schema framework namespace must already exist in the well-formed PrintTicket XML. // first check if any standard namespace declaration is missing in the root element foreach (XmlAttribute attr in root.Attributes) { if (attr.Name.StartsWith("xmlns:", StringComparison.Ordinal) || (attr.Name == "xmlns")) { if (attr.Value == PrintSchemaNamespaces.StandardKeywordSet) { hasPSK = true; } else if (attr.Value == PrintSchemaNamespaces.xsi) { hasXSI = true; } else if (attr.Value == PrintSchemaNamespaces.xsd) { hasXSD = true; } } } // then add the declarations for any missing standard namespaces if (!hasPSK) { AddStdNamespaceDeclaration(root, PrintSchemaPrefixes.StandardKeywordSet, PrintSchemaNamespaces.StandardKeywordSet); } if (!hasXSI) { AddStdNamespaceDeclaration(root, PrintSchemaPrefixes.xsi, PrintSchemaNamespaces.xsi); } if (!hasXSD) { AddStdNamespaceDeclaration(root, PrintSchemaPrefixes.xsd, PrintSchemaNamespaces.xsd); } } ////// Add namespace declaration for the specified namespace URI. /// ////// Unable to add standard namespace declaration at root element. /// public static void AddStdNamespaceDeclaration(XmlElement root, string prefix_header, string nsURI) { int limit = 1000; // hard-coded upper limit for namespace prefix string look up int index; // We need to find a prefix string that doesn't conflict with any prefixes already used. for (index = 0; index < limit; index++) { // the new prefix is in the format like: "psk0000" "xsi0001" "xsd0100" string prefix = prefix_header + index.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0'); if (root.Attributes["xmlns:" + prefix] == null) { root.SetAttribute("xmlns:" + prefix, nsURI); break; } } if (index >= limit) { throw NewPTFormatException(String.Format(CultureInfo.CurrentCulture, PTUtility.GetTextFromResource("FormatException.AddXmlnsFailAtRootElement"))); } } ////// Gets the feature XML element in the PrintTicket for the specified feature name. /// Null is returned if the feature XML element can't be found. /// public static XmlElement GetSchemaElementWithNameAttr(InternalPrintTicket pt, XmlElement parent, string schemaTag, string nameAttrWanted) { XmlElement elementMatched = null; bool foundMatch = false; // Now go through each child element to find the matching schema element XmlNode child = parent.FirstChild; // It's recommended that traversing the node in forward-only movement by using NextSibling // is best for XmlDocument performance. This is because the list is not double-linked. while (child != null) { // We are looking for a standard namespace schema element node if ((child.NodeType != XmlNodeType.Element) || (child.LocalName != schemaTag) || (child.NamespaceURI != PrintSchemaNamespaces.Framework)) { child = child.NextSibling; continue; } if (nameAttrWanted == null) { // Caller is not looking for a "name" attribute value match, so we already // found the wanted schema element. foundMatch = true; } else { // We need to match the "name" attribute value. string childName = ((XmlElement)child).GetAttribute( PrintSchemaTags.Framework.NameAttr, PrintSchemaNamespaces.FrameworkAttrForXmlDOM); if ((childName != null) && (childName.Length != 0) && (XmlDocQName.GetURI(pt.XmlDoc, childName) == PrintSchemaNamespaces.StandardKeywordSet) && (XmlDocQName.GetLocalName(childName) == nameAttrWanted)) { foundMatch = true; } } if (foundMatch) { elementMatched = (XmlElement)child; break; } child = child.NextSibling; } return elementMatched; } ////// Removes all child schema element nodes that match the schemaTag name and the optional /// name attribute value of the specified parent node. This should only be used before /// writing a new schema element setting. /// public static void RemoveAllSchemaElementsWithNameAttr(InternalPrintTicket pt, XmlElement parent, string schemaTag, string nameAttrToDelete) { XmlElement childMatched; // Now go through each child element to find the matching schema element XmlNode child = parent.FirstChild; // It's recommended that traversing the node in forward-only movement by using NextSibling // is best for XmlDocument performance. This is because the list is not double-linked. while (child != null) { childMatched = null; // We are looking for a standard namespace schema element node if ((child.NodeType != XmlNodeType.Element) || (child.LocalName != schemaTag) || (child.NamespaceURI != PrintSchemaNamespaces.Framework)) { child = child.NextSibling; continue; } if (nameAttrToDelete == null) { // Caller is not looking for a "name" attribute value match, so we already // found the wanted schema element. childMatched = (XmlElement)child; } else { // We need to match the "name" attribute value. string childName = ((XmlElement)child).GetAttribute( PrintSchemaTags.Framework.NameAttr, PrintSchemaNamespaces.FrameworkAttrForXmlDOM); if ((childName != null) && (childName.Length != 0) && (XmlDocQName.GetURI(pt.XmlDoc, childName) == PrintSchemaNamespaces.StandardKeywordSet) && (XmlDocQName.GetLocalName(childName) == nameAttrToDelete)) { childMatched = (XmlElement)child; } } child = child.NextSibling; if (childMatched != null) { parent.RemoveChild(childMatched); } } } public static XmlElement AddSchemaElementWithNameAttr(InternalPrintTicket pt, XmlElement parent, string schemaTag, string nameAttr) { string prefix = pt.XmlDoc.DocumentElement.GetPrefixOfNamespace(PrintSchemaNamespaces.Framework); XmlElement newNode = pt.XmlDoc.CreateElement(prefix, schemaTag, PrintSchemaNamespaces.Framework); if (nameAttr != null) { newNode.SetAttribute(PrintSchemaTags.Framework.NameAttr, PrintSchemaNamespaces.FrameworkAttrForXmlDOM, XmlDocQName.GetQName(pt.XmlDoc, PrintSchemaNamespaces.StandardKeywordSet, nameAttr)); } return (XmlElement)parent.AppendChild(newNode); } public static void SetXsiTypeAttr(InternalPrintTicket pt, XmlElement valueElement, string xsiType) { // the attribute is in the format like: xsi:type="xsd:integer" valueElement.SetAttribute("type", PrintSchemaNamespaces.xsi, XmlDocQName.GetQName(pt.XmlDoc, PrintSchemaNamespaces.xsd, xsiType)); } #endregion Public Methods #region Private Methods ////// Returns a new FormatException instance for not-well-formed PrintTicket XML. /// /// detailed message about the violation of well-formness ///the new FormatException instance private static FormatException NewPTFormatException(string detailMsg) { return InternalPrintTicket.NewPTFormatException(detailMsg); } ////// Returns a new FormatException instance for not-well-formed PrintTicket XML. /// /// detailed message about the violation of well-formness /// the exception that causes the violation of well-formness ///the new FormatException instance private static FormatException NewPTFormatException(string detailMsg, Exception innerException) { return InternalPrintTicket.NewPTFormatException(detailMsg, innerException); } #endregion Private Methods } internal class XmlDocQName { #region Constructors private XmlDocQName() {} #endregion Constructors #region Public Methods public static string GetURI(XmlDocument xmlDoc, string QName) { int colonIndex = QName.IndexOf(":", StringComparison.Ordinal); string prefix = (colonIndex == (-1)) ? "" : QName.Substring(0, colonIndex); string uri = xmlDoc.DocumentElement.GetNamespaceOfPrefix(prefix); // return uri; } public static string GetLocalName(string QName) { int colonIndex = QName.IndexOf(":", StringComparison.Ordinal); string localName =QName.Substring(colonIndex + 1); return localName; } public static string GetQName(XmlDocument xmlDoc, string URI, string localName) { string QName; string prefix = xmlDoc.DocumentElement.GetPrefixOfNamespace(URI); if (prefix == null) { QName = localName; } else { QName = prefix + ":" + localName; } // Console.WriteLine("URI:{0}, localName:{1} ===> QName:{2}", URI, localName, QName); return QName; } #endregion Public Methods } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Util.cs
- ActivityCodeDomSerializationManager.cs
- RequestSecurityToken.cs
- DisposableCollectionWrapper.cs
- TextRunTypographyProperties.cs
- OleDbPropertySetGuid.cs
- XPathPatternParser.cs
- RestHandlerFactory.cs
- CellQuery.cs
- _AutoWebProxyScriptWrapper.cs
- GridEntry.cs
- CharEnumerator.cs
- XmlSequenceWriter.cs
- XslCompiledTransform.cs
- Array.cs
- Command.cs
- TagPrefixCollection.cs
- COM2IVsPerPropertyBrowsingHandler.cs
- DataPagerCommandEventArgs.cs
- SystemFonts.cs
- RegexCapture.cs
- RadioButtonStandardAdapter.cs
- MetafileHeaderWmf.cs
- ExpressionConverter.cs
- RenamedEventArgs.cs
- RootBrowserWindowAutomationPeer.cs
- ArgumentOutOfRangeException.cs
- DecimalFormatter.cs
- CatalogZone.cs
- ContextMenuService.cs
- VisualStyleTypesAndProperties.cs
- XmlElementCollection.cs
- DBPropSet.cs
- ConfigWriter.cs
- DataServiceKeyAttribute.cs
- AdornerLayer.cs
- SpAudioStreamWrapper.cs
- SystemWebCachingSectionGroup.cs
- MarginsConverter.cs
- MobileFormsAuthentication.cs
- IpcClientManager.cs
- DataGridViewAccessibleObject.cs
- MimeBasePart.cs
- VerificationException.cs
- TriggerAction.cs
- ParseNumbers.cs
- DataKey.cs
- FileDialogCustomPlacesCollection.cs
- TrackBarDesigner.cs
- Timer.cs
- FileDialog_Vista.cs
- Transform3DGroup.cs
- AlternateViewCollection.cs
- PolicyValidator.cs
- CommandDevice.cs
- UserMapPath.cs
- IndentedTextWriter.cs
- TrackingStringDictionary.cs
- Point3D.cs
- HtmlMeta.cs
- MetadataArtifactLoaderFile.cs
- HyperLinkStyle.cs
- RemoteHelper.cs
- Focus.cs
- Vector3D.cs
- XmlDataImplementation.cs
- COMException.cs
- InstanceDataCollectionCollection.cs
- ObjectContext.cs
- PropertyBuilder.cs
- HostedNamedPipeTransportManager.cs
- PanelDesigner.cs
- FixedSOMImage.cs
- XmlSchemaAll.cs
- Thumb.cs
- BrowserCapabilitiesFactory.cs
- HttpPostedFile.cs
- UriTemplatePathPartiallyEquivalentSet.cs
- SqlColumnizer.cs
- LateBoundBitmapDecoder.cs
- PasswordRecovery.cs
- OracleBFile.cs
- Hyperlink.cs
- SimpleBitVector32.cs
- GenericTypeParameterConverter.cs
- ExpressionPrefixAttribute.cs
- SoapExtensionImporter.cs
- EventDescriptor.cs
- AnnotationResource.cs
- TileModeValidation.cs
- AsyncInvokeOperation.cs
- SqlServer2KCompatibilityCheck.cs
- DesignUtil.cs
- DataTableMappingCollection.cs
- SQLDoubleStorage.cs
- DirectionalLight.cs
- TextEditorSelection.cs
- TimeManager.cs
- UTF8Encoding.cs
- ServicePointManager.cs