Search in sources :

Example 31 with XMLElement

use of net.n3.nanoxml.XMLElement in project freeplane by freeplane.

the class PresentationWriter method writeSlide.

private void writeSlide(XMLElement xmlPresentation, Slide s) {
    XMLElement xmlSlide = xmlPresentation.createElement(SLIDE);
    xmlPresentation.addChild(xmlSlide);
    xmlSlide.setAttribute(NAME, s.getName());
    if (s.showsAncestors())
        xmlSlide.setAttribute(SHOWS_ANCESTORS, TRUE);
    if (s.showsDescendants())
        xmlSlide.setAttribute(SHOWS_DESCENDANTS, TRUE);
    if (s.showsOnlySpecificNodes())
        xmlSlide.setAttribute(SHOWS_ONLY_SPECIFIC_NODES, TRUE);
    if (s.changesZoom())
        xmlSlide.setAttribute(CHANGES_ZOOM, TRUE);
    final String placedNodeId = s.getPlacedNodeId();
    if (placedNodeId != null) {
        xmlSlide.setAttribute(PLACED_NODE_ID, placedNodeId);
    }
    final NodePosition placedNodePosition = s.getPlacedNodePosition();
    if (placedNodePosition != NodePosition.CENTER) {
        xmlSlide.setAttribute(PLACED_NODE_POSITION, placedNodePosition.name());
    }
    float zoom = s.getZoom();
    if (zoom != 1f)
        xmlSlide.setAttribute(ZOOM, Float.toString(zoom));
    ASelectableCondition filterCondition = s.getFilterCondition();
    if (filterCondition != null) {
        XMLElement xmlCondition = new XMLElement(SLIDE_CONDITION);
        filterCondition.toXml(xmlCondition);
        xmlSlide.addChild(xmlCondition);
    }
    XMLElement xmlNodes = new XMLElement(NODES_ON_SLIDE);
    for (String nodeId : s.getSelectedNodeIds()) {
        if (map.getNodeForID(nodeId) != null) {
            XMLElement xmlNode = new XMLElement(NODE_ON_SLIDE);
            xmlNode.setAttribute(NODE_ID, nodeId);
            xmlNodes.addChild(xmlNode);
        }
    }
    if (xmlNodes.hasChildren())
        xmlSlide.addChild(xmlNodes);
    if (s.foldsNodes()) {
        XMLElement xmlFoldedNodes = new XMLElement(FOLDED_NODES);
        for (String nodeId : s.getFoldedNodeIds()) {
            if (map.getNodeForID(nodeId) != null) {
                XMLElement xmlNode = new XMLElement(NODE_ON_SLIDE);
                xmlNode.setAttribute(NODE_ID, nodeId);
                xmlFoldedNodes.addChild(xmlNode);
            }
        }
        xmlSlide.addChild(xmlFoldedNodes);
    }
}
Also used : XMLElement(org.freeplane.n3.nanoxml.XMLElement) NodePosition(org.freeplane.features.map.IMapSelection.NodePosition) ASelectableCondition(org.freeplane.features.filter.condition.ASelectableCondition)

Example 32 with XMLElement

use of net.n3.nanoxml.XMLElement in project freeplane by freeplane.

the class PresentationWriter method register.

void register(MapController mapController, final PresentationController presentationController) {
    mapController.getReadManager().addElementHandler("hook", new IElementDOMHandler() {

        private final ConditionFactory conditionFactory = FilterController.getCurrentFilterController().getConditionFactory();

        @Override
        public Object createElement(Object parent, String tag, XMLElement attributes) {
            if (attributes == null) {
                return null;
            }
            if (!PRESENTATIONS.equals(attributes.getAttribute("NAME", null))) {
                return null;
            }
            return parent;
        }

        @Override
        public void endElement(Object parent, String tag, Object element, XMLElement dom) {
            final NodeModel node = (NodeModel) parent;
            final MapModel map = node.getMap();
            final NamedElementFactory<Slide> slideFactory = presentationController.getSlideFactory(map);
            MapPresentations mapPresentationExtension = presentationController.getPresentations(map);
            NamedElementCollection<Presentation> presentations = mapPresentationExtension.presentations;
            Enumeration<XMLElement> xmlPresentations = dom.enumerateChildren();
            while (xmlPresentations.hasMoreElements()) {
                XMLElement xmlPresentation = xmlPresentations.nextElement();
                presentations.add(xmlPresentation.getAttribute(NAME, "noname"));
                Enumeration<XMLElement> xmlSlides = xmlPresentation.enumerateChildren();
                NamedElementCollection<Slide> slides = presentations.getCurrentElement().slides;
                while (xmlSlides.hasMoreElements()) {
                    XMLElement xmlSlide = xmlSlides.nextElement();
                    final String name = xmlSlide.getAttribute(NAME, "noname");
                    Slide s = slideFactory.create(name);
                    Slide slide = applySlideAttributes(xmlSlide, s);
                    slides.add(slide);
                }
                if (slides.getSize() > 1)
                    slides.selectCurrentElement(0);
            }
            if (presentations.getSize() > 1)
                presentations.selectCurrentElement(0);
            node.addExtension(mapPresentationExtension);
        }

        Slide applySlideAttributes(XMLElement xmlSlide, Slide s) {
            s.setShowsAncestors(toBoolean(xmlSlide, SHOWS_ANCESTORS));
            s.setShowsDescendants(toBoolean(xmlSlide, SHOWS_DESCENDANTS));
            s.setShowsOnlySpecificNodes(toBoolean(xmlSlide, SHOWS_ONLY_SPECIFIC_NODES));
            s.setChangesZoom(toBoolean(xmlSlide, CHANGES_ZOOM));
            final String centeredNodeId = toString(xmlSlide, CENTERED_NODE_ID);
            if (centeredNodeId != null) {
                s.setPlacedNodeId(centeredNodeId);
            }
            final String placedNodeId = toString(xmlSlide, PLACED_NODE_ID);
            if (placedNodeId != null) {
                s.setPlacedNodeId(placedNodeId);
            }
            final String nodePosition = xmlSlide.getAttribute(PLACED_NODE_POSITION, NodePosition.CENTER.name());
            s.setPlacedNodePosition(NodePosition.valueOf(nodePosition));
            s.setZoom(toFloat(xmlSlide, ZOOM));
            Enumeration<XMLElement> childAttributes = xmlSlide.enumerateChildren();
            while (childAttributes.hasMoreElements()) {
                XMLElement xmlElement = childAttributes.nextElement();
                if (xmlElement.getName().equals(NODES_ON_SLIDE)) {
                    Set<String> ids = loadSpecificNodeIds(xmlElement);
                    s.setSelectedNodeIds(ids);
                } else if (xmlElement.getName().equals(FOLDED_NODES)) {
                    Set<String> ids = loadSpecificNodeIds(xmlElement);
                    s.setFoldedNodeIDs(ids);
                } else if (xmlElement.getName().equals(SLIDE_CONDITION)) {
                    ASelectableCondition condition = loadFilterCondition(xmlElement);
                    s.setFilterCondition(condition);
                }
            }
            return s;
        }

        private Set<String> loadSpecificNodeIds(XMLElement xmlNodeIds) {
            LinkedHashSet<String> nodeIds = new LinkedHashSet<>();
            Enumeration<XMLElement> nodesEnumeration = xmlNodeIds.enumerateChildren();
            while (nodesEnumeration.hasMoreElements()) {
                XMLElement nodeIdXml = nodesEnumeration.nextElement();
                if (nodeIdXml.getName().equals(NODE_ON_SLIDE)) {
                    String id = nodeIdXml.getAttribute(NODE_ID, null);
                    if (id != null)
                        nodeIds.add(id);
                }
            }
            return nodeIds;
        }

        private ASelectableCondition loadFilterCondition(XMLElement xmlElement) {
            return conditionFactory.loadCondition(xmlElement.getChildAtIndex(0));
        }

        private float toFloat(XMLElement element, String attribute) {
            return Float.parseFloat(element.getAttribute(attribute, "1f"));
        }

        private boolean toBoolean(XMLElement element, String attribute) {
            return Boolean.parseBoolean(element.getAttribute(attribute, ""));
        }

        private String toString(XMLElement element, String attribute) {
            return element.getAttribute(attribute, null);
        }
    });
    mapController.getWriteManager().addExtensionElementWriter(MapPresentations.class, new IExtensionElementWriter() {

        @Override
        public void writeContent(ITreeWriter writer, Object element, IExtension extension) throws IOException {
            new PresentationWriter(((NodeModel) element).getMap()).writeContent(writer, extension);
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Enumeration(java.util.Enumeration) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) IElementDOMHandler(org.freeplane.core.io.IElementDOMHandler) MapModel(org.freeplane.features.map.MapModel) IOException(java.io.IOException) XMLElement(org.freeplane.n3.nanoxml.XMLElement) ASelectableCondition(org.freeplane.features.filter.condition.ASelectableCondition) IExtensionElementWriter(org.freeplane.core.io.IExtensionElementWriter) NodeModel(org.freeplane.features.map.NodeModel) ConditionFactory(org.freeplane.features.filter.condition.ConditionFactory) IExtension(org.freeplane.core.extension.IExtension) ITreeWriter(org.freeplane.core.io.ITreeWriter)

Example 33 with XMLElement

use of net.n3.nanoxml.XMLElement in project jwt by emweb.

the class RenderUtils method parseXHTML.

// private static void printXmlTree(XMLElement e, int level) {
// for (Object o : e.getChildren()) {
// XMLElement c = ((XMLElement)o);
// for (int i = 0; i < level; ++i)
// System.err.print("\t");
// System.err.print(c.getName() + " : " + c.getContent());
// System.err.print("\n");
// 
// if (c.getChildren().size() > 0)
// printXmlTree(c, level + 1);
// }
// }
static XMLElement parseXHTML(String xhtml) {
    IXMLParser parser;
    try {
        xhtml = "<div>" + xhtml + "</div>";
        parser = XMLParserFactory.createDefaultXMLParser();
        IXMLReader reader = StdXMLReader.stringReader(xhtml);
        parser.setReader(reader);
        parser.setResolver(new XHtmlFilter(true));
        XMLElement xml = (XMLElement) parser.parse();
        extractTextNodes(xml);
        return xml;
    } catch (ClassNotFoundException e) {
        logger.info("Exception while parsing xhtml", e);
        logger.trace("xhtml was: {}", xhtml);
    } catch (InstantiationException e) {
        logger.info("Exception while parsing xhtml", e);
        logger.trace("xhtml was: {}", xhtml);
    } catch (IllegalAccessException e) {
        logger.info("Exception while parsing xhtml", e);
        logger.trace("xhtml was: {}", xhtml);
    } catch (XMLException e) {
        logger.info("Exception while parsing xhtml: {}", e.toString(), e);
        logger.trace("xhtml was: {}", xhtml);
    }
    return null;
}
Also used : XMLException(net.n3.nanoxml.XMLException) IXMLReader(net.n3.nanoxml.IXMLReader) IXMLParser(net.n3.nanoxml.IXMLParser) XMLElement(net.n3.nanoxml.XMLElement) IXMLElement(net.n3.nanoxml.IXMLElement) XHtmlFilter(eu.webtoolkit.jwt.XHtmlFilter)

Example 34 with XMLElement

use of net.n3.nanoxml.XMLElement in project freeplane by freeplane.

the class WindowConfigurationStorage method unmarschall.

protected XMLElement unmarschall(final String marshalled, final JDialog dialog) {
    if (marshalled != null) {
        final IXMLParser parser = XMLParserFactory.createDefaultXMLParser();
        final IXMLReader xmlReader = new StdXMLReader(new StringReader(marshalled));
        parser.setReader(xmlReader);
        try {
            final XMLElement storage = (XMLElement) parser.parse();
            if (storage != null) {
                x = Integer.parseInt(storage.getAttribute("x", "-1"));
                y = Integer.parseInt(storage.getAttribute("y", "-1"));
                width = Integer.parseInt(storage.getAttribute("width", "-1"));
                height = Integer.parseInt(storage.getAttribute("height", "-1"));
                UITools.setBounds(dialog, x, y, width, height);
                return storage;
            }
        } catch (final NumberFormatException e) {
            LogUtils.severe(e);
        } catch (final XMLException e) {
            LogUtils.severe(e);
        }
    }
    final Frame rootFrame = JOptionPane.getFrameForComponent(dialog);
    final Dimension prefSize = rootFrame.getSize();
    prefSize.width = prefSize.width * 3 / 4;
    prefSize.height = prefSize.height * 3 / 4;
    dialog.setSize(prefSize);
    return null;
}
Also used : XMLException(org.freeplane.n3.nanoxml.XMLException) Frame(java.awt.Frame) IXMLReader(org.freeplane.n3.nanoxml.IXMLReader) IXMLParser(org.freeplane.n3.nanoxml.IXMLParser) StringReader(java.io.StringReader) StdXMLReader(org.freeplane.n3.nanoxml.StdXMLReader) Dimension(java.awt.Dimension) XMLElement(org.freeplane.n3.nanoxml.XMLElement)

Example 35 with XMLElement

use of net.n3.nanoxml.XMLElement in project freeplane by freeplane.

the class StdXMLBuilder method endElement.

/**
 * This method is called when the end of an XML elemnt is encountered.
 *
 * @see #startElement
 * @param name
 *            the name of the element.
 * @param nsPrefix
 *            the prefix used to identify the namespace. If no namespace has
 *            been specified, this parameter is null.
 * @param nsURI
 *            the URI associated with the namespace. If no namespace has
 *            been specified, or no URI is associated with nsPrefix, this
 *            parameter is null.
 */
public void endElement(final String name, final String nsPrefix, final String nsURI) {
    final XMLElement elt = (XMLElement) stack.pop();
    if (elt.getChildrenCount() == 1) {
        final XMLElement child = elt.getChildAtIndex(0);
        if (child.getName() == null) {
            elt.setContent(child.getContent());
            elt.removeChildAtIndex(0);
        }
    }
}
Also used : XMLElement(org.freeplane.n3.nanoxml.XMLElement)

Aggregations

XMLElement (org.freeplane.n3.nanoxml.XMLElement)65 IOException (java.io.IOException)8 IXMLParser (org.freeplane.n3.nanoxml.IXMLParser)6 IXMLReader (org.freeplane.n3.nanoxml.IXMLReader)6 StdXMLReader (org.freeplane.n3.nanoxml.StdXMLReader)6 ASelectableCondition (org.freeplane.features.filter.condition.ASelectableCondition)5 NodeModel (org.freeplane.features.map.NodeModel)5 BufferedInputStream (java.io.BufferedInputStream)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 XMLWriter (org.freeplane.n3.nanoxml.XMLWriter)4 Color (java.awt.Color)3 FileWriter (java.io.FileWriter)3 Writer (java.io.Writer)3 IXMLElement (net.n3.nanoxml.IXMLElement)3 XMLElement (net.n3.nanoxml.XMLElement)3 Point (java.awt.Point)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Locale (java.util.Locale)2