GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • @maxgraph/core

This view

  • Original page
  • New address

GeezerVison is showing maxgraph.github.io — Codec | @maxgraph/core

  • Codec

Class Codec

XML codec for JavaScript object graphs. See ObjectCodec for a description of the general encoding/decoding scheme. This class uses the codecs registered in CodecRegistry for encoding/decoding each object.

References

In order to resolve references, especially forward references, the Codec constructor must be given the document that contains the referenced elements.

Examples

The following code is used to encode a graph model.

const encoder = new Codec();
const result = encoder.encode(graph.getDataModel());
const xml = xmlUtils.getXml(result);

WARN: as of version 0.6.0, the codecs provided by maxGraph are no longer registered by default, they MUST be registered before performing encode or decode. For instance, you can use the registerAllCodecs function (or other related functions) to register the codecs.

Example

Using the code below, an XML document is decoded into an existing model. The document may be obtained using parseXml for parsing an XML string.

const doc = xmlUtils.parseXml(xmlString);
const codec = new Codec(doc);
codec.decode(doc.documentElement, graph.getDataModel());

Example

This example demonstrates parsing a list of isolated cells into an existing graph model. Note that the cells do not have a parent reference so they can be added anywhere in the cell hierarchy after parsing.

const xml = `<GraphDataModel>
<root>
<Cell id="2" value="Hello," vertex="1">
<Geometry _x="20" _y="20" _width="80" _height="30" as="geometry"/>
</Cell>
<Cell id="3" value="World!" vertex="1">
<Geometry _x="200" _y="150" _width="80" _height="30" as="geometry"/>
</Cell>
<Cell id="4" value="" edge="1" source="2" target="3">
<Geometry relative="1" as="geometry"/>
</Cell>
</root>
</GraphDataModel>
`;
const doc = xmlUtils.parseXml(xml);
const codec = new Codec(doc);
let elt = doc.documentElement.firstChild;
const cells = [];

while (elt != null) {
cells.push(codec.decode(elt));
elt = elt.nextSibling;
}
graph.addCells(cells);

Example

Using the following code, the selection cells of a graph are encoded and the output is displayed in a dialog box.

const enc = new Codec();
const cells = graph.getSelectionCells();
const xml = xmlUtils.getPrettyXml(enc.encode(cells));

Newlines in the XML can be converted to
, in which case a '
' argument must be passed to getXml as the second argument.

Debugging

For debugging, I/O you can use the following code to get the sequence of encoded objects:

const oldEncode = encode;
encode(obj)
{
GlobalConfig.logger.show();
GlobalConfig.logger.debug('Codec.encode: obj=' + StringUtils.getFunctionName(obj.constructor));

return oldEncode.apply(this, arguments);
};

Note that the I/O system adds object codecs for new object automatically. For decoding those objects, the constructor should be written as follows:

var MyObj(name)
{
// ...
};
  • Defined in serialization/Codec.ts:142
Index

Constructors

constructor

Properties

document elements encodeDefaults objects

Methods

addElement decode decodeCell encode encodeCell getElementById getId getObject insertIntoGraph isCellCodec lookup putObject reference setAttribute updateElements

Constructors

constructor

  • new Codec(document?: XMLDocument): Codec

    Parameters

    • document: XMLDocument = ...

    Returns Codec

    • Defined in serialization/Codec.ts:143

Properties

document

document: XMLDocument

The owner document of the codec.

  • Defined in serialization/Codec.ts:151

elements

elements: Record<string, Element> = {}

Lookup table for resolving IDs to elements.

  • Defined in serialization/Codec.ts:161

encodeDefaults

encodeDefaults: boolean = false

Specifies if default values should be encoded.

Default

false
  • Defined in serialization/Codec.ts:167

objects

objects: { [key: string]: Element }

Maps from IDs to objects.

  • Defined in serialization/Codec.ts:156

Methods

addElement

  • addElement(node: Element): void

    Adds the given element to elements if it has an ID.

    Parameters

    • node: Element

    Returns void

    • Defined in serialization/Codec.ts:249

decode

  • decode(node: Element | null, into?: object): object | null

    Decodes the given XML node. The optional "into" argument specifies an existing object to be used. If no object is given, then a new instance is created using the constructor from the codec.

    The function returns the passed in object or the new instance if no object was given.

    Parameters

    • node: Element | null

      XML node to be decoded.

    • Optionalinto: object

      Optional object to be decoded into.

    Returns object | null

    • Defined in serialization/Codec.ts:355

decodeCell

  • decodeCell(node: Element, restoreStructures?: boolean): Cell | null

    Decodes cells that have been encoded using inversion, ie. where the user object is the enclosing node in the XML, and restores the group and graph structure in the cells. Returns a new Cell instance that represents the given node.

    Parameters

    • node: Element

      XML node that contains the cell data.

    • restoreStructures: boolean = true

      Optional boolean indicating whether the graph structure should be restored by calling insert and insertEdge on the parent and terminals, respectively. Default is true.

    Returns Cell | null

    • Defined in serialization/Codec.ts:431

encode

  • encode(obj: any): Element | null

    Encodes the specified object and returns the resulting XML node.

    Parameters

    • obj: any

      Object to be encoded.

    Returns Element | null

    • Defined in serialization/Codec.ts:326

encodeCell

  • encodeCell(cell: Cell, node: Node, includeChildren?: boolean): void

    Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitly used by the respective object encoders (e.g. ModelCodec, ChildChangeCodec and RootChangeCodec). This implementation writes the given cell and its children as a (flat) sequence into the given node. The children are not encoded if the optional includeChildren is false. The function is in charge of adding the result into the given node and has no return value.

    Parameters

    • cell: Cell

      Cell to be encoded.

    • node: Node

      Parent XML node to add the encoded cell into.

    • OptionalincludeChildren: boolean

      Optional boolean indicating if the function should include all descendents. Default is true.

    Returns void

    • Defined in serialization/Codec.ts:391

getElementById

  • getElementById(id: string): Element

    Returns the element with the given ID from document.

    Parameters

    • id: string

      String that contains the ID.

    Returns Element

    • Defined in serialization/Codec.ts:233

getId

  • getId(obj: any): string

    Returns the ID of the specified object. This implementation calls reference first and if that returns null handles the object as an Cell by returning their IDs using Cell.getId. If no ID exists for the given cell, then an on-the-fly ID is generated using CellPath.create.

    Parameters

    • obj: any

      Object to return the ID for.

    Returns string

    • Defined in serialization/Codec.ts:278

getObject

  • getObject(id: string): any

    Returns the decoded object for the element with the specified ID in document. If the object is not known then lookup is used to find an object. If no object is found, then the element with the respective ID from the document is parsed using decode.

    Parameters

    • id: string

    Returns any

    • Defined in serialization/Codec.ts:186

insertIntoGraph

  • insertIntoGraph(cell: Cell): void

    Inserts the given cell into its parent and terminal cells.

    Parameters

    • cell: Cell

    Returns void

    • Defined in serialization/Codec.ts:467

isCellCodec

  • isCellCodec(codec: ObjectCodec | null): boolean

    Returns true if the given codec is a cell codec. This uses CellCodec.isCellCodec to check if the codec is of the given type.

    Parameters

    • codec: ObjectCodec | null

    Returns boolean

    • Defined in serialization/Codec.ts:411

lookup

  • lookup(id: string): any

    Hook for subclassers to implement a custom lookup mechanism for cell IDs. This implementation always returns null.

    Example:

    const codec = new Codec();
    codec.lookup(id)
    {
    return model.getCell(id);
    };

    Parameters

    • id: string

      ID of the object to be returned.

    Returns any

    • Defined in serialization/Codec.ts:224

putObject

  • putObject(id: string, obj: any): any

    Associates the given object with the given ID and returns the given object.

    Parameters

    • id: string

      ID for the object to be associated with.

    • obj: any

      Object to be associated with the ID.

    Returns any

    • Defined in serialization/Codec.ts:175

reference

  • reference(obj: any): any

    Hook for subclassers to implement a custom method for retrieving IDs from objects. This implementation always returns null.

    Example:

    const codec = new Codec();
    codec.reference(obj)
    {
    return obj.getCustomId();
    };

    Parameters

    • obj: any

      Object whose ID should be returned.

    Returns any

    • Defined in serialization/Codec.ts:317

setAttribute

  • setAttribute(node: Element, attribute: string, value: any): void

    Sets the attribute on the specified node to value. This is a helper method that makes sure the attribute and value arguments are not null.

    Parameters

    • node: Element

      XML node to set the attribute for.

    • attribute: string

      The name of the attribute to be set.

    • value: any

      New value of the attribute.

    Returns void

    • Defined in serialization/Codec.ts:503

updateElements

  • updateElements(): void

    Returns void

    • Defined in serialization/Codec.ts:238

Settings

Member Visibility
  • Protected
  • Inherited
Theme

On This Page

ReferencesExamples
  • Example
  • Example
  • Example
Debugging
Constructors
constructor
Properties
documentelementsencodeDefaultsobjects
Methods
addElementdecodedecodeCellencodeencodeCellgetElementByIdgetIdgetObjectinsertIntoGraphisCellCodeclookupputObjectreferencesetAttributeupdateElements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.