List of usage examples for org.dom4j QName QName
public QName(String name, Namespace namespace)
From source file:com.cladonia.xngreditor.actions.ToolsUppercaseAction.java
License:Open Source License
/** * Uppercase the attribute of a parent element that matches the passed parameter att * @param parent the parent element/* w w w .j a va 2 s . c om*/ * @param att the attribute to be matched * @return the new list of attributes * @throws Exception */ private List uppercaseAttributes(XElement parent, Attribute att) throws Exception { int attributeCount = parent.attributeCount(); List attributeList = parent.attributes(); //create an array to hold all the attributes Attribute[] attArray = new Attribute[attributeCount]; for (int cnt = 0; cnt < attributeCount; ++cnt) { //add each attribute to the array attArray[cnt] = (Attribute) attributeList.get(cnt); } //work on the array for (int cnt = 0; cnt < attributeCount; ++cnt) { Attribute attOld = attArray[cnt]; if (attOld == att) { String name = attArray[cnt].getName(); name = uppercaseString(name); String value = attArray[cnt].getValue(); Namespace ns = attArray[cnt].getNamespace(); attArray[cnt] = new XAttribute(new QName(name, ns), value); } } //then remove all previous and add all the attributes back into the document List newAttributes = Arrays.asList(attArray); return (newAttributes); }
From source file:com.cladonia.xngreditor.FileUtilities.java
License:Open Source License
/** * Create a new document from a Grammar type or a schema. * * @param schema the schema used as a template. * @param type the type of document./*from www .j a va 2s . c om*/ * * @return the new document. */ public static ExchangerDocument createDocument(XMLSchema schema, GrammarProperties type) throws IOException, SAXParseException { // System.out.println( "FileUtilities.createDocument( "+schema+", "+type+")"); XElement root = null; ExchangerDocument document = null; if (type != null) { String prefix = type.getNamespacePrefix(); if (!StringUtilities.isEmpty(prefix)) { root = new XElement(type.getRootElementName(), type.getNamespace(), type.getNamespacePrefix()); } else { root = new XElement(type.getRootElementName(), type.getNamespace()); } Vector namespaces = type.getNamespaces(); for (int i = 0; i < namespaces.size(); i++) { NamespaceProperties namespace = (NamespaceProperties) namespaces.elementAt(i); root.addNamespace(namespace.getPrefix(), namespace.getURI()); } document = new ExchangerDocument((XElement) root); root = document.getRoot(); String publicID = type.getPublicID(); String validationLocation = type.getSystemID(); if (StringUtilities.isEmpty(validationLocation)) { validationLocation = type.getValidationLocation(); } if (type.getValidationGrammar() == XMLGrammar.TYPE_DTD && !StringUtilities.isEmpty(validationLocation)) { if (!StringUtilities.isEmpty(publicID)) { document.getDocument().setDocType( new DefaultDocumentType(type.getRootElementName(), publicID, validationLocation)); } else { document.getDocument() .setDocType(new DefaultDocumentType(type.getRootElementName(), validationLocation)); } } else if (type.getValidationGrammar() == XMLGrammar.TYPE_XSD && !StringUtilities.isEmpty(validationLocation)) { validationLocation = URLUtilities.encodeURL(validationLocation); Namespace xsiNamespace = root.getNamespaceForURI("http://www.w3.org/2001/XMLSchema-instance"); if (xsiNamespace == null) { xsiNamespace = new DefaultNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.add(xsiNamespace); } String namespace = type.getNamespace(); if (!StringUtilities.isEmpty(namespace)) { root.addAttribute(new QName("schemaLocation", xsiNamespace), namespace + " " + validationLocation); } else { root.addAttribute(new QName("noNamespaceSchemaLocation", xsiNamespace), validationLocation); } } } else if (schema != null) { // bring up the root selection dialog RootSelectionDialog dialog = getRootSelectionDialog(); dialog.setSchema(schema); if (!dialog.isCancelled()) { SchemaElement element = dialog.getSelectedElement(); root = new XElement(element.getName(), element.getNamespace()); } } else { root = new XElement("xngr"); } root.setText("\n"); if (document == null) { document = new ExchangerDocument((XElement) root); } document.update(); return document; }
From source file:com.collabnet.ccf.core.ga.GenericArtifactHelper.java
License:Open Source License
/** * Creates a generic artifact XML representation out of the Java object * /*ww w .j a va2s. c om*/ * @param genericArtifact * Java object that will be represented as XML document * @return XML representation of generic artifact */ public static Document createGenericArtifactXMLDocument(GenericArtifact genericArtifact) throws GenericArtifactParsingException { Document document = DocumentHelper.createDocument(); document.setXMLEncoding(EncodingAwareObject.UTF_8); // Create XML elements with attributes Element root = addRootElement(document, ARTIFACT_ROOT_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE); switch (genericArtifact.getArtifactAction()) { case CREATE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_CREATE); break; } case DELETE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_DELETE); break; } case IGNORE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_IGNORE); break; } case UPDATE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UPDATE); break; } case RESYNC: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_RESYNC); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_ACTION + " specified."); } } switch (genericArtifact.getArtifactMode()) { case CHANGEDFIELDSONLY: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_CHANGED_FIELDS_ONLY); break; } case COMPLETE: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_COMPLETE); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_MODE + "specified."); } } ArtifactTypeValue artifactType = genericArtifact.getArtifactType(); switch (artifactType) { case ATTACHMENT: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_ATTACHMENT); String content = genericArtifact.getArtifactValue(); // TODO BASE64 validation? if (content != null) // embed content in CDATA section setValue(root, content, true); break; } case DEPENDENCY: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_DEPENDENCY); break; } case PLAINARTIFACT: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_PLAIN_ARTIFACT); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_TYPE + " specified."); } } switch (genericArtifact.getIncludesFieldMetaData()) { case TRUE: { addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_TRUE); break; } case FALSE: { addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_FALSE); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_MODE + "specified."); } } addAttribute(root, SOURCE_ARTIFACT_LAST_MODIFICATION_DATE, genericArtifact.getSourceArtifactLastModifiedDate()); addAttribute(root, TARGET_ARTIFACT_LAST_MODIFICATION_DATE, genericArtifact.getTargetArtifactLastModifiedDate()); // addAttribute(root, ARTIFACT_LAST_READ_TRANSACTION_ID, genericArtifact // .getLastReadTransactionId()); addAttribute(root, ERROR_CODE, genericArtifact.getErrorCode()); addAttribute(root, SOURCE_ARTIFACT_VERSION, genericArtifact.getSourceArtifactVersion()); addAttribute(root, TARGET_ARTIFACT_VERSION, genericArtifact.getTargetArtifactVersion()); addAttribute(root, CONFLICT_RESOLUTION_PRIORITY, genericArtifact.getConflictResolutionPriority()); // only create optional attributes if necessary if (artifactType == ArtifactTypeValue.DEPENDENCY || artifactType == ArtifactTypeValue.ATTACHMENT || artifactType == ArtifactTypeValue.PLAINARTIFACT) { addAttribute(root, DEP_PARENT_SOURCE_ARTIFACT_ID, genericArtifact.getDepParentSourceArtifactId()); addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_ID, genericArtifact.getDepParentSourceRepositoryId()); addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_KIND, genericArtifact.getDepParentSourceRepositoryKind()); addAttribute(root, DEP_PARENT_TARGET_ARTIFACT_ID, genericArtifact.getDepParentTargetArtifactId()); addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_ID, genericArtifact.getDepParentTargetRepositoryId()); addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_KIND, genericArtifact.getDepParentTargetRepositoryKind()); } // dependencies have even more optional attributes if (artifactType == ArtifactTypeValue.DEPENDENCY) { addAttribute(root, DEP_CHILD_SOURCE_ARTIFACT_ID, genericArtifact.getDepChildSourceArtifactId()); addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_ID, genericArtifact.getDepChildSourceRepositoryId()); addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_KIND, genericArtifact.getDepChildSourceRepositoryKind()); addAttribute(root, DEP_CHILD_TARGET_ARTIFACT_ID, genericArtifact.getDepChildTargetArtifactId()); addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_ID, genericArtifact.getDepChildTargetRepositoryId()); addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_KIND, genericArtifact.getDepChildTargetRepositoryKind()); } addAttribute(root, SOURCE_ARTIFACT_ID, genericArtifact.getSourceArtifactId()); addAttribute(root, SOURCE_REPOSITORY_ID, genericArtifact.getSourceRepositoryId()); addAttribute(root, SOURCE_REPOSITORY_KIND, genericArtifact.getSourceRepositoryKind()); addAttribute(root, SOURCE_SYSTEM_ID, genericArtifact.getSourceSystemId()); addAttribute(root, SOURCE_SYSTEM_KIND, genericArtifact.getSourceSystemKind()); addAttribute(root, TARGET_ARTIFACT_ID, genericArtifact.getTargetArtifactId()); addAttribute(root, TARGET_REPOSITORY_ID, genericArtifact.getTargetRepositoryId()); addAttribute(root, TARGET_REPOSITORY_KIND, genericArtifact.getTargetRepositoryKind()); addAttribute(root, TARGET_SYSTEM_ID, genericArtifact.getTargetSystemId()); addAttribute(root, TARGET_SYSTEM_KIND, genericArtifact.getTargetSystemKind()); addAttribute(root, SOURCE_SYSTEM_TIMEZONE, genericArtifact.getSourceSystemTimezone()); addAttribute(root, TARGET_SYSTEM_TIMEZONE, genericArtifact.getTargetSystemTimezone()); // addAttribute(root, SOURCE_SYSTEM_ENCODING, genericArtifact // .getSourceSystemEncoding()); // addAttribute(root, TARGET_SYSTEM_ENCODING, genericArtifact // .getTargetSystemEncoding()); addAttribute(root, TRANSACTION_ID, genericArtifact.getTransactionId()); if (genericArtifact.getAllGenericArtifactFields() != null) { // now add fields for (GenericArtifactField genericArtifactField : genericArtifact.getAllGenericArtifactFields()) { Element field = addElement(root, ARTIFACT_FIELD_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE); switch (genericArtifactField.getFieldAction()) { case APPEND: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_APPEND); break; } case DELETE: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_DELETE); break; } case REPLACE: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_REPLACE); break; } case UNKNOWN: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for field-attribute " + FIELD_ACTION + " specified."); } } addAttribute(field, FIELD_NAME, genericArtifactField.getFieldName()); addAttribute(field, FIELD_TYPE, genericArtifactField.getFieldType()); if (genericArtifactField.getFieldValueHasChanged()) { addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_TRUE); } else { addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_FALSE); } if (genericArtifact.getIncludesFieldMetaData() .equals(GenericArtifact.IncludesFieldMetaDataValue.TRUE)) { addAttribute(field, MIN_OCCURS, genericArtifactField.getMinOccursValue()); addAttribute(field, MAX_OCCURS, genericArtifactField.getMaxOccursValue()); addAttribute(field, NULL_VALUE_SUPPORTED, genericArtifactField.getNullValueSupported()); } else if (!GenericArtifactField.VALUE_UNKNOWN .equals(genericArtifactField.getAlternativeFieldName()) && genericArtifactField.getAlternativeFieldName() != null) { // if the alternative field name field has been set, we will ship it even if the other meta data has not been populated addAttribute(field, ALTERNATIVE_FIELD_NAME, genericArtifactField.getAlternativeFieldName()); } setFieldValue(field, genericArtifactField.getFieldValue(), genericArtifactField.getFieldValueType()); } } root.addAttribute( new QName(SCHEMA_LOCATION_ATTRIBUTE, new Namespace(SCHEMA_NAMESPACE_PREFIX, SCHEMA_NAMESPACE)), CCF_SCHEMA_LOCATION); return document; }
From source file:com.collabnet.ccf.core.GenericArtifactHelper.java
License:Apache License
/** * Creates a generic artifact XML representation out of the Java object * /* ww w . ja v a 2 s. c om*/ * @param genericArtifact * Java object that will be represented as XML document * @return XML representation of generic artifact */ public static Document createGenericArtifactXMLDocument(GenericArtifact genericArtifact) throws GenericArtifactParsingException { Document document = DocumentHelper.createDocument(); document.setXMLEncoding("UTF-8"); // Create XML elements with attributes Element root = addRootElement(document, ARTIFACT_ROOT_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE); switch (genericArtifact.getArtifactAction()) { case CREATE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_CREATE); break; } case DELETE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_DELETE); break; } case IGNORE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_IGNORE); break; } case UPDATE: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UPDATE); break; } case RESYNC: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_RESYNC); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_ACTION, ARTIFACT_ACTION_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_ACTION + " specified."); } } switch (genericArtifact.getArtifactMode()) { case CHANGEDFIELDSONLY: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_CHANGED_FIELDS_ONLY); break; } case COMPLETE: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_COMPLETE); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_MODE, ARTIFACT_MODE_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_MODE + "specified."); } } ArtifactTypeValue artifactType = genericArtifact.getArtifactType(); switch (artifactType) { case ATTACHMENT: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_ATTACHMENT); String content = genericArtifact.getArtifactValue(); // TODO BASE64 validation? if (content != null) // embed content in CDATA section setValue(root, content, true); break; } case DEPENDENCY: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_DEPENDENCY); break; } case PLAINARTIFACT: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_PLAIN_ARTIFACT); break; } case UNKNOWN: { addAttribute(root, ARTIFACT_TYPE, ARTIFACT_TYPE_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_TYPE + " specified."); } } switch (genericArtifact.getIncludesFieldMetaData()) { case TRUE: { addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_TRUE); break; } case FALSE: { addAttribute(root, INCLUDES_FIELD_META_DATA, INCLUDES_FIELD_META_DATA_FALSE); break; } default: { throw new GenericArtifactParsingException( "Non valid value for root-attribute " + ARTIFACT_MODE + "specified."); } } addAttribute(root, SOURCE_ARTIFACT_LAST_MODIFICATION_DATE, genericArtifact.getSourceArtifactLastModifiedDate()); addAttribute(root, TARGET_ARTIFACT_LAST_MODIFICATION_DATE, genericArtifact.getTargetArtifactLastModifiedDate()); // addAttribute(root, ARTIFACT_LAST_READ_TRANSACTION_ID, genericArtifact // .getLastReadTransactionId()); addAttribute(root, ERROR_CODE, genericArtifact.getErrorCode()); addAttribute(root, SOURCE_ARTIFACT_VERSION, genericArtifact.getSourceArtifactVersion()); addAttribute(root, TARGET_ARTIFACT_VERSION, genericArtifact.getTargetArtifactVersion()); addAttribute(root, CONFLICT_RESOLUTION_PRIORITY, genericArtifact.getConflictResolutionPriority()); // only create optional attributes if necessary if (artifactType == ArtifactTypeValue.DEPENDENCY || artifactType == ArtifactTypeValue.ATTACHMENT) { addAttribute(root, DEP_PARENT_SOURCE_ARTIFACT_ID, genericArtifact.getDepParentSourceArtifactId()); addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_ID, genericArtifact.getDepParentSourceRepositoryId()); addAttribute(root, DEP_PARENT_SOURCE_REPOSITORY_KIND, genericArtifact.getDepParentSourceRepositoryKind()); addAttribute(root, DEP_PARENT_TARGET_ARTIFACT_ID, genericArtifact.getDepParentTargetArtifactId()); addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_ID, genericArtifact.getDepParentTargetRepositoryId()); addAttribute(root, DEP_PARENT_TARGET_REPOSITORY_KIND, genericArtifact.getDepParentTargetRepositoryKind()); } // dependencies have even more optional attributes if (artifactType == ArtifactTypeValue.DEPENDENCY) { addAttribute(root, DEP_CHILD_SOURCE_ARTIFACT_ID, genericArtifact.getDepChildSourceArtifactId()); addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_ID, genericArtifact.getDepChildSourceRepositoryId()); addAttribute(root, DEP_CHILD_SOURCE_REPOSITORY_KIND, genericArtifact.getDepChildSourceRepositoryKind()); addAttribute(root, DEP_CHILD_TARGET_ARTIFACT_ID, genericArtifact.getDepChildTargetArtifactId()); addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_ID, genericArtifact.getDepChildTargetRepositoryId()); addAttribute(root, DEP_CHILD_TARGET_REPOSITORY_KIND, genericArtifact.getDepChildTargetRepositoryKind()); } addAttribute(root, SOURCE_ARTIFACT_ID, genericArtifact.getSourceArtifactId()); addAttribute(root, SOURCE_REPOSITORY_ID, genericArtifact.getSourceRepositoryId()); addAttribute(root, SOURCE_REPOSITORY_KIND, genericArtifact.getSourceRepositoryKind()); addAttribute(root, SOURCE_SYSTEM_ID, genericArtifact.getSourceSystemId()); addAttribute(root, SOURCE_SYSTEM_KIND, genericArtifact.getSourceSystemKind()); addAttribute(root, TARGET_ARTIFACT_ID, genericArtifact.getTargetArtifactId()); addAttribute(root, TARGET_REPOSITORY_ID, genericArtifact.getTargetRepositoryId()); addAttribute(root, TARGET_REPOSITORY_KIND, genericArtifact.getTargetRepositoryKind()); addAttribute(root, TARGET_SYSTEM_ID, genericArtifact.getTargetSystemId()); addAttribute(root, TARGET_SYSTEM_KIND, genericArtifact.getTargetSystemKind()); addAttribute(root, SOURCE_SYSTEM_TIMEZONE, genericArtifact.getSourceSystemTimezone()); addAttribute(root, TARGET_SYSTEM_TIMEZONE, genericArtifact.getTargetSystemTimezone()); // addAttribute(root, SOURCE_SYSTEM_ENCODING, genericArtifact // .getSourceSystemEncoding()); // addAttribute(root, TARGET_SYSTEM_ENCODING, genericArtifact // .getTargetSystemEncoding()); addAttribute(root, TRANSACTION_ID, genericArtifact.getTransactionId()); if (genericArtifact.getAllGenericArtifactFields() != null) { // now add fields for (GenericArtifactField genericArtifactField : genericArtifact.getAllGenericArtifactFields()) { Element field = addElement(root, ARTIFACT_FIELD_ELEMENT_NAME, CCF_ARTIFACT_NAMESPACE); switch (genericArtifactField.getFieldAction()) { case APPEND: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_APPEND); break; } case DELETE: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_DELETE); break; } case REPLACE: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_REPLACE); break; } case UNKNOWN: { addAttribute(field, FIELD_ACTION, FIELD_ACTION_UNKNOWN); break; } default: { throw new GenericArtifactParsingException( "Non valid value for field-attribute " + FIELD_ACTION + " specified."); } } addAttribute(field, FIELD_NAME, genericArtifactField.getFieldName()); addAttribute(field, FIELD_TYPE, genericArtifactField.getFieldType()); if (genericArtifactField.getFieldValueHasChanged()) { addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_TRUE); } else { addAttribute(field, FIELD_VALUE_HAS_CHANGED, FIELD_VALUE_HAS_CHANGED_FALSE); } if (genericArtifact.getIncludesFieldMetaData() .equals(GenericArtifact.IncludesFieldMetaDataValue.TRUE)) { addAttribute(field, MIN_OCCURS, genericArtifactField.getMinOccursValue()); addAttribute(field, MAX_OCCURS, genericArtifactField.getMaxOccursValue()); addAttribute(field, NULL_VALUE_SUPPORTED, genericArtifactField.getNullValueSupported()); addAttribute(field, ALTERNATIVE_FIELD_NAME, genericArtifactField.getAlternativeFieldName()); } setFieldValue(field, genericArtifactField.getFieldValue(), genericArtifactField.getFieldValueType()); } } root.addAttribute( new QName(SCHEMA_LOCATION_ATTRIBUTE, new Namespace(SCHEMA_NAMESPACE_PREFIX, SCHEMA_NAMESPACE)), CCF_SCHEMA_LOCATION); return document; }
From source file:com.doculibre.constellio.opensearch.OpenSearchSolrServer.java
License:Open Source License
@SuppressWarnings("unchecked") private static SolrDocumentList parse(Element rootElement) throws IOException { SolrDocumentList solrDocumentList = new SolrDocumentList(); Element channelElement = rootElement.element("channel"); String totalResultsStr = channelElement.elementText(new QName("totalResults", NS_OPENSEARCH)); String startIndexStr = channelElement.elementText(new QName("startIndex", NS_OPENSEARCH)); long numFound = Long.parseLong(totalResultsStr); long start = Long.parseLong(startIndexStr); solrDocumentList.setNumFound(numFound); solrDocumentList.setStart(start);/* w w w . ja v a2 s.c om*/ for (Iterator<Element> it = channelElement.elementIterator("item"); it.hasNext();) { Element itemElement = it.next(); String title = itemElement.elementText("title"); String description = itemElement.elementText("description"); String link = itemElement.elementText("link"); title = CharSetUtils.convert(title, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1); description = CharSetUtils.convert(description, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1); link = CharSetUtils.convert(link, CharSetUtils.UTF_8, CharSetUtils.ISO_8859_1); SolrDocument solrDocument = new SolrDocument(); solrDocument.addField("title", title); solrDocument.addField("description", description); solrDocument.addField("link", link); solrDocumentList.add(solrDocument); } return solrDocumentList; }
From source file:com.haulmont.cuba.core.sys.persistence.MappingFileCreator.java
License:Apache License
private Document createDocument(Map<Class<?>, List<Attr>> mappings) { Document doc = DocumentHelper.createDocument(); Element rootEl = doc.addElement("entity-mappings", XMLNS); Namespace xsi = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); rootEl.add(xsi);/*ww w. ja v a 2 s . c o m*/ rootEl.addAttribute(new QName("schemaLocation", xsi), SCHEMA_LOCATION); rootEl.addAttribute("version", PERSISTENCE_VER); for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) { if (entry.getKey().getAnnotation(MappedSuperclass.class) != null) { Element entityEl = rootEl.addElement("mapped-superclass", XMLNS); entityEl.addAttribute("class", entry.getKey().getName()); createAttributes(entry, entityEl); } } for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) { if (entry.getKey().getAnnotation(Entity.class) != null) { Element entityEl = rootEl.addElement("entity", XMLNS); entityEl.addAttribute("class", entry.getKey().getName()); entityEl.addAttribute("name", entry.getKey().getAnnotation(Entity.class).name()); createAttributes(entry, entityEl); } } for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) { if (entry.getKey().getAnnotation(Embeddable.class) != null) { Element entityEl = rootEl.addElement("embeddable", XMLNS); entityEl.addAttribute("class", entry.getKey().getName()); createAttributes(entry, entityEl); } } return doc; }
From source file:com.haulmont.cuba.gui.xml.XmlInheritanceProcessor.java
License:Apache License
private void process(Element resultElem, Element extElem) { // set text// w w w .j a v a2 s. c om if (!StringUtils.isBlank(extElem.getText())) resultElem.setText(extElem.getText()); // add all attributes from extension for (Attribute attribute : Dom4j.attributes(extElem)) { if (resultElem == document.getRootElement() && attribute.getName().equals("extends")) { // ignore "extends" in root element continue; } resultElem.addAttribute(attribute.getName(), attribute.getValue()); } String idx = extElem.attributeValue(new QName("index", extNs)); if (resultElem != document.getRootElement() && StringUtils.isNotBlank(idx)) { int index = Integer.parseInt(idx); Element parent = resultElem.getParent(); if (index < 0 || index > parent.elements().size()) { String message = String.format( "Incorrect extension XML for screen. Could not move existing element %s to position %s", resultElem.getName(), index); throw new DevelopmentException(message, ParamsMap.of("element", resultElem.getName(), "index", index)); } parent.remove(resultElem); //noinspection unchecked parent.elements().add(index, resultElem); } // add and process elements Set<Element> justAdded = new HashSet<>(); for (Element element : Dom4j.elements(extElem)) { // look for suitable locator ElementTargetLocator locator = null; for (ElementTargetLocator l : targetLocators) { if (l.suitableFor(element)) { locator = l; break; } } if (locator != null) { Element target = locator.locate(resultElem, element); // process target or a new element if target not found if (target != null) { process(target, element); } else { addNewElement(resultElem, element, justAdded); } } else { // if no suitable locator found, look for a single element with the same name List<Element> list = Dom4j.elements(resultElem, element.getName()); if (list.size() == 1 && !justAdded.contains(list.get(0))) { process(list.get(0), element); } else { addNewElement(resultElem, element, justAdded); } } } }
From source file:com.haulmont.cuba.gui.xml.XmlInheritanceProcessor.java
License:Apache License
private void addNewElement(Element resultElem, Element element, Set<Element> justAdded) { String idx = element.attributeValue(new QName("index", extNs)); Element newElement;/*from w w w . j av a 2s . c o m*/ if (StringUtils.isBlank(idx)) { newElement = resultElem.addElement(element.getName()); } else { newElement = DocumentHelper.createElement(element.getName()); @SuppressWarnings("unchecked") List<Element> elements = resultElem.elements(); int index = Integer.parseInt(idx); if (index < 0 || index > elements.size()) { String message = String.format( "Incorrect extension XML for screen. Could not paste new element %s to position %s", newElement.getName(), index); throw new DevelopmentException(message, ParamsMap.of("element", newElement.getName(), "index", index)); } elements.add(index, newElement); } justAdded.add(newElement); process(newElement, element); }
From source file:com.ostrichemulators.semtool.poi.main.LowMemXlsReader.java
/** * Gets sheet name-to-id mapping// w ww. ja va 2 s .com * * @param r * @return */ private LinkedHashMap<String, String> readSheetInfo(XSSFReader r) { LinkedHashMap<String, String> map = new LinkedHashMap<>(); try (InputStream is = r.getWorkbookData()) { SAXReader sax = new SAXReader(); Document doc = sax.read(is); Namespace ns = new Namespace("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); Element sheets = doc.getRootElement().element("sheets"); for (Object sheet : sheets.elements("sheet")) { Element e = Element.class.cast(sheet); String name = e.attributeValue("name"); String id = e.attributeValue(new QName("id", ns)); map.put(name, id); } } catch (Exception e) { log.error(e, e); } return map; }
From source file:com.rayo.core.xml.providers.ColibriProvider.java
License:Apache License
private void createColibriOfferEvent(ColibriOfferEvent event, Document document) { Element root = document.addElement(new QName("offer", NAMESPACE)); root.addAttribute("muc", event.getMuc().toString()); root.addAttribute("videobridge", event.getMuc().getNode()); root.addAttribute("nickname", event.getNickname()); root.addAttribute("participant", event.getParticipant().toString()); root.add(event.getConference().createCopy()); }