public static void TransformContent(Node node, HttpContext context, object extension) { //var traceMessage = string.Concat(" TRANSFORM content: ", node.Name, Environment.NewLine); //System.Diagnostics.Trace.Write(traceMessage); var outputStream = context.Response.OutputStream; var content = Content.Load(node.Path); var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, IndentChars = " " }; XmlWriter writer = null; try { writer = XmlWriter.Create(outputStream, settings); writer.WriteStartDocument(); writer.WriteStartElement("ContentMetaData"); writer.WriteElementString("ContentType", content.ContentType.Name); writer.WriteElementString("ContentName", content.Name); writer.WriteStartElement("Fields"); var expContext = new ExportContext(content.Path, ""); content.ExportFieldData(writer, expContext); writer.WriteEndElement(); writer.WriteEndElement(); } finally { if (writer != null) { writer.Flush(); writer.Close(); } } }
private static void ExportContentType(Content content, ExportContext context, string indent) { BinaryData binaryData = ((ContentType)content.ContentHandler).Binary; var name = content.Name + "Ctd.xml"; var fsPath = Path.Combine(context.ContentTypeDirectory, name); Stream source = null; FileStream target = null; try { source = binaryData.GetStream(); target = new FileStream(fsPath, FileMode.Create); for (var i = 0; i < source.Length; i++) target.WriteByte((byte)source.ReadByte()); } finally { if (source != null) source.Close(); if (target != null) { target.Flush(); target.Close(); } } }
private static void ExportByFilter(Content root, ExportContext context, string fsRoot, string queryPath) { string queryText = null; using (var reader = new StreamReader(queryPath)) { queryText = reader.ReadToEnd(); } var query = ContentQuery.CreateQuery(queryText); var result = query.Execute(); var maxCount = result.Count; var count = 0; foreach (var nodeId in result.Identifiers) { string fsPath = null; Content content = null; try { content = Content.Load(nodeId); var relPath = content.Path.Remove(0, 1).Replace("/", "\\"); fsPath = Path.Combine(fsRoot, relPath); var fsDir = Path.GetDirectoryName(fsPath); var dirInfo = new DirectoryInfo(fsDir); if (!dirInfo.Exists) Directory.CreateDirectory(fsDir); ExportContent(content, context, fsDir, String.Concat(++count, "/", maxCount, ": ", relPath, "\\")); } catch (Exception ex) { PrintException(ex, content == null ? fsPath : content.Path); } } }
private static void ExportContent(Content content, ExportContext context, string fsPath, string indent) { if (content.ContentHandler is ContentType) { LogWriteLine(indent, content.Name); ExportContentType(content, context, indent); return; } context.CurrentDirectory = fsPath; LogWriteLine(indent, content.Name); string metaFilePath = Path.Combine(fsPath, content.Name + ".Content"); XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.IndentChars = " "; XmlWriter writer = null; try { writer = XmlWriter.Create(metaFilePath, settings); //<?xml version="1.0" encoding="utf-8"?> //<ContentMetaData> // <ContentType>Site</ContentType> // <Fields> // ... writer.WriteStartDocument(); writer.WriteStartElement("ContentMetaData"); writer.WriteElementString("ContentType", content.ContentType.Name); writer.WriteElementString("ContentName", content.Name); writer.WriteStartElement("Fields"); try { content.ExportFieldData(writer, context); } catch (Exception e) { PrintException(e, fsPath); writer.WriteComment(String.Concat("EXPORT ERROR", CR, e.Message, CR, e.StackTrace)); } writer.WriteEndElement(); writer.WriteStartElement("Permissions"); writer.WriteElementString("Clear", null); content.ContentHandler.Security.ExportPermissions(writer); writer.WriteEndElement(); writer.WriteEndElement(); } finally { if (writer != null) { writer.Flush(); writer.Close(); } } }
private static void ExportContentTree(Content content, ExportContext context, string fsPath, string indent) { try { ExportContent(content, context, fsPath, indent); } catch (Exception ex) { var path = content == null ? fsPath : content.Path; PrintException(ex, path); return; } //TODO: SmartFolder may contain real items too if (content.ContentHandler is SmartFolder) return; // create folder only if it has children var contentAsFolder = content.ContentHandler as IFolder; var contentAsGeneric = content.ContentHandler as GenericContent; //try everything that can have children (generic content, content types or other non-gc nodes) if (contentAsFolder == null && contentAsGeneric == null) return; try { var settings = new QuerySettings {EnableAutofilters = false, EnableLifespanFilter = false}; var queryResult = contentAsFolder == null ? contentAsGeneric.GetChildren(settings) : contentAsFolder.GetChildren(settings); if (queryResult.Count == 0) return; var children = queryResult.Nodes; var newDir = Path.Combine(fsPath, GetSafeFileNameFromContentName(content.Name)); if (!(content.ContentHandler is ContentType)) Directory.CreateDirectory(newDir); var newIndent = indent + " "; foreach (var childContent in from node in children select Content.Create(node)) ExportContentTree(childContent, context, newDir, newIndent); } catch (Exception ex) { PrintException(ex, fsPath); } }
private static void Export(string repositoryPath, string fsPath, string queryPath) { try { //-- check fs folder DirectoryInfo dirInfo = new DirectoryInfo(fsPath); if (!dirInfo.Exists) { LogWrite("Creating target directory: ", fsPath, " ... "); Directory.CreateDirectory(fsPath); LogWriteLine("Ok"); } else { LogWriteLine("Target directory exists: ", fsPath, ". Exported contents will override existing subelements."); } //-- load export root Content root = Content.Load(repositoryPath); if (root == null) { LogWriteLine(); LogWriteLine("Content does not exist: ", repositoryPath); } else { LogWriteLine(); LogWriteLine("=========================== Export ==========================="); LogWriteLine("From: ", repositoryPath); LogWriteLine("To: ", fsPath); if (queryPath != null) LogWriteLine("Filter: ", queryPath); LogWriteLine("=============================================================="); var context = new ExportContext(repositoryPath, fsPath); if (queryPath != null) ExportByFilter(root, context, fsPath, queryPath); else ExportContentTree(root, context, fsPath, ""); LogWriteLine("--------------------------------------------------------------"); LogWriteLine("Outer references:"); var outerRefs = context.GetOuterReferences(); if (outerRefs.Count == 0) LogWriteLine("All references are exported."); else foreach (var item in outerRefs) LogWriteLine(item); } } catch (Exception e) { PrintException(e, fsPath); } LogWriteLine("=============================================================="); if (exceptions == 0) LogWriteLine("Export is successfully finished."); else LogWriteLine("Export is finished with ", exceptions, " errors."); LogWriteLine("Read log file: ", _logFilePath); }
protected virtual void ExportData2(XmlWriter writer, ExportContext context) { ExportData(writer, context); }
protected virtual void ExportData(XmlWriter writer, ExportContext context) { throw ExportNotImplementedException(); }
internal void Export2(XmlWriter writer, ExportContext context) { if (ReadOnly) return; if (GetData() == null) return; if (!HasExportData) return; FieldSubType subType; var exportName = GetExportName(this.Name, out subType); writer.WriteStartElement(exportName); if (subType != FieldSubType.General) writer.WriteAttributeString(FIELDSUBTYPEATTRIBUTENAME, subType.ToString()); ExportData2(writer, context); writer.WriteEndElement(); }