public void TestDomXmlWriterPicksCorrectSubstitutions() { var loader = GetSchemaLoader(); DomNode node; string output; const string testDoc = @"<?xml version=""1.0"" encoding=""utf-8""?> <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns=""test""> <basic name=""something"" /> <middle name=""some-other-thing"" special=""true"" /> <descendant name=""leaf-thing"" special=""true"" extra=""important"" /> <container name=""some-container""> <middle name=""some-nested-thing"" /> <middle name=""some-other-nested-thing"" special=""true"" /> </container> </root>"; using (Stream s = CreateStreamFromString(testDoc)) { DomXmlReader reader = new DomXmlReader(loader); node = reader.Read(s, null); } Assert.AreEqual(4, node.Children.Count()); var children = node.Children.ToList(); Assert.AreEqual("test:basicType", children[0].Type.Name); Assert.AreEqual("test:middleType", children[1].Type.Name); Assert.AreEqual("test:descendantType", children[2].Type.Name); Assert.AreEqual("test:containerType", children[3].Type.Name); using (MemoryStream s = new MemoryStream()) { DomXmlWriter writer = new DomXmlWriter(loader.GetTypeCollection("test")); writer.Write(node, s, null); output = Encoding.UTF8.GetString(s.ToArray()); } Assert.AreEqual(testDoc, output); }
/// <summary> /// Loads the document at the given URI. Creates a D2dTimelineRenderer and D2dTimelineControl /// (through TimelineDocument's Renderer property) to render and display timelines. /// If isMasterDocument is true and if the file doesn't exist, a new document is created.</summary> /// <param name="uri">URI of document to load</param> /// <param name="isMasterDocument">True iff is master document</param> /// <returns>TimelineDocument loaded</returns> private TimelineDocument LoadOrCreateDocument(Uri uri, bool isMasterDocument) { // Documents need to have a absolute Uri, so that the relative references to sub-documents // are not ambiguous, and so that the FileWatcherService can be used. string filePath; if (uri.IsAbsoluteUri) { filePath = uri.LocalPath; } else if (!isMasterDocument) { filePath = PathUtil.GetAbsolutePath(uri.OriginalString, Path.GetDirectoryName(s_repository.ActiveDocument.Uri.LocalPath)); uri = new Uri(filePath, UriKind.Absolute); } else { filePath = PathUtil.GetAbsolutePath(uri.OriginalString, Directory.GetCurrentDirectory()); uri = new Uri(filePath, UriKind.Absolute); } // Check if the repository contains this Uri. Remember that document Uris have to be absolute. bool isNewToThisEditor = true; DomNode node = null; TimelineDocument document = (TimelineDocument)s_repository.GetDocument(uri); if (document != null) { node = document.DomNode; isNewToThisEditor = false; } else if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(s_schemaLoader); node = reader.Read(stream, uri); } } else if (isMasterDocument) { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.timelineType.Type, Schema.timelineRootElement); } if (node != null) { if (document == null) { document = node.Cast<TimelineDocument>(); D2dTimelineRenderer renderer = CreateTimelineRenderer(); document.Renderer = renderer; renderer.Init(document.TimelineControl.D2dGraphics); string fileName = Path.GetFileName(filePath); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); //Set IsDocument to true to prevent exception in command service if two files with the // same name, but in different directories, are opened. controlInfo.IsDocument = true; TimelineContext timelineContext = document.Cast<TimelineContext>(); timelineContext.ControlInfo = controlInfo; document.Uri = uri; if (isMasterDocument) s_repository.ActiveDocument = document;//adds 'document' else { // For sub-documents, we want ActiveDocument to remain the main document so that // TimelineValidator can identify if a sub-document or master document is being // modified. IDocument previous = s_repository.ActiveDocument; s_repository.ActiveDocument = document;//adds 'document' s_repository.ActiveDocument = previous;//restores master document } } IHierarchicalTimeline hierarchical = document.Timeline as IHierarchicalTimeline; if (hierarchical != null) { ResolveAll(hierarchical, new HashSet<IHierarchicalTimeline>()); } // Listen to events if this is the first time we've seen this. if (isNewToThisEditor) { // The master document/context needs to listen to events on any sub-document // so that transactions can be cancelled correctly. if (isMasterDocument) { node.AttributeChanging += DomNode_AttributeChanging; } else { DomNode masterNode = s_repository.ActiveDocument.As<DomNode>(); node.SubscribeToEvents(masterNode); } } // Initialize Dom extensions now that the data is complete node.InitializeExtensions(); } return document; }
/// <summary> /// Opens or creates a document at the given URI. Create an AdaptableControl with control adapters for viewing state machine. /// Handles application data persistence.</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; string fileName = Path.GetFileName(filePath); if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.fsmType.Type, Schema.fsmRootElement); // create an empty root prototype folder node.SetChild( Schema.fsmType.prototypeFolderChild, new DomNode(Schema.prototypeFolderType.Type)); } Document document = null; if (node != null) { // set up the AdaptableControl for editing FSMs var control = new D2dAdaptableControl(); control.SuspendLayout(); control.BackColor = SystemColors.ControlLight; control.AllowDrop = true; var transformAdapter = new TransformAdapter(); // required by several of the other adapters transformAdapter.UniformScale = true; transformAdapter.MinScale = new PointF(0.25f, 0.25f); transformAdapter.MaxScale = new PointF(4, 4); var viewingAdapter = new ViewingAdapter(transformAdapter); // implements IViewingContext for framing or ensuring that items are visible var canvasAdapter = new CanvasAdapter(); // implements a bounded canvas to limit scrolling var autoTranslateAdapter = // implements auto translate when the user drags out of control's client area new AutoTranslateAdapter(transformAdapter); var mouseTransformManipulator = // implements mouse drag translate and scale new MouseTransformManipulator(transformAdapter); var mouseWheelManipulator = // implements mouse wheel scale new MouseWheelManipulator(transformAdapter); var scrollbarAdapter = // adds scroll bars to control, driven by canvas and transform new ScrollbarAdapter(transformAdapter, canvasAdapter); var hoverAdapter = new HoverAdapter(); // add hover events over pickable items hoverAdapter.HoverStarted += control_HoverStarted; hoverAdapter.HoverStopped += control_HoverStopped; var annotationAdaptor = new D2dAnnotationAdapter(m_theme); // display annotations under diagram var fsmAdapter = // adapt control to allow binding to graph data new D2dGraphAdapter<State, Transition, NumberedRoute>(m_fsmRenderer, transformAdapter); var fsmStateEditAdapter = // adapt control to allow state editing new D2dGraphNodeEditAdapter<State, Transition, NumberedRoute>(m_fsmRenderer, fsmAdapter, transformAdapter); var fsmTransitionEditAdapter = // adapt control to allow transition new D2dGraphEdgeEditAdapter<State, Transition, NumberedRoute>(m_fsmRenderer, fsmAdapter, transformAdapter); var mouseLayoutManipulator = new MouseLayoutManipulator(transformAdapter); // apply adapters to control; ordering is from back to front, that is, the first adapter // will be conceptually underneath all the others. Mouse and keyboard events are fed to // the adapters in the reverse order, so it all makes sense to the user. control.Adapt( hoverAdapter, scrollbarAdapter, autoTranslateAdapter, new RectangleDragSelector(), transformAdapter, viewingAdapter, canvasAdapter, mouseTransformManipulator, mouseWheelManipulator, new KeyboardGraphNavigator<State, Transition, NumberedRoute>(), //new GridAdapter(), annotationAdaptor, fsmAdapter, fsmStateEditAdapter, fsmTransitionEditAdapter, new LabelEditAdapter(), new SelectionAdapter(), mouseLayoutManipulator, new DragDropAdapter(m_statusService), new ContextMenuAdapter(m_commandService, m_contextMenuCommandProviders) ); control.ResumeLayout(); // associate the control with the viewing context; other adapters use this // adapter for viewing, layout and calculating bounds. ViewingContext viewingContext = node.Cast<ViewingContext>(); viewingContext.Control = control; // set document URI document = node.As<Document>(); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); //Set IsDocument to true to prevent exception in command service if two files with the // same name, but in different directories, are opened. controlInfo.IsDocument = true; document.ControlInfo = controlInfo; document.Uri = uri; // now that the data is complete, initialize the rest of the extensions to the Dom data; // this is needed for adapters such as validators, which may not be referenced anywhere // but still need to be initialized. node.InitializeExtensions(); // set control's context to main editing context EditingContext editingContext = node.Cast<EditingContext>(); control.Context = editingContext; // show the FSM control m_controlHostService.RegisterControl(control, controlInfo, this); } return document; }
/// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; string fileName = Path.GetFileName(filePath); if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.winGuiCommonDataType.Type, Schema.winGuiCommonDataRootElement); } WinGuiCommonDataDocument document = null; if (node != null) { // Initialize Dom extensions now that the data is complete node.InitializeExtensions(); WinGuiCommonDataContext context = node.As<WinGuiCommonDataContext>(); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); context.ControlInfo = controlInfo; // set document URI document = node.As<WinGuiCommonDataDocument>(); document.Uri = uri; context.ListView.Tag = document; // show the ListView control m_controlHostService.RegisterControl(context.ListView, controlInfo, this); } return document; }
null); // "open document" icon /// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(UISchema.UIType.Type, UISchema.UIRootElement); UI ui = node.As<UI>(); ui.Name = "UI"; } Document document = null; if (node != null) { // Initialize Dom extensions now that the data is complete; after this, all Dom node // adapters will have been bound to their underlying Dom node. node.InitializeExtensions(); // get the root node's UIDocument adapter document = node.As<Document>(); document.Uri = uri; // only allow 1 open document at a time Document activeDocument = m_documentRegistry.GetActiveDocument<Document>(); if (activeDocument != null) Close(activeDocument); } return document; }
IEnumerable<object> IResourceMetadataService.GetMetadata(IEnumerable<Uri> resourceUris) { List<DomNode> rootNodes = new List<DomNode>(); foreach (Uri resourceUri in resourceUris) { string reExt = System.IO.Path.GetExtension(resourceUri.LocalPath).ToLower(); ResourceMetadataInfo resInfo; m_extMap.TryGetValue(reExt, out resInfo); if(resInfo == null) m_extMap.TryGetValue(".*", out resInfo); string metadataFilePath = resourceUri.LocalPath + resInfo.MetadataFileExt; Uri metadataUri = new Uri(metadataFilePath); DomNode rootNode = null; if (File.Exists(metadataFilePath)) { // read existing metadata using (FileStream stream = File.OpenRead(metadataFilePath)) { var reader = new DomXmlReader(m_schemaLoader); rootNode = reader.Read(stream, metadataUri); } } else { rootNode = resInfo.CreateNode(); rootNode.SetAttribute(Schema.resourceMetadataType.uriAttribute, resourceUri); } rootNode.InitializeExtensions(); ResourceMetadataDocument document = rootNode.Cast<ResourceMetadataDocument>(); document.Uri = metadataUri; rootNodes.Add(rootNode); } return rootNodes; }
private void SetActiveDocument(Stream stream) { var domReader = new DomXmlReader(s_schemaLoader); var node = domReader.Read(stream, null); node.InitializeExtensions(); m_activeDocument = node.Cast<SkinDocument>(); var context = node.As<SkinEditingContext>(); m_treeControlAdapter.TreeView = context; m_PropertyGrid.Bind(null); context.SelectionChanged += (sender, e) => { var selectedNode = context.GetLastSelected<DomNode>(); var propContext = new CustomPropertyEditingContext(selectedNode, m_activeDocument); m_PropertyGrid.Bind(propContext); }; context.Ended += Context_Refresh; context.Cancelled += Context_Refresh; m_activeDocument.UriChanged += (sender, e) => UpdateTitleText(); m_activeDocument.DirtyChanged += (sender, e) => UpdateTitleText(); UpdateTitleText(); OnSkinChanged(); }
public static XLEPlacementDocument OpenOrCreate(Uri uri, ISchemaLoader schemaLoader) { if (!uri.IsAbsoluteUri) return null; var docRegistry = GetDocRegistry(); if (docRegistry != null) { var existing = docRegistry.FindDocument(uri); if (existing != null) return null; // prevent a second reference here } string filePath = uri.LocalPath; bool createdNewFile = false; DomNode rootNode = null; if (File.Exists(filePath)) { // read existing document using custom dom XML reader using (FileStream stream = File.OpenRead(filePath)) { // Note -- Sony code uses "CustomDomXmlReader" to modify // the urls of relative assets in reference types at // load time. // However, we're going to prefer a method that does this // on demand, rather than at load time -- so we should be able // to use a standard xml reader. // var reader = new CustomDomXmlReader(Globals.ResourceRoot, schemaLoader); var reader = new DomXmlReader(schemaLoader as XmlSchemaTypeLoader); rootNode = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema rootNode = new DomNode(Schema.placementsDocumentType.Type, Schema.placementsDocumentRootElement); createdNewFile = true; } var doc = rootNode.As<XLEPlacementDocument>(); doc.Uri = uri; // Initialize Dom extensions now that the data is complete rootNode.InitializeExtensions(); if (docRegistry!=null) docRegistry.Add(doc); doc.Dirty = createdNewFile; return doc; }
/// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; string fileName = Path.GetFileName(filePath); if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.winGuiCommonDataType.Type, Schema.winGuiCommonDataRootElement); } WinGuiWpfDataDocument document = null; if (node != null) { // Initialize Dom extensions now that the data is complete node.InitializeExtensions(); WinGuiWpfDataContext context = node.As<WinGuiWpfDataContext>(); context.SelectionChanged += ContextSelectionChanged; ControlInfo controlInfo = new ControlInfo(Path.Combine(filePath, fileName), StandardControlGroup.Center, new Sce.Atf.Wpf.Docking.DockContent(null, null), this); context.ControlInfo = controlInfo; // set document URI document = node.As<WinGuiWpfDataDocument>(); document.Uri = uri; context.ListView.Tag = document; // If the document is empty, add some data so there's something to look at if (!context.Items.Any(typeof(object))) { DomNode domNode = new DomNode(Schema.eventType.Type); domNode.SetAttribute(Schema.eventType.nameAttribute, "First Event"); domNode.SetAttribute(Schema.eventType.durationAttribute, 100); var dataObject = new System.Windows.DataObject(new object[] { domNode }); context.Insert(dataObject); domNode = new DomNode(Schema.eventType.Type); domNode.SetAttribute(Schema.eventType.nameAttribute, "Second Event"); domNode.SetAttribute(Schema.eventType.durationAttribute, 200); var dataObject2 = new System.Windows.DataObject(new object[] { domNode }); context.Insert(dataObject2); } // show the ListView control ControlInfo registeredControlInfo = (ControlInfo)m_controlHostService.RegisterControl(context.ListView, fileName, "WPF Sample file", controlInfo.Group, fileName, this); if (registeredControlInfo.DockContent != null) { registeredControlInfo.DockContent.Closing += DockContent_Closing; } } return document; }
/// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; string fileName = Path.GetFileName(filePath); if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.storyType.Type, Schema.storyRootElement); } StoryDocument document = null; if (node != null) { // Initialize Dom extensions now that the data is complete node.InitializeExtensions(); // set document URI document = node.As<StoryDocument>(); document.Init(uri); document.Read(); // show the ListView control m_controlHostService.RegisterControl(document.Control, document.ControlInfo, this); } return document; }
/// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.circuitDocumentType.Type, Schema.circuitRootElement); // create an empty root prototype folder node.SetChild( Schema.circuitDocumentType.prototypeFolderChild, new DomNode(Schema.prototypeFolderType.Type)); } CircuitDocument circuitCircuitDocument = null; if (node != null) { AdaptableControl control = CreateCircuitControl(node); control.AddHelp("https://github.com/SonyWWS/ATF/wiki/Adaptable-Controls".Localize()); var viewingContext = node.Cast<ViewingContext>(); viewingContext.Control = control; circuitCircuitDocument = node.Cast<CircuitDocument>(); string fileName = Path.GetFileName(filePath); ControlInfo controlInfo = new ControlInfo(fileName, filePath, StandardControlGroup.Center); //Set IsDocument to true to prevent exception in command service if two files with the // same name, but in different directories, are opened. controlInfo.IsDocument = true; circuitCircuitDocument.ControlInfo = controlInfo; circuitCircuitDocument.Uri = uri; var editingContext = node.Cast<CircuitEditingContext>(); editingContext.GetLocalBound = GetLocalBound; editingContext.GetWorldOffset = GetWorldOffset; editingContext.GetTitleHeight = GetTitleHeight; editingContext.GetLabelHeight = GetLabelHeight; editingContext.GetSubContentOffset = GetSubContentOffset; control.Context = editingContext; editingContext.SchemaLoader = m_schemaLoader; // schema needed for cut and paste between applications // now that the data is complete, initialize all other extensions to the Dom data node.InitializeExtensions(); m_circuitControlRegistry.RegisterControl(node, control, controlInfo, this); // Set the zoom and translation to show the existing items (if any). var enumerableContext = editingContext.Cast<IEnumerableContext>(); if (viewingContext.CanFrame(enumerableContext.Items)) viewingContext.Frame(enumerableContext.Items); } return circuitCircuitDocument; }
/// <summary> /// Opens or creates a document at the given URI</summary> /// <param name="uri">Document URI</param> /// <returns>Document, or null if the document couldn't be opened or created</returns> public IDocument Open(Uri uri) { DomNode node = null; string filePath = uri.LocalPath; string fileName = Path.GetFileName(filePath); if (File.Exists(filePath)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } else { // create new document by creating a Dom node of the root type defined by the schema node = new DomNode(Schema.eventSequenceType.Type, Schema.eventSequenceRootElement); } EventSequenceDocument document = null; if (node != null) { // Initialize Dom extensions now that the data is complete node.InitializeExtensions(); EventSequenceContext context = node.As<EventSequenceContext>(); ControlInfo controlInfo = new ControlInfo(Path.Combine(filePath, fileName), StandardControlGroup.Center, new DockContent(null, null), this); context.ControlInfo = controlInfo; // set document URI document = node.As<EventSequenceDocument>(); document.Uri = uri; context.Document = document; // show the document editor // This line requires references to System.Drawing and System.Windows.Forms. Would really like to remove those dependencies! m_controlHostService.RegisterControl(context.View, fileName, "Event sequence document", StandardControlGroup.Center, Path.Combine(filePath, fileName), this); } return document; }
/// <summary> /// Load circuit templates stored in an external file</summary> protected override ImportedContent LoadExternalTemplateLibrary(Uri uri) { DomNode node = null; string filePath = string.Empty; if (uri == null) { var dlg = new OpenFileDialog(); dlg.Filter = "Circuit Template File (*.circuit)|*.circuit".Localize(); dlg.CheckFileExists = true; if (dlg.ShowDialog() == DialogResult.OK) { uri = new Uri(dlg.FileName, UriKind.RelativeOrAbsolute); filePath = dlg.FileName; } } else filePath = uri.LocalPath; if (File.Exists(filePath)) { if (TemplatingContext.ValidateNewFolderUri(uri)) { // read existing document using standard XML reader using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { DomXmlReader reader = new DomXmlReader(m_schemaLoader); node = reader.Read(stream, uri); } } } return new ImportedContent(node, uri); }