List of usage examples for org.w3c.dom Element setAttributeNode
public Attr setAttributeNode(Attr newAttr) throws DOMException;
From source file:org.bigtextml.topics.ParallelTopicModel.java
public Document topicXMLReport(int numWords) { try {/*ww w . j av a 2s .co m*/ ArrayList<TreeSet<IDSorter>> topicSortedWords = getSortedWords(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("topicModel"); doc.appendChild(rootElement); for (int topic = 0; topic < numTopics; topic++) { Element topicNode = doc.createElement("topic"); topicNode.setAttributeNode(getAttribute("id", Integer.toString(topic), doc)); topicNode.setAttributeNode(getAttribute("alpha", Double.toString(alpha[topic]), doc)); topicNode .setAttributeNode(getAttribute("totalTokens", Double.toString(tokensPerTopic[topic]), doc)); rootElement.appendChild(topicNode); int word = 0; Iterator<IDSorter> iterator = topicSortedWords.get(topic).iterator(); while (iterator.hasNext() && word < numWords) { IDSorter info = iterator.next(); Element wordNode = doc.createElement("word"); wordNode.setAttributeNode(getAttribute("rank", Integer.toString(word), doc)); wordNode.setAttributeNode(getAttribute("weight", Double.toString(info.getWeight()), doc)); wordNode.appendChild(doc.createTextNode((String) alphabet.lookupObject(info.getID()))); topicNode.appendChild(wordNode); word++; } } return doc; } catch (Exception e) { throw new RuntimeException("Error generating XML Topic Report " + e); } }
From source file:org.eclipse.smila.search.datadictionary.DataDictionaryController.java
/** * Adds the index./*from w w w.j ava 2 s. c om*/ * * @param indexTypeName * the index type name * * @throws DataDictionaryException * the data dictionary exception */ public static void addIndex(final String indexTypeName) throws DataDictionaryException { synchronized (dd) { ensureLoaded(); final Enumeration<DIndex> indiceTypes = _dataDictionaryTypes.getIndices(); while (indiceTypes.hasMoreElements()) { final DIndex dIndexType = indiceTypes.nextElement(); if (dIndexType.getName().equalsIgnoreCase(indexTypeName)) { // CLONE // encode to XML final Document doc = XMLUtils.getDocument(); final Element rootElement = doc.createElementNS(DAnyFinderDataDictionaryCodec.NS, "AnyFinderDataDictionary"); Attr attr = null; attr = doc.createAttribute("xmlns:xsi"); attr.setValue("http://www.w3.org/2001/XMLSchema-instance"); rootElement.setAttributeNode(attr); attr = doc.createAttribute("xsi:schemaLocation"); attr.setValue(DAnyFinderDataDictionaryCodec.NS + " ../xml/AnyFinderDataDictionary.xsd"); rootElement.setAttributeNode(attr); doc.appendChild(rootElement); final DIndex dIndex; try { DIndexCodec.encode(dIndexType, rootElement); } catch (final DDException e) { throw new DataDictionaryException(e); } final Document doc2; try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLUtils.stream(doc.getDocumentElement(), true, "UTF-8", bos); doc2 = XMLUtils.parse(bos.toByteArray(), true); } catch (final XMLUtilsException e) { throw new DataDictionaryException(e); } final DAnyFinderDataDictionary dictionary; // decode again to DIndex try { dictionary = DAnyFinderDataDictionaryCodec.decode(doc2.getDocumentElement()); } catch (final DDException e) { throw new DataDictionaryException(e); } dIndex = dictionary.getIndex(dIndexType.getName()); addIndex(dIndex); return; } } } throw new DataDictionaryException(String.format("Index type [%s] was not found!", indexTypeName)); }
From source file:org.firesoa.common.schema.DOMInitializer.java
protected static void createAttribute(Document doc, Element elem, XmlSchemaAttributeGroupMember attrMember) { if (attrMember instanceof XmlSchemaAttribute) { XmlSchemaAttribute attrSchema = (XmlSchemaAttribute) attrMember; XmlSchemaForm attrForm = attrSchema.getForm(); Attr attr = null;// w w w. j av a 2 s. com QName attrSchemaQName = attrSchema.getQName(); if (XmlSchemaForm.QUALIFIED.equals(attrForm)) { String attrQualifiedName = getQualifiedName(doc, attrSchemaQName); attr = doc.createAttributeNS(attrSchemaQName.getNamespaceURI(), attrQualifiedName); } else { attr = doc.createAttribute(attrSchema.getName()); } elem.setAttributeNode(attr); String value = ""; if (!StringUtils.isEmpty(attrSchema.getFixedValue())) { value = attrSchema.getFixedValue(); } else if (!StringUtils.isEmpty(attrSchema.getDefaultValue())) { value = attrSchema.getDefaultValue(); } if (!StringUtils.isEmpty(value)) { attr.setValue(value); } } else if (attrMember instanceof XmlSchemaAttributeGroupRef) { XmlSchemaAttributeGroupRef attrGroupRef = (XmlSchemaAttributeGroupRef) attrMember; List<XmlSchemaAttributeGroupMember> groupMembers = attrGroupRef.getRef().getTarget().getAttributes(); for (XmlSchemaAttributeGroupMember groupMember : groupMembers) { createAttribute(doc, elem, groupMember); } } else if (attrMember instanceof XmlSchemaAttributeGroup) { XmlSchemaAttributeGroup attrGroup = (XmlSchemaAttributeGroup) attrMember; List<XmlSchemaAttributeGroupMember> groupMembers = attrGroup.getAttributes(); for (XmlSchemaAttributeGroupMember groupMember : groupMembers) { createAttribute(doc, elem, groupMember); } } }
From source file:org.firesoa.common.schema.DOMInitializer.java
private static String getQualifiedName(Document doc, QName qName) { Element rootElement = (Element) doc.getDocumentElement(); if (rootElement != null && !equalStrings(qName.getNamespaceURI(), rootElement.getNamespaceURI())) { // Attr attrTmp = rootElement.getAttributeNodeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, "ns0"); // System.out.println("===========found attrTmp=="+attrTmp); String nsPrefix = null;//from ww w . java 2s . c om nsPrefix = rootElement.lookupPrefix(qName.getNamespaceURI()); if (nsPrefix == null || nsPrefix.trim().equals("")) { int nsNumber = 1; NamedNodeMap attrMap = rootElement.getAttributes(); int length = attrMap.getLength(); for (int i = 0; i < length; i++) { Attr attr = (Attr) attrMap.item(i); String name = attr.getName(); if (name.startsWith(Constants.XMLNS_ATTRIBUTE)) { if (attr.getValue().equals(qName.getNamespaceURI())) { // Namespace? nsPrefix = attr.getLocalName(); break; } nsNumber++; } } if (nsPrefix == null) { nsPrefix = "ns" + nsNumber; } } Attr attr = doc.createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE + ":" + nsPrefix); attr.setValue(qName.getNamespaceURI()); rootElement.setAttributeNode(attr); return nsPrefix + ":" + qName.getLocalPart(); } else { return "ns0:" + qName.getLocalPart(); } }
From source file:org.globus.wsrf.tools.wsdl.TypesProcessor.java
private Element getSchema() throws Exception { Types types = this.definition.getTypes(); if (types == null) { types = definition.createTypes(); this.definition.setTypes(types); }//from w ww . ja v a2s . co m Element schema = null; List elementList = types.getExtensibilityElements(); if (elementList.isEmpty()) { UnknownExtensibilityElement extensibilityElement = new UnknownExtensibilityElement(); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = docBuilder.newDocument(); schema = doc.createElementNS(XSD_NS, "xsd:schema"); Attr nameSpace = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsd"); nameSpace.setValue(XSD_NS); schema.setAttributeNode(nameSpace); nameSpace = doc.createAttribute("targetNamespace"); nameSpace.setValue(definition.getTargetNamespace()); schema.setAttributeNode(nameSpace); schema.appendChild(doc.createTextNode("\n ")); doc.appendChild(schema); extensibilityElement.setElement(schema); types.addExtensibilityElement(extensibilityElement); } else { schema = ((UnknownExtensibilityElement) elementList.get(0)).getElement(); } return schema; }
From source file:org.globus.wsrf.tools.wsdl.TypesProcessor.java
public void addResourceProperties(QName portTypeName, Map resourcePropertyElements, Map schemaDocumentLocations) throws Exception { logger.debug("Starting to build resource properties element"); PortType portType = this.getPortType(portTypeName); QName resourceProperties = (QName) portType.getExtensionAttribute(RP); addPrefix(WSRP_NS, "wsrp"); Element schema = getSchema(); Document doc = schema.getOwnerDocument(); String schemaPrefix = ""; if (schema.getPrefix() != null) { schemaPrefix = schema.getPrefix() + ":"; }//from ww w .j a v a2 s.c o m Element properties = null; if (resourceProperties == null) { resourceProperties = new QName(portType.getQName().getLocalPart() + "GTWSDLResourceProperties"); portType.setExtensionAttribute(RP, resourceProperties); properties = doc.createElementNS(XSD_NS, schemaPrefix + "element"); properties.setAttribute("name", resourceProperties.getLocalPart()); properties.appendChild(doc.createTextNode("\n ")); schema.appendChild(properties); schema.appendChild(doc.createTextNode("\n ")); } else { NodeList elementNodes = schema.getElementsByTagNameNS(XSD_NS, "element"); String name; Element element; for (int i = 0; i < elementNodes.getLength(); i++) { element = (Element) elementNodes.item(i); name = element.getAttribute("name"); if (name != null && XmlUtils.getFullQNameFromString(name, element).getLocalPart() .equals(resourceProperties.getLocalPart())) { properties = element; break; } } if (properties == null) { throw new Exception( "Unable to find '" + resourceProperties + "' element in WSDL schema section of '" + this.definition.getDocumentBaseURI() + "' document."); } } String type = properties.getAttribute("type"); NodeList complexTypeElements; Element complexType = null; if (type != null && !type.equals("")) { complexTypeElements = schema.getElementsByTagNameNS(XSD_NS, "complexType"); Element currentType; QName currentName; QName typeName = XmlUtils.getFullQNameFromString(type, properties); for (int i = 0; i < complexTypeElements.getLength(); i++) { currentType = (Element) complexTypeElements.item(i); currentName = XmlUtils.getFullQNameFromString(currentType.getAttribute("name"), currentType); if (currentName.getLocalPart().equals(typeName.getLocalPart())) { complexType = currentType; break; } } if (complexType == null) { throw new Exception("Unable to find type entry for '" + resourceProperties + "' element"); } } else { complexTypeElements = properties.getElementsByTagNameNS(XSD_NS, "complexType"); if (complexTypeElements.getLength() > 0) { complexType = (Element) complexTypeElements.item(0); } else { complexType = doc.createElementNS(XSD_NS, schemaPrefix + "complexType"); complexType.appendChild(doc.createTextNode("\n ")); properties.appendChild(complexType); properties.appendChild(doc.createTextNode("\n ")); } } NodeList sequenceElements = complexType.getElementsByTagNameNS(XSD_NS, "sequence"); Element sequence; if (sequenceElements.getLength() > 0) { sequence = (Element) sequenceElements.item(0); } else { sequence = doc.createElementNS(XSD_NS, schemaPrefix + "sequence"); complexType.appendChild(sequence); complexType.appendChild(doc.createTextNode("\n ")); } Collection resourcePropertiesList = resourcePropertyElements.values(); Iterator elementIterator = resourcePropertiesList.iterator(); int nsCounter = 0; while (elementIterator.hasNext()) { sequence.appendChild(doc.createTextNode("\n ")); Element resourcePropertyElement = doc.createElementNS(XSD_NS, schemaPrefix + "element"); XSParticle particle = (XSParticle) elementIterator.next(); XSElementDeclaration element = (XSElementDeclaration) particle.getTerm(); String prefix = XmlUtils.getPrefix(element.getNamespace(), schema); addSchemaDefinitions(element, schemaDocumentLocations, schema); if (prefix == null) { prefix = "rpns" + String.valueOf(nsCounter++); Attr nameSpace = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix); nameSpace.setValue(element.getNamespace()); schema.setAttributeNode(nameSpace); } resourcePropertyElement.setAttribute("ref", prefix + ":" + element.getName()); resourcePropertyElement.setAttribute("minOccurs", String.valueOf(particle.getMinOccurs())); if (particle.getMaxOccursUnbounded()) { resourcePropertyElement.setAttribute("maxOccurs", String.valueOf("unbounded")); } else { resourcePropertyElement.setAttribute("maxOccurs", String.valueOf(particle.getMaxOccurs())); } sequence.appendChild(resourcePropertyElement); } sequence.appendChild(doc.createTextNode("\n ")); logger.debug("Resource properties element: " + XmlUtils.getElementAsString(properties)); }
From source file:org.globus.wsrf.tools.wsdl.WSDLPreprocessor.java
private static XSModel loadSchema(Element schema, Definition def) { // add namespaces from definition element Map definitionNameSpaces = def.getNamespaces(); Set nameSpaces = definitionNameSpaces.entrySet(); Iterator nameSpacesIterator = nameSpaces.iterator(); while (nameSpacesIterator.hasNext()) { Entry nameSpaceEntry = (Entry) nameSpacesIterator.next(); if (!"".equals((String) nameSpaceEntry.getKey()) && !schema.hasAttributeNS("http://www.w3.org/2000/xmlns/", (String) nameSpaceEntry.getKey())) { Attr nameSpace = schema.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + nameSpaceEntry.getKey()); nameSpace.setValue((String) nameSpaceEntry.getValue()); schema.setAttributeNode(nameSpace); }/* w w w .j a v a2 s .c o m*/ } LSInput schemaInput = new DOMInputImpl(); schemaInput .setStringData("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + XmlUtils.getElementAsString(schema)); log.info("Loading schema in types section of definition " + def.getDocumentBaseURI()); schemaInput.setSystemId(def.getDocumentBaseURI()); XMLSchemaLoader schemaLoader = new XMLSchemaLoader(); XSModel schemaModel = schemaLoader.load(schemaInput); log.info("Done loading"); return schemaModel; }
From source file:org.jbpm.bpel.xml.DeploymentDescriptorWriter.java
protected void writeMyRole(MyRoleDescriptor myRole, Element partnerLinkElem) { Element myRoleElem = partnerLinkElem.getOwnerDocument() .createElementNS(BpelConstants.NS_DEPLOYMENT_DESCRIPTOR, BpelConstants.ELEM_MY_ROLE); partnerLinkElem.appendChild(myRoleElem); // partner link handle String handle = myRole.getHandle(); if (handle != null) myRoleElem.setAttribute(BpelConstants.ATTR_HANDLE, handle); // service/*from w w w . j a v a2s .c om*/ QName service = myRole.getService(); if (service != null) { Attr serviceAttr = myRoleElem.getOwnerDocument().createAttribute(BpelConstants.ATTR_SERVICE); myRoleElem.setAttributeNode(serviceAttr); XmlUtil.setQNameValue(serviceAttr, service); } // port String port = myRole.getPort(); if (port != null) myRoleElem.setAttribute(BpelConstants.ATTR_PORT, port); }
From source file:org.mrgeo.resources.tms.TileMapServiceResource.java
protected static Document mrsPyramidMetadataToTileMapXml(final String raster, final String url, final MrsPyramidMetadata mpm) throws ParserConfigurationException { /*//from w ww . j av a 2 s. c o m * String tileMap = "<?xml version='1.0' encoding='UTF-8' ?>" + * "<TileMap version='1.0.0' tilemapservice='http://localhost/mrgeo-services/api/tms/1.0.0'>" + * " <Title>AfPk Elevation V2</Title>" + " <Abstract>A test of V2 MrsPyramid.</Abstract>" * + " <SRS>EPSG:4326</SRS>" + " <BoundingBox minx='68' miny='33' maxx='72' maxy='35' />" + * " <Origin x='68' y='33' />" + * " <TileFormat width='512' height='512' mime-type='image/tiff' extension='tif' />" + * " <TileSets profile='global-geodetic'>" + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/1' units-per-pixel='0.3515625' order='1' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/2' units-per-pixel='0.17578125' order='2' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/3' units-per-pixel='0.08789063' order='3' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/4' units-per-pixel='0.08789063' order='4' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/5' units-per-pixel='0.08789063' order='5' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/6' units-per-pixel='0.08789063' order='6' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/7' units-per-pixel='0.08789063' order='7' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/8' units-per-pixel='0.08789063' order='8' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/9' units-per-pixel='0.08789063' order='9' />" * + * " <TileSet href='http://localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2/10' units-per-pixel='0.08789063' order='10' />" * + " </TileSets>" + "</TileMap>"; */ final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements final Document doc = docBuilder.newDocument(); final Element rootElement = doc.createElement("TileMap"); doc.appendChild(rootElement); final Attr v = doc.createAttribute("version"); v.setValue(VERSION); rootElement.setAttributeNode(v); final Attr tilemapservice = doc.createAttribute("tilemapservice"); tilemapservice.setValue(normalizeUrl(normalizeUrl(url).replace(raster, ""))); rootElement.setAttributeNode(tilemapservice); // child elements final Element title = doc.createElement("Title"); title.setTextContent(raster); rootElement.appendChild(title); final Element abst = doc.createElement("Abstract"); abst.setTextContent(""); rootElement.appendChild(abst); final Element srs = doc.createElement("SRS"); srs.setTextContent(SRS); rootElement.appendChild(srs); final Element bbox = doc.createElement("BoundingBox"); rootElement.appendChild(bbox); final Attr minx = doc.createAttribute("minx"); minx.setValue(String.valueOf(mpm.getBounds().w)); bbox.setAttributeNode(minx); final Attr miny = doc.createAttribute("miny"); miny.setValue(String.valueOf(mpm.getBounds().s)); bbox.setAttributeNode(miny); final Attr maxx = doc.createAttribute("maxx"); maxx.setValue(String.valueOf(mpm.getBounds().e)); bbox.setAttributeNode(maxx); final Attr maxy = doc.createAttribute("maxy"); maxy.setValue(String.valueOf(mpm.getBounds().n)); bbox.setAttributeNode(maxy); final Element origin = doc.createElement("Origin"); rootElement.appendChild(origin); final Attr x = doc.createAttribute("x"); x.setValue(String.valueOf(mpm.getBounds().w)); origin.setAttributeNode(x); final Attr y = doc.createAttribute("y"); y.setValue(String.valueOf(mpm.getBounds().s)); origin.setAttributeNode(y); final Element tileformat = doc.createElement("TileFormat"); rootElement.appendChild(tileformat); final Attr w = doc.createAttribute("width"); w.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(w); final Attr h = doc.createAttribute("height"); h.setValue(String.valueOf(mpm.getTilesize())); tileformat.setAttributeNode(h); final Attr mt = doc.createAttribute("mime-type"); mt.setValue("image/tiff"); tileformat.setAttributeNode(mt); final Attr ext = doc.createAttribute("extension"); ext.setValue("tif"); tileformat.setAttributeNode(ext); final Element tilesets = doc.createElement("TileSets"); rootElement.appendChild(tilesets); final Attr profile = doc.createAttribute("profile"); profile.setValue("global-geodetic"); tilesets.setAttributeNode(profile); for (int i = 0; i <= mpm.getMaxZoomLevel(); i++) { final Element tileset = doc.createElement("TileSet"); tilesets.appendChild(tileset); final Attr href = doc.createAttribute("href"); href.setValue(normalizeUrl(normalizeUrl(url)) + "/" + i); tileset.setAttributeNode(href); final Attr upp = doc.createAttribute("units-per-pixel"); upp.setValue(String.valueOf(180d / 256d / Math.pow(2, i))); tileset.setAttributeNode(upp); final Attr order = doc.createAttribute("order"); order.setValue(String.valueOf(i)); tileset.setAttributeNode(order); } return doc; }
From source file:org.mrgeo.resources.tms.TileMapServiceResource.java
protected static Document mrsPyramidToTileMapServiceXml(final String url, final List<String> pyramidNames) throws ParserConfigurationException, DOMException, UnsupportedEncodingException { /*/* w ww .j ava 2 s.c o m*/ * String tileMapService = "<?xml version='1.0' encoding='UTF-8' ?>" + * "<TileMapService version='1.0.0' services='http://localhost/mrgeo-services/api/tms/'>" + * " <Title>Example Tile Map Service</Title>" + * " <Abstract>This is a longer description of the example tiling map service.</Abstract>" + * " <TileMaps>" + " <TileMap " + " title='AfPk Elevation V2' " + * " srs='EPSG:4326' " + " profile='global-geodetic' " + * " href='http:///localhost/mrgeo-services/api/tms/1.0.0/AfPkElevationV2' />" + * " </TileMaps>" + "</TileMapService>"; */ final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements final Document doc = docBuilder.newDocument(); final Element rootElement = doc.createElement("TileMapService"); doc.appendChild(rootElement); final Attr v = doc.createAttribute("version"); v.setValue(VERSION); rootElement.setAttributeNode(v); final Attr service = doc.createAttribute("services"); service.setValue(normalizeUrl(normalizeUrl(url).replace(VERSION, ""))); rootElement.setAttributeNode(service); // child elements final Element title = doc.createElement("Title"); title.setTextContent("Tile Map Service"); rootElement.appendChild(title); final Element abst = doc.createElement("Abstract"); abst.setTextContent("MrGeo MrsPyramid rasters available as TMS"); rootElement.appendChild(abst); final Element tilesets = doc.createElement("TileMaps"); rootElement.appendChild(tilesets); Collections.sort(pyramidNames); for (final String p : pyramidNames) { final Element tileset = doc.createElement("TileMap"); tilesets.appendChild(tileset); final Attr href = doc.createAttribute("href"); href.setValue(normalizeUrl(url) + "/" + URLEncoder.encode(p, "UTF-8")); tileset.setAttributeNode(href); final Attr maptitle = doc.createAttribute("title"); maptitle.setValue(p); tileset.setAttributeNode(maptitle); final Attr srs = doc.createAttribute("srs"); srs.setValue(SRS); tileset.setAttributeNode(srs); final Attr profile = doc.createAttribute("profile"); profile.setValue("global-geodetic"); tileset.setAttributeNode(profile); } return doc; }