Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

In this page you can find the example usage for org.dom4j Element addAttribute.

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeSetTabIndex(Element parentElement, SetTabIndex tag) {
    Element element = parentElement.addElement("settabindex");
    element.addAttribute("depth", Integer.toString(tag.getDepth()));
    element.addAttribute("tabindex", Integer.toString(tag.getTabIndex()));
}

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeSoundStreamHead(Element parentElement, SoundStreamHead tag) {
    Element element = parentElement.addElement("soundstreamhead");
    element.addAttribute("streamformat", getSoundFormatString(tag.getStreamFormat()));
    element.addAttribute("streamrate", getSoundRateString(tag.getStreamRate()));
    if (tag.isStreamStereo()) {
        element.addAttribute("streamstereo", "true");
    }/*from  ww  w.java 2  s. co m*/
    element.addAttribute("streamsamplecount", Integer.toString(tag.getStreamSampleCount()));
    element.addAttribute("playbackrate", getSoundRateString(tag.getPlaybackRate()));
    if (tag.isPlaybackStereo()) {
        element.addAttribute("playbackstereo", "true");
    }
    if (tag.getStreamFormat() == SoundStreamHead.FORMAT_MP3) {
        element.addAttribute("latencyseek", Short.toString(tag.getLatencySeek()));
    }
}

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeSoundStreamHead2(Element parentElement, SoundStreamHead2 tag) {
    Element element = parentElement.addElement("soundstreamhead2");
    element.addAttribute("streamformat", getSoundFormatString(tag.getStreamFormat()));
    element.addAttribute("streamrate", getSoundRateString(tag.getStreamRate()));
    if (tag.isStream16BitSample()) {
        element.addAttribute("streamsample16bit", "true");
    }/*w w  w .  j  a v  a2  s  . c o m*/
    if (tag.isStreamStereo()) {
        element.addAttribute("streamstereo", "true");
    }
    element.addAttribute("streamsamplecount", Integer.toString(tag.getStreamSampleCount()));
    element.addAttribute("playbackrate", getSoundRateString(tag.getPlaybackRate()));
    if (tag.isPlayback16BitSample()) {
        element.addAttribute("playbacksample16bit", "true");
    }
    if (tag.isPlaybackStereo()) {
        element.addAttribute("playbackstereo", "true");
    }
    if (tag.getStreamFormat() == SoundStreamHead.FORMAT_MP3) {
        element.addAttribute("latencyseek", Short.toString(tag.getLatencySeek()));
    }
}

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeStartSound(Element parentElement, StartSound tag) {
    Element element = parentElement.addElement("startsound");
    element.addAttribute("soundid", Integer.toString(tag.getSoundId()));
    RecordXMLWriter.writeSoundInfo(element, tag.getSoundInfo());
}

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeUnknownTag(Element parentElement, UnknownTag tag) {
    Element element = parentElement.addElement("unknowntag");
    element.addAttribute("code", Integer.toString(tag.getCode()));
    element.addText(Base64.encode(tag.getData()));
}

From source file:com.jswiff.xml.TagXMLWriter.java

License:Open Source License

private static void writeVideoFrame(Element parentElement, VideoFrame tag) {
    Element element = parentElement.addElement("videoframe");
    element.addAttribute("streamid", Integer.toString(tag.getStreamId()));
    element.addAttribute("framenum", Integer.toString(tag.getFrameNum()));
    element.addElement("videodata").addText(Base64.encode(tag.getVideoData()));
}

From source file:com.jswiff.xml.XMLWriter.java

License:Open Source License

private void writeHeader() {
    Element headerElement = rootElement.addElement("header");
    headerElement.addAttribute("swfversion", Integer.toString(swfDocument.getVersion()));
    if (swfDocument.isCompressed()) {
        headerElement.addAttribute("compressed", "true");
    }//www  .j ava 2s . c om
    Element frames = headerElement.addElement("frames");
    frames.addAttribute("count", Integer.toString(swfDocument.getFrameCount()));
    frames.addAttribute("rate", Integer.toString(swfDocument.getFrameRate()));
    Rect size = swfDocument.getFrameSize();
    RecordXMLWriter.writeRect(frames, "size", size);
    Color bgColor = swfDocument.getBackgroundColor();
    if (bgColor != null) {
        RecordXMLWriter.writeColor(headerElement, "bgcolor", bgColor);
    }
    if (swfDocument.getVersion() >= 8) {
        switch (swfDocument.getAccessMode()) {
        case SWFDocument.ACCESS_MODE_LOCAL:
            headerElement.addAttribute("access", "local");
            break;
        case SWFDocument.ACCESS_MODE_NETWORK:
            headerElement.addAttribute("access", "network");
            break;
        }
        String metadata = swfDocument.getMetadata();
        if (metadata != null) {
            headerElement.addElement("metadata").addText(Base64.encodeString(metadata));
        }
    }
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected List<Component> getAllComponents() throws Exception {
    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();

    String taglibsXML = "<components></components>";

    Document taglibsDoc = SAXReaderUtil
            .read(new InputSource(new ByteArrayInputStream(taglibsXML.getBytes("utf-8"))));

    Element root = taglibsDoc.getRootElement();

    for (Document currentDoc : getComponentDefinitionDocs()) {
        currentDoc = _getExtendedDocument(currentDoc);

        Element currentRoot = currentDoc.getRootElement();

        String defaultPackage = currentRoot.attributeValue("short-name");
        List<Element> extComponentNodes = currentRoot.elements("component");

        for (Element extComponent : extComponentNodes) {
            String extComponentPackage = Convert.toString(extComponent.attributeValue("package"),
                    defaultPackage);//w ww  . j  a  v a 2  s .c  o  m

            extComponent.addAttribute("package", extComponentPackage);
        }

        Element authors = currentRoot.element("author");

        List<Element> components = currentRoot.elements("component");

        for (Element component : components) {
            Element copy = component.createCopy();
            Element componentAuthors = copy.element("authors");

            if ((authors != null) && (componentAuthors == null)) {
                copy.add(authors.createCopy());
            }

            root.add(copy);
        }

        List<org.dom4j.Attribute> attributes = currentRoot.attributes();

        for (org.dom4j.Attribute attribute : attributes) {
            root.addAttribute(attribute.getName(), attribute.getValue());
        }
    }

    doc.add(root.createCopy());

    return getComponents(doc);
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Document mergeXMLAttributes(Document doc1, Document doc2) {
    Element doc2Root = doc2.getRootElement();
    Element doc1Root = doc1.getRootElement();
    Element docRoot = doc2Root.createCopy();

    docRoot.clearContent();//w ww  .j  ava 2  s .  com

    if (doc1Root != null) {
        Iterator<Object> attributesIterator = doc1Root.attributeIterator();

        while (attributesIterator.hasNext()) {
            org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

            if (attribute.getName().equals("extends")) {
                continue;
            }

            docRoot.addAttribute(attribute.getName(), attribute.getValue());
        }

        Element descriptionElement = doc1Root.element("description");

        if (descriptionElement != null) {
            docRoot.add(descriptionElement.createCopy());
        }
    }

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();
    doc.setRootElement(docRoot);

    List<Element> doc2Components = doc2Root.elements(_COMPONENT);

    for (Element doc2Component : doc2Components) {
        Element component = doc2Component.createCopy();

        String name = doc2Component.attributeValue("name");

        Element doc1Component = getComponentNode(doc1, name);

        if (doc1Component != null) {
            Element doc1ComponentDescriptionElement = doc1Component.element("description");

            if (doc1ComponentDescriptionElement != null) {
                Element descriptionElement = component.element("description");

                if (descriptionElement != null) {
                    component.remove(descriptionElement);
                }

                component.add(doc1ComponentDescriptionElement.createCopy());
            }

            Iterator<Object> attributesIterator = doc1Component.attributeIterator();

            while (attributesIterator.hasNext()) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

                component.addAttribute(attribute.getName(), attribute.getValue());
            }

            Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES);

            Element attributesNode = component.element(_ATTRIBUTES);

            if ((doc1AttributesNode != null) && (attributesNode != null)) {
                List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE);

                List<Element> attributes = attributesNode.elements(_ATTRIBUTE);

                for (Element doc1Attribute : doc1Attributes) {
                    Element attribute = getElementByName(attributes, doc1Attribute.elementText("name"));

                    if (attribute != null) {
                        attributesNode.remove(attribute);
                    }

                    attributesNode.add(doc1Attribute.createCopy());
                }
            }

            Element doc1EventsNode = doc1Component.element(_EVENTS);

            Element eventsNode = component.element(_EVENTS);

            if ((doc1EventsNode != null) && (eventsNode != null)) {
                List<Element> doc1Events = doc1EventsNode.elements(_EVENT);

                List<Element> events = eventsNode.elements(_EVENT);

                for (Element doc1Event : doc1Events) {
                    Element event = getElementByName(events, doc1Event.elementText("name"));

                    if (event != null) {
                        eventsNode.add(event);
                    }

                    eventsNode.add(doc1Event.createCopy());
                }
            }
        }

        doc.getRootElement().add(component);
    }

    if (doc1Root != null) {
        List<Element> doc1Components = doc1Root.elements(_COMPONENT);

        for (Element doc1Component : doc1Components) {
            Element component = doc1Component.createCopy();

            String name = doc1Component.attributeValue("name");

            Element doc2Component = getComponentNode(doc2, name);

            if (doc2Component == null) {
                doc.getRootElement().add(component);
            }
        }
    }

    return doc;
}

From source file:com.liferay.alloy.tools.transformer.AlloyDocsTransformer.java

License:Open Source License

private void _createXML() {
    ArrayList<Component> components = getComponents();

    Document doc = DocumentFactory.getInstance().createDocument();
    Element root = doc.addElement("components");

    root.addAttribute("short-name", _DEFAULT_TAGLIB_SHORT_NAME);
    root.addAttribute("uri", _DEFAULT_TAGLIB_URI);
    root.addAttribute("tlib-version", _DEFAULT_TAGLIB_VERSION);

    for (Component component : components) {
        Element componentNode = root.addElement("component");

        componentNode.addAttribute("name", component.getName());
        componentNode.addAttribute("module", component.getModule());
        componentNode.addAttribute("package", component.getPackage());
        componentNode.addAttribute("bodyContent", String.valueOf(component.isBodyContent()));
        componentNode.addAttribute("alloyComponent", String.valueOf(component.isAlloyComponent()));

        Element descriptionNode = componentNode.addElement("description");
        descriptionNode.addCDATA(component.getDescription());
        Element attributesNode = componentNode.addElement("attributes");
        Element eventsNode = componentNode.addElement("events");

        for (Attribute attribute : component.getAttributes()) {
            Element attributeNode = attributesNode.addElement("attribute");

            Element defaultValueNode = attributeNode.addElement("defaultValue");
            Element attributeDescriptionNode = attributeNode.addElement("description");
            Element javaScriptTypeNode = attributeNode.addElement("javaScriptType");
            Element nameNode = attributeNode.addElement("name");
            Element readOnlyNode = attributeNode.addElement("readOnly");

            defaultValueNode.setText(attribute.getDefaultValue());
            attributeDescriptionNode.addCDATA(_getAttributeDescription(attribute));
            javaScriptTypeNode.setText(attribute.getJavaScriptType());
            nameNode.setText(attribute.getName());
            readOnlyNode.setText(Boolean.toString(attribute.isReadOnly()));
        }//from  w ww  .  ja v  a 2  s . com

        for (Attribute event : component.getEvents()) {
            Element eventNode = eventsNode.addElement("event");
            Element nameNode = eventNode.addElement("name");
            Element typeNode = eventNode.addElement("type");
            Element elementDescriptionNode = eventNode.addElement("description");

            nameNode.setText(event.getName());
            elementDescriptionNode.addCDATA(_getAttributeDescription(event));
            typeNode.setText(event.getType());
        }
    }

    try {
        File file = new File(_outputXML);

        file.getParentFile().mkdirs();

        FileOutputStream fos = new FileOutputStream(file);

        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(fos, format);

        writer.write(doc);
        writer.flush();

        System.out.println("Writing " + _outputXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
}