/** * Initialize the tracer. * * @param fileName The name of the trace file */ public XMLTracer(String fileName) { m_fileName = fileName; // Create a new DOM DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance(); DocumentBuilder constructeur = null; try { constructeur = fabrique.newDocumentBuilder(); } catch (ParserConfigurationException e1) { System.out.println("Error creating the DOM"); // TODO Auto-generated catch block e1.printStackTrace(); } m_document = constructeur.newDocument(); // DOM's properties m_document.setXmlVersion("1.0"); m_document.setXmlStandalone(true); // date Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String date = sdf.format(cal.getTime()); // Prepare the tree m_sequence = m_document.createElement("sequence"); m_sequence.setAttribute("version", "Ernest 10.0"); m_sequence.setAttribute("date", date); m_document.appendChild(m_sequence); }
public void savePreset(String name, Layout layout) { Preset preset = addPreset(new Preset(name, layout)); try { // Create file if dont exist FileObject folder = FileUtil.getConfigFile("layoutpresets"); if (folder == null) { folder = FileUtil.getConfigRoot().createFolder("layoutpresets"); } FileObject presetFile = folder.getFileObject(name, "xml"); if (presetFile == null) { presetFile = folder.createData(name, "xml"); } // Create doc DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); final Document document = documentBuilder.newDocument(); document.setXmlVersion("1.0"); document.setXmlStandalone(true); // Write doc preset.writeXML(document); // Write XML file Source source = new DOMSource(document); Result result = new StreamResult(FileUtil.toFile(presetFile)); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
/** * Creates a DOM representation (fullfilling musicXML dtd) of the specified tune. * * @param tune A tune. * @return A DOM representation (fullfilling musicXML dtd) of the specified tune. */ public Document createMusicXmlDOM(Tune tune) { Document doc = null; try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(SCORE_PARTWISE_TAG); doc.appendChild(root); doc.setXmlVersion("1.0"); root.setAttribute("version", "2.0"); Element movementNumberEl = doc.createElement(MOVEMENT_NUMBER_TAG); root.appendChild(movementNumberEl); Element movementTitleEl = doc.createElement(MOVEMENT_TITLE_TAG); if (tune.getTitles().length > 0) movementTitleEl.appendChild(doc.createTextNode(tune.getTitles()[0])); root.appendChild(movementTitleEl); Element identificationEl = doc.createElement(IDENTIFICATION_TAG); Element encodingEl = doc.createElement(ENCODING_TAG); Element softwareEl = doc.createElement(SOFTWARE_TAG); softwareEl.appendChild(doc.createTextNode("ABC4J")); encodingEl.appendChild(softwareEl); Element encodingDateEl = doc.createElement(ENCODING_DATE_TAG); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); encodingDateEl.appendChild(doc.createTextNode(sdf.format(new Date()))); encodingEl.appendChild(encodingDateEl); identificationEl.appendChild(encodingEl); root.appendChild(identificationEl); Element partListEl = doc.createElement(PART_LIST_TAG); Element scorePartEl = doc.createElement(SCORE_PART_TAG); scorePartEl.setAttribute(ID_ATTRIBUTE, "P1"); Element partNameEl = doc.createElement(PART_NAME_TAG); Element partEl = doc.createElement(PART_TAG); partEl.setAttribute(ID_ATTRIBUTE, "P1"); // partNameEl.appendChild(doc.createTextNode(tune.getTitles()[0])); scorePartEl.appendChild(partNameEl); partListEl.appendChild(scorePartEl); root.appendChild(partListEl); root.appendChild(partEl); // Music music = tune.getMusic(); convert(doc, tune.getMusic(), partEl); /* * int measureNb = tune.getMusic().countMeasures(); for (int i=1; * i<=measureNb; i++) { Measure meas = * tune.getMusic().getMeasure(i); partEl.appendChild(convert(doc, * meas, i)); } */ } catch (Exception e) { e.printStackTrace(); } return doc; }
/** * Create a new XML document without document type using a custom document builder. * * @param aDocBuilder The document builder to use. May not be <code>null</code>. * @param eVersion The XML version to use. If <code>null</code> is passed, {@link * EXMLVersion#DEFAULT} will be used. * @return The created document. Never <code>null</code>. */ @Nonnull public static Document newDocument( @Nonnull final DocumentBuilder aDocBuilder, @Nullable final EXMLVersion eVersion) { ValueEnforcer.notNull(aDocBuilder, "DocBuilder"); final Document aDoc = aDocBuilder.newDocument(); aDoc.setXmlVersion((eVersion != null ? eVersion : EXMLVersion.DEFAULT).getVersion()); return aDoc; }
public XmiClassDiagramStar(ClassDiagram classDiagram) throws ParserConfigurationException { this.classDiagram = classDiagram; final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); this.document = builder.newDocument(); document.setXmlVersion("1.0"); document.setXmlStandalone(true); final Element xmi = document.createElement("XMI"); xmi.setAttribute("xmi.version", "1.1"); xmi.setAttribute("xmlns:UML", "href://org.omg/UML/1.3"); document.appendChild(xmi); final Element header = document.createElement("XMI.header"); xmi.appendChild(header); final Element metamodel = document.createElement("XMI.metamodel"); metamodel.setAttribute("xmi.name", "UML"); metamodel.setAttribute("xmi.version", "1.3"); header.appendChild(metamodel); final Element content = document.createElement("XMI.content"); xmi.appendChild(content); // <UML:Model xmi.id="UMLModel.4" name="Design Model" // visibility="public" isSpecification="false" isRoot="false" // isLeaf="false" isAbstract="false"> final Element model = document.createElement("UML:Model"); model.setAttribute("xmi.id", "model1"); model.setAttribute("name", "PlantUML"); content.appendChild(model); // <UML:Namespace.ownedElement> this.ownedElement = document.createElement("UML:Namespace.ownedElement"); model.appendChild(ownedElement); for (final Entity ent : classDiagram.entities().values()) { // if (fileFormat == FileFormat.XMI_ARGO && isStandalone(ent) == false) { // continue; // } final Element cla = createEntityNode(ent); ownedElement.appendChild(cla); done.add(ent); } // if (fileFormat != FileFormat.XMI_STANDARD) { for (final Link link : classDiagram.getLinks()) { addLink(link); } // } }
public void exportIntroductionPuzzle(IntroductionPuzzle puzzle, OutputStream os) throws TransformerException, ParserConfigurationException { Document xmlDoc; synchronized ( mDocumentBuilder) { // TODO: Figure out whether the DocumentBuilder is maybe synchronized // anyway xmlDoc = mDOM.createDocument(null, WebOfTrust.WOT_NAME, null); } // 1.0 does not support all Unicode characters which the String class supports. To prevent us // from having to filter all Strings, we use 1.1 xmlDoc.setXmlVersion("1.1"); Element rootElement = xmlDoc.getDocumentElement(); // We include the WoT version to have an easy way of handling bogus XML which might be created // by bugged versions. rootElement.setAttribute("Version", Long.toString(Version.getRealVersion())); Element puzzleElement = xmlDoc.createElement("IntroductionPuzzle"); puzzleElement.setAttribute( "Version", Integer.toString(INTRODUCTION_XML_FORMAT_VERSION)); /* Version of the XML format */ // This lock is actually not necessary because all values which are taken from the puzzle are // final. We leave it here just to make sure that it does // not get lost if it becomes necessary someday. synchronized (puzzle) { puzzleElement.setAttribute("ID", puzzle.getID()); puzzleElement.setAttribute("Type", puzzle.getType().toString()); puzzleElement.setAttribute("MimeType", puzzle.getMimeType()); synchronized (mDateFormat) { puzzleElement.setAttribute("ValidUntil", mDateFormat.format(puzzle.getValidUntilDate())); } Element dataElement = xmlDoc.createElement("Data"); dataElement.setAttribute("Value", Base64.encodeStandard(puzzle.getData())); puzzleElement.appendChild(dataElement); } rootElement.appendChild(puzzleElement); DOMSource domSource = new DOMSource(xmlDoc); StreamResult resultStream = new StreamResult(os); synchronized (mSerializer) { mSerializer.transform(domSource, resultStream); } }
/** * Create a new document with a document type using a custom document builder. * * @param aDocBuilder the document builder to be used. May not be <code>null</code>. * @param eVersion The XML version to use. If <code>null</code> is passed, {@link * EXMLVersion#DEFAULT} will be used. * @param sQualifiedName The qualified name to use. * @param sPublicId The public ID of the document type. * @param sSystemId The system ID of the document type. * @return The created document. Never <code>null</code>. */ @Nonnull public static Document newDocument( @Nonnull final DocumentBuilder aDocBuilder, @Nullable final EXMLVersion eVersion, @Nonnull final String sQualifiedName, @Nullable final String sPublicId, @Nullable final String sSystemId) { ValueEnforcer.notNull(aDocBuilder, "DocBuilder"); final DOMImplementation aDomImpl = aDocBuilder.getDOMImplementation(); final DocumentType aDocType = aDomImpl.createDocumentType(sQualifiedName, sPublicId, sSystemId); final Document aDoc = aDomImpl.createDocument(sSystemId, sQualifiedName, aDocType); aDoc.setXmlVersion((eVersion != null ? eVersion : EXMLVersion.DEFAULT).getVersion()); return aDoc; }
public static void generate(String directory) { try { PrintWriter writer = new PrintWriter(new File(directory + File.separator + "radargun-1.1.xsd")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); doc.setXmlVersion("1.0"); doc.setXmlStandalone(true); generate(doc); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", 3); Transformer trans = tf.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(writer); DOMSource source = new DOMSource(doc); trans.transform(source, result); writer.flush(); } catch (FileNotFoundException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (ParserConfigurationException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (TransformerConfigurationException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } catch (TransformerException e) { e .printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } }
public void exportIntroduction(OwnIdentity identity, OutputStream os) throws TransformerException { Document xmlDoc; synchronized ( mDocumentBuilder) { // TODO: Figure out whether the DocumentBuilder is maybe synchronized // anyway xmlDoc = mDOM.createDocument(null, WebOfTrust.WOT_NAME, null); } // 1.0 does not support all Unicode characters which the String class supports. To prevent us // from having to filter all Strings, we use 1.1 xmlDoc.setXmlVersion("1.1"); Element rootElement = xmlDoc.getDocumentElement(); // We include the WoT version to have an easy way of handling bogus XML which might be created // by bugged versions. rootElement.setAttribute("Version", Long.toString(Version.getRealVersion())); Element introElement = xmlDoc.createElement("IdentityIntroduction"); introElement.setAttribute( "Version", Integer.toString(XML_FORMAT_VERSION)); /* Version of the XML format */ Element identityElement = xmlDoc.createElement("Identity"); // synchronized(mWoT) { // Not necessary according to JavaDoc of identity.getRequestURI() identityElement.setAttribute("URI", identity.getRequestURI().toString()); // } introElement.appendChild(identityElement); rootElement.appendChild(introElement); DOMSource domSource = new DOMSource(xmlDoc); StreamResult resultStream = new StreamResult(os); synchronized ( mSerializer) { // TODO: Figure out whether the Serializer is maybe synchronized anyway mSerializer.transform(domSource, resultStream); } }
/** * @param featureCollection * @param writer * @param fragment : true if we write in a stream, dont write start and end elements * @throws DataStoreException */ public Element writeFeatureCollection( final FeatureCollection featureCollection, final boolean fragment, final boolean wfs) throws DataStoreException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // then we have to create document-loader: factory.setNamespaceAware(false); DocumentBuilder loader = factory.newDocumentBuilder(); // creating a new DOM-document... Document document = loader.newDocument(); // the XML header if (!fragment) { document.setXmlVersion("1.0"); // writer.writeStartDocument("UTF-8", "1.0"); } // the root Element final Element rootElement; if (wfs) { rootElement = document.createElementNS("http://www.opengis.net/wfs", "FeatureCollection"); rootElement.setPrefix("wfs"); } else { rootElement = document.createElementNS("http://www.opengis.net/gml", "FeatureCollection"); rootElement.setPrefix("gml"); } document.appendChild(rootElement); String collectionID = ""; if (featureCollection.getID() != null) { collectionID = featureCollection.getID(); } final Attr idAttribute = document.createAttributeNS(Namespaces.GML, "id"); idAttribute.setValue(collectionID); idAttribute.setPrefix("gml"); rootElement.setAttributeNodeNS(idAttribute); if (schemaLocation != null && !schemaLocation.equals("")) { rootElement.setAttributeNS( "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", schemaLocation); } /*FeatureType type = featureCollection.getFeatureType(); if (type != null && type.getName() != null) { String namespace = type.getName().getNamespaceURI(); if (namespace != null && !namespace.equals(Namespaces.GML)) { Prefix prefix = getPrefix(namespace); writer.writeNamespace(prefix.prefix, namespace); } }*/ /* * The boundedby part */ final Element boundElement = writeBounds(featureCollection.getEnvelope(), document); if (boundElement != null) { rootElement.appendChild(boundElement); } // we write each feature member of the collection FeatureIterator iterator = featureCollection.iterator(); try { while (iterator.hasNext()) { final Feature f = iterator.next(); final Element memberElement = document.createElementNS(Namespaces.GML, "featureMember"); memberElement.setPrefix("gml"); memberElement.appendChild(writeFeature(f, document, true)); rootElement.appendChild(memberElement); } } finally { // we close the stream iterator.close(); } return rootElement; }
public void exportOwnIdentity(OwnIdentity identity, OutputStream os) throws TransformerException { Document xmlDoc; synchronized ( mDocumentBuilder) { // TODO: Figure out whether the DocumentBuilder is maybe synchronized // anyway xmlDoc = mDOM.createDocument(null, WebOfTrust.WOT_NAME, null); } // 1.0 does not support all Unicode characters which the String class supports. To prevent us // from having to filter all Strings, we use 1.1 xmlDoc.setXmlVersion("1.1"); Element rootElement = xmlDoc.getDocumentElement(); // We include the WoT version to have an easy way of handling bogus XML which might be created // by bugged versions. rootElement.setAttribute("Version", Long.toString(Version.getRealVersion())); /* Create the identity Element */ Element identityElement = xmlDoc.createElement("Identity"); identityElement.setAttribute( "Version", Integer.toString(XML_FORMAT_VERSION)); /* Version of the XML format */ synchronized (mWoT) { identityElement.setAttribute("Name", identity.getNickname()); identityElement.setAttribute( "PublishesTrustList", Boolean.toString(identity.doesPublishTrustList())); /* Create the context Elements */ for (String context : identity.getContexts()) { Element contextElement = xmlDoc.createElement("Context"); contextElement.setAttribute("Name", context); identityElement.appendChild(contextElement); } /* Create the property Elements */ for (Entry<String, String> property : identity.getProperties().entrySet()) { Element propertyElement = xmlDoc.createElement("Property"); propertyElement.setAttribute("Name", property.getKey()); propertyElement.setAttribute("Value", property.getValue()); identityElement.appendChild(propertyElement); } /* Create the trust list Element and its trust Elements */ if (identity.doesPublishTrustList()) { Element trustListElement = xmlDoc.createElement("TrustList"); int trustCount = 0; for (Trust trust : mWoT.getGivenTrustsSortedDescendingByLastSeen(identity)) { if (++trustCount > MAX_IDENTITY_XML_TRUSTEE_AMOUNT) { Logger.normal( this, "Amount of trustees exceeded " + MAX_IDENTITY_XML_TRUSTEE_AMOUNT + ", not adding any more to trust list of " + identity); break; } /* We should make very sure that we do not reveal the other own identity's */ if (trust.getTruster() != identity) throw new RuntimeException( "Error in WoT: It is trying to export trust values of someone else in the trust list " + "of " + identity + ": Trust value from " + trust.getTruster() + ""); Element trustElement = xmlDoc.createElement("Trust"); trustElement.setAttribute("Identity", trust.getTrustee().getRequestURI().toString()); trustElement.setAttribute("Value", Byte.toString(trust.getValue())); trustElement.setAttribute("Comment", trust.getComment()); trustListElement.appendChild(trustElement); } identityElement.appendChild(trustListElement); } } rootElement.appendChild(identityElement); DOMSource domSource = new DOMSource(xmlDoc); StreamResult resultStream = new StreamResult(os); synchronized ( mSerializer) { // TODO: Figure out whether the Serializer is maybe synchronized anyway mSerializer.transform(domSource, resultStream); } }