List of usage examples for javax.xml.namespace QName getPrefix
public String getPrefix()
Get the prefix of this QName
.
The prefix assigned to a QName
might NOT be valid in a different context.
From source file:com.twinsoft.convertigo.engine.migration.Migration7_0_0.java
public static void migrate(final String projectName) { try {// w w w.j a v a2 s. com Map<String, Reference> referenceMap = new HashMap<String, Reference>(); XmlSchema projectSchema = null; Project project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName, false); // Copy all xsd files to project's xsd directory File destDir = new File(project.getXsdDirPath()); copyXsdOfProject(projectName, destDir); String projectWsdlFilePath = Engine.PROJECTS_PATH + "/" + projectName + "/" + projectName + ".wsdl"; File wsdlFile = new File(projectWsdlFilePath); String projectXsdFilePath = Engine.PROJECTS_PATH + "/" + projectName + "/" + projectName + ".xsd"; File xsdFile = new File(projectXsdFilePath); if (xsdFile.exists()) { // Load project schema from old XSD file XmlSchemaCollection collection = new XmlSchemaCollection(); collection.setSchemaResolver(new DefaultURIResolver() { public InputSource resolveEntity(String targetNamespace, String schemaLocation, String baseUri) { // Case of a c8o project location if (schemaLocation.startsWith("../") && schemaLocation.endsWith(".xsd")) { try { String targetProjectName = schemaLocation.substring(3, schemaLocation.indexOf("/", 3)); File pDir = new File(Engine.PROJECTS_PATH + "/" + targetProjectName); if (pDir.exists()) { File pFile = new File(Engine.PROJECTS_PATH + schemaLocation.substring(2)); // Case c8o project is already migrated if (!pFile.exists()) { Document doc = Engine.theApp.schemaManager .getSchemaForProject(targetProjectName).getSchemaDocument(); DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory.newInstance().newTransformer().transform(source, result); StringReader reader = new StringReader(writer.toString()); return new InputSource(reader); } } return null; } catch (Exception e) { Engine.logDatabaseObjectManager .warn("[Migration 7.0.0] Unable to find schema location \"" + schemaLocation + "\"", e); return null; } } else if (schemaLocation.indexOf("://") == -1 && schemaLocation.endsWith(".xsd")) { return super.resolveEntity(targetNamespace, schemaLocation, Engine.PROJECTS_PATH + "/" + projectName); } return super.resolveEntity(targetNamespace, schemaLocation, baseUri); } }); projectSchema = SchemaUtils.loadSchema(new File(projectXsdFilePath), collection); ConvertigoError.updateXmlSchemaObjects(projectSchema); SchemaMeta.setCollection(projectSchema, collection); for (Connector connector : project.getConnectorsList()) { for (Transaction transaction : connector.getTransactionsList()) { try { // Migrate transaction in case of a Web Service consumption project if (transaction instanceof XmlHttpTransaction) { XmlHttpTransaction xmlHttpTransaction = (XmlHttpTransaction) transaction; String reqn = xmlHttpTransaction.getResponseElementQName(); if (!reqn.equals("")) { boolean useRef = reqn.indexOf(";") == -1; // Doc/Literal case if (useRef) { try { String[] qn = reqn.split(":"); QName refName = new QName( projectSchema.getNamespaceContext().getNamespaceURI(qn[0]), qn[1]); xmlHttpTransaction.setXmlElementRefAffectation(new XmlQName(refName)); } catch (Exception e) { } } // RPC case else { int index, index2; try { index = reqn.indexOf(";"); String opName = reqn.substring(0, index); if ((index2 = reqn.indexOf(";", index + 1)) != -1) { String eltName = reqn.substring(index + 1, index2); String eltType = reqn.substring(index2 + 1); String[] qn = eltType.split(":"); QName typeName = new QName( projectSchema.getNamespaceContext().getNamespaceURI(qn[0]), qn[1]); String responseElementQName = opName + ";" + eltName + ";" + "{" + typeName.getNamespaceURI() + "}" + typeName.getLocalPart(); xmlHttpTransaction.setResponseElementQName(responseElementQName); } } catch (Exception e) { } } } } // Retrieve required XmlSchemaObjects for transaction QName requestQName = new QName(project.getTargetNamespace(), transaction.getXsdRequestElementName()); QName responseQName = new QName(project.getTargetNamespace(), transaction.getXsdResponseElementName()); LinkedHashMap<QName, XmlSchemaObject> map = new LinkedHashMap<QName, XmlSchemaObject>(); XmlSchemaWalker dw = XmlSchemaWalker.newDependencyWalker(map, true, false); dw.walkByElementRef(projectSchema, requestQName); dw.walkByElementRef(projectSchema, responseQName); // Create transaction schema String targetNamespace = projectSchema.getTargetNamespace(); String prefix = projectSchema.getNamespaceContext().getPrefix(targetNamespace); XmlSchema transactionSchema = SchemaUtils.createSchema(prefix, targetNamespace, XsdForm.unqualified.name(), XsdForm.unqualified.name()); // Add required prefix declarations List<String> nsList = new LinkedList<String>(); for (QName qname : map.keySet()) { String nsURI = qname.getNamespaceURI(); if (!nsURI.equals(Constants.URI_2001_SCHEMA_XSD)) { if (!nsList.contains(nsURI)) { nsList.add(nsURI); } } String nsPrefix = qname.getPrefix(); if (!nsURI.equals(targetNamespace)) { NamespaceMap nsMap = SchemaUtils.getNamespaceMap(transactionSchema); if (nsMap.getNamespaceURI(nsPrefix) == null) { nsMap.add(nsPrefix, nsURI); transactionSchema.setNamespaceContext(nsMap); } } } // Add required imports for (String namespaceURI : nsList) { XmlSchemaObjectCollection includes = projectSchema.getIncludes(); for (int i = 0; i < includes.getCount(); i++) { XmlSchemaObject xmlSchemaObject = includes.getItem(i); if (xmlSchemaObject instanceof XmlSchemaImport) { if (((XmlSchemaImport) xmlSchemaObject).getNamespace() .equals(namespaceURI)) { // do not allow import with same ns ! if (namespaceURI.equals(project.getTargetNamespace())) continue; String location = ((XmlSchemaImport) xmlSchemaObject) .getSchemaLocation(); // This is a convertigo project reference if (location.startsWith("../")) { // Copy all xsd files to xsd directory String targetProjectName = location.substring(3, location.indexOf("/", 3)); copyXsdOfProject(targetProjectName, destDir); } // Add reference addReferenceToMap(referenceMap, namespaceURI, location); // Add import addImport(transactionSchema, namespaceURI, location); } } } } QName responseTypeQName = new QName(project.getTargetNamespace(), transaction.getXsdResponseTypeName()); // Add required schema objects for (QName qname : map.keySet()) { if (qname.getNamespaceURI().equals(targetNamespace)) { XmlSchemaObject ob = map.get(qname); if (qname.getLocalPart().startsWith("ConvertigoError")) continue; transactionSchema.getItems().add(ob); // Add missing response error element and attributes if (qname.equals(responseTypeQName)) { Transaction.addSchemaResponseObjects(transactionSchema, (XmlSchemaComplexType) ob); } } } // Add missing ResponseType (with document) if (map.containsKey(responseTypeQName)) { Transaction.addSchemaResponseType(transactionSchema, transaction); } // Add everything if (map.isEmpty()) { Transaction.addSchemaObjects(transactionSchema, transaction); } // Add c8o error objects (for internal xsd edition only) ConvertigoError.updateXmlSchemaObjects(transactionSchema); // Save schema to file String transactionXsdFilePath = transaction.getSchemaFilePath(); new File(transaction.getSchemaFileDirPath()).mkdirs(); SchemaUtils.saveSchema(transactionXsdFilePath, transactionSchema); } catch (Exception e) { Engine.logDatabaseObjectManager .error("[Migration 7.0.0] An error occured while migrating transaction \"" + transaction.getName() + "\"", e); } if (transaction instanceof TransactionWithVariables) { TransactionWithVariables transactionVars = (TransactionWithVariables) transaction; handleRequestableVariable(transactionVars.getVariablesList()); // Change SQLQuery variables : i.e. {id} --> {{id}} if (transaction instanceof SqlTransaction) { String sqlQuery = ((SqlTransaction) transaction).getSqlQuery(); sqlQuery = sqlQuery.replaceAll("\\{([a-zA-Z0-9_]+)\\}", "{{$1}}"); ((SqlTransaction) transaction).setSqlQuery(sqlQuery); } } } } } else {// Should only happen for projects which version <= 4.6.0 XmlSchemaCollection collection = new XmlSchemaCollection(); String prefix = project.getName() + "_ns"; projectSchema = SchemaUtils.createSchema(prefix, project.getNamespaceUri(), XsdForm.unqualified.name(), XsdForm.unqualified.name()); ConvertigoError.addXmlSchemaObjects(projectSchema); SchemaMeta.setCollection(projectSchema, collection); for (Connector connector : project.getConnectorsList()) { for (Transaction transaction : connector.getTransactionsList()) { if (transaction instanceof TransactionWithVariables) { TransactionWithVariables transactionVars = (TransactionWithVariables) transaction; handleRequestableVariable(transactionVars.getVariablesList()); } } } } // Handle sequence objects for (Sequence sequence : project.getSequencesList()) { handleSteps(projectSchema, referenceMap, sequence.getSteps()); handleRequestableVariable(sequence.getVariablesList()); } // Add all references to project if (!referenceMap.isEmpty()) { for (Reference reference : referenceMap.values()) project.add(reference); } // Delete XSD file if (xsdFile.exists()) xsdFile.delete(); // Delete WSDL file if (wsdlFile.exists()) wsdlFile.delete(); } catch (Exception e) { Engine.logDatabaseObjectManager .error("[Migration 7.0.0] An error occured while migrating project \"" + projectName + "\"", e); } }
From source file:io.twipple.springframework.data.clusterpoint.convert.DefaultClusterpointTypeMapperTest.java
@Test public void testWriteType() throws Exception { ClusterpointDocument document = new ClusterpointDocument(); mapper.writeType(MockEntity.class, document); QName typeAlias = document.getTypeAlias(); assertNotNull(typeAlias);//from ww w.j a va 2s . c o m assertEquals("urn:io-twipple-springframework-data-clusterpoint-convert", typeAlias.getNamespaceURI()); assertEquals("MockEntity", typeAlias.getLocalPart()); assertEquals("mock", typeAlias.getPrefix()); }
From source file:com.amalto.core.history.accessor.AttributeAccessor.java
private Attr createAttribute(Node parentNode, Document domDocument) { // Ensure xsi prefix is declared if (attributeName.indexOf(':') > 0) { String attributePrefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$ String namespaceURI = domDocument.lookupNamespaceURI(attributePrefix); if (namespaceURI == null) { if ("xsi".equals(attributePrefix)) { //$NON-NLS-1$ domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); //$NON-NLS-1$ } else if ("tmdm".equals(attributePrefix)) { //$NON-NLS-1$ domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:tmdm", SkipAttributeDocumentBuilder.TALEND_NAMESPACE); //$NON-NLS-1$ } else { throw new IllegalArgumentException("Unrecognized attribute prefix: '" + attributePrefix + "'."); //$NON-NLS-1$ //$NON-NLS-2$ }/* w w w .j ava 2 s . c o m*/ } } QName qName = getQName(domDocument); Attr newAttribute = domDocument.createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart()); String prefix = qName.getPrefix(); newAttribute.setPrefix(prefix); parentNode.getAttributes().setNamedItemNS(newAttribute); return newAttribute; }
From source file:StAXEventTreeViewer.java
public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file) throws XMLStreamException, FileNotFoundException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file)); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: StartDocument startDocument = (StartDocument) event; DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion()); current.add(version);// w w w. jav a 2 s . c o m current.add(new DefaultMutableTreeNode(startDocument.isStandalone())); current.add(new DefaultMutableTreeNode(startDocument.standaloneSet())); current.add(new DefaultMutableTreeNode(startDocument.encodingSet())); current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme())); break; case XMLStreamConstants.START_ELEMENT: StartElement startElement = (StartElement) event; QName elementName = startElement.getName(); DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart()); current.add(element); current = element; if (!elementName.getNamespaceURI().equals("")) { String prefix = elementName.getPrefix(); if (prefix.equals("")) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI()); current.add(namespace); } for (Iterator it = startElement.getAttributes(); it.hasNext();) { Attribute attr = (Attribute) it.next(); DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name=" + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')"); String attURI = attr.getName().getNamespaceURI(); if (!attURI.equals("")) { String attPrefix = attr.getName().getPrefix(); if (attPrefix.equals("")) { attPrefix = "[None]"; } attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI)); } current.add(attribute); } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: Characters characters = (Characters) event; if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) { String data = characters.getData(); if (data.length() != 0) { current.add(new DefaultMutableTreeNode(characters.getData())); } } break; case XMLStreamConstants.DTD: DTD dtde = (DTD) event; current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration())); default: System.out.println(event.getClass().getName()); } } }
From source file:de.ingrid.iplug.csw.dsc.cswclient.impl.SoapRequest.java
/** * @see OpenGIS Catalogue Services Specification 2.0.2 - ISO Metadata * Application Profile 8.2.2.2/* w ww.j a va 2 s . c o m*/ */ @Override public Document doGetRecordById(String serverURL, CSWQuery query) throws Exception { // create the request OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace cswNs = fac.createOMNamespace(query.getSchema().getQName().getNamespaceURI(), query.getSchema().getQName().getPrefix()); // create method OMElement method = fac.createOMElement(Operation.GET_RECORD_BY_ID.toString(), cswNs); // add the default parameters method.addAttribute("service", CSWConstants.SERVICE_TYPE, null); // add the query specific parameters method.addAttribute("version", query.getVersion(), null); method.addAttribute("outputFormat", query.getOutputFormat().toString(), null); QName outputSchemaQN = query.getOutputSchema().getQName(); method.declareNamespace(outputSchemaQN.getNamespaceURI(), outputSchemaQN.getPrefix()); if (outputSchemaQN.getLocalPart().length() > 0) method.addAttribute("outputSchema", outputSchemaQN.getPrefix() + ":" + outputSchemaQN.getLocalPart(), null); else method.addAttribute("outputSchema", outputSchemaQN.getNamespaceURI(), null); // create Id OMElement idNode = fac.createOMElement("Id", cswNs); idNode.setText(query.getId()); method.addChild(idNode); // create ElementSetName element typename OMElement elementSetName = fac.createOMElement("ElementSetName", cswNs); elementSetName.setText(query.getElementSetName().toString()); method.addChild(elementSetName); // send the request try { return sendRequest(serverURL, method); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.evolveum.midpoint.util.DOMUtil.java
public static Element createElement(Document document, QName qname, Element parentElement, Element definitionElement) { lookupOrCreateNamespaceDeclaration(parentElement, qname.getNamespaceURI(), qname.getPrefix(), definitionElement, true);// w w w. j a v a 2 s .co m return createElement(document, qname); }
From source file:de.ingrid.iplug.csw.dsc.cswclient.impl.SoapRequest.java
/** * @see OpenGIS Catalogue Services Specification 2.0.2 - ISO Metadata * Application Profile 8.2.2.1/*w ww . j a v a 2 s. c o m*/ */ @Override public Document doGetRecords(String serverURL, CSWQuery query) throws Exception { // create the request OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace cswNs = fac.createOMNamespace(query.getSchema().getQName().getNamespaceURI(), query.getSchema().getQName().getPrefix()); // create method OMElement method = fac.createOMElement(Operation.GET_RECORDS.toString(), cswNs); method.declareNamespace(Namespace.ISO.getQName().getNamespaceURI(), Namespace.ISO.getQName().getPrefix()); method.declareNamespace(Namespace.GML.getQName().getNamespaceURI(), Namespace.GML.getQName().getPrefix()); // add the default parameters method.addAttribute("service", CSWConstants.SERVICE_TYPE, null); // add the query specific parameters method.addAttribute("version", query.getVersion(), null); method.addAttribute("outputFormat", query.getOutputFormat().toString(), null); method.addAttribute("resultType", query.getResultType().toString(), null); method.addAttribute("startPosition", Integer.toString(query.getStartPosition()), null); method.addAttribute("maxRecords", Integer.toString(query.getMaxRecords()), null); QName outputSchemaQN = query.getOutputSchema().getQName(); method.declareNamespace(outputSchemaQN.getNamespaceURI(), outputSchemaQN.getPrefix()); if (outputSchemaQN.getLocalPart().length() > 0) method.addAttribute("outputSchema", outputSchemaQN.getPrefix() + ":" + outputSchemaQN.getLocalPart(), null); else method.addAttribute("outputSchema", outputSchemaQN.getNamespaceURI(), null); // create Query element typename OMElement queryElem = fac.createOMElement("Query", cswNs); // add typeNames attribute List<String> typeNames = new ArrayList<String>(); for (TypeName typeName : query.getTypeNames()) { QName typeNameQN = typeName.getQName(); method.declareNamespace(typeNameQN.getNamespaceURI(), typeNameQN.getPrefix()); typeNames.add(typeNameQN.getPrefix() + ":" + typeNameQN.getLocalPart()); } String typeNamesValue = StringUtils.join(typeNames.toArray(), ","); queryElem.addAttribute("typeNames", typeNamesValue, null); // create ElementSetName element typename OMElement elementSetName = fac.createOMElement("ElementSetName", cswNs); elementSetName.setText(query.getElementSetName().toString()); queryElem.addChild(elementSetName); // add the Filter if (query.getConstraint() != null) { // create Constraint // make sure the constraint element is only created when // we have a filter. OMElement constraint = fac.createOMElement("Constraint", cswNs); constraint.addAttribute("version", query.getConstraintLanguageVersion(), null); queryElem.addChild(constraint); OMElement filter = XMLUtils.toOM(query.getConstraint().getDocumentElement()); constraint.addChild(filter); } method.addChild(queryElem); // send the request try { return sendRequest(serverURL, method); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:eu.delving.sip.xml.SourceConverter.java
private void handleAttribute(Path path, Attribute attr) { Path extended = path.child(Tag.attribute(attr.getName())); if (extended.equals(uniqueElementPath)) { unique = attr.getValue();// ww w . j av a2 s. com } else if (path.equals(recordRootPath)) { QName a = attr.getName(); QName attrName = new QName(a.getNamespaceURI(), "_" + a.getLocalPart(), a.getPrefix()); eventBuffer.add(eventFactory.createStartElement(attrName, null, null)); eventBuffer.add(eventFactory.createCharacters(attr.getValue())); eventBuffer.add(eventFactory.createEndElement(attrName, null)); eventBuffer.add(eventFactory.createCharacters("\n")); } }
From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalWriter.java
private QName setQNamePrefixExplicitIfNeeded(QName name) { if (name != null && StringUtils.isNotBlank(name.getNamespaceURI()) && StringUtils.isBlank(name.getPrefix())) { return setQNamePrefixExplicit(name); } else {//from w ww .ja v a2 s.com return name; } }
From source file:com.streamsets.pipeline.lib.xml.StreamingXmlParser.java
private String getNameAndTrackNs(QName name) { final String uri = name.getNamespaceURI(); if (!Strings.isNullOrEmpty(uri)) { String prefix;//from w w w .j ava2s . c o m if (!namespaceUriToPrefix.containsKey(uri)) { prefix = name.getPrefix(); if (Strings.isNullOrEmpty(prefix)) { //generate a new namespace prefix for it prefix = GENERATED_NAMESPACE_PREFIX + generatedNsPrefixCount++; } //else the element already came with a prefix, so just use that namespaceUriToPrefix.put(uri, prefix); } else { prefix = namespaceUriToPrefix.get(uri); } return prefix + ":" + name.getLocalPart(); } else { // element is in no namespace return name.getLocalPart(); } }