List of usage examples for javax.xml.stream XMLStreamReader next
public int next() throws XMLStreamException;
From source file:org.apache.solr.handler.XMLLoader.java
/** * @since solr 1.3// w w w. j ava2 s. c om */ void processDelete(UpdateRequestProcessor processor, XMLStreamReader parser) throws XMLStreamException, IOException { // Parse the command DeleteUpdateCommand deleteCmd = new DeleteUpdateCommand(); deleteCmd.fromPending = true; deleteCmd.fromCommitted = true; for (int i = 0; i < parser.getAttributeCount(); i++) { String attrName = parser.getAttributeLocalName(i); String attrVal = parser.getAttributeValue(i); if ("fromPending".equals(attrName)) { deleteCmd.fromPending = StrUtils.parseBoolean(attrVal); } else if ("fromCommitted".equals(attrName)) { deleteCmd.fromCommitted = StrUtils.parseBoolean(attrVal); } else { XmlUpdateRequestHandler.log.warn("unexpected attribute delete/@" + attrName); } } StringBuilder text = new StringBuilder(); while (true) { int event = parser.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: String mode = parser.getLocalName(); if (!("id".equals(mode) || "query".equals(mode))) { XmlUpdateRequestHandler.log.warn("unexpected XML tag /delete/" + mode); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag /delete/" + mode); } text.setLength(0); break; case XMLStreamConstants.END_ELEMENT: String currTag = parser.getLocalName(); if ("id".equals(currTag)) { deleteCmd.id = text.toString(); } else if ("query".equals(currTag)) { deleteCmd.query = text.toString(); } else if ("delete".equals(currTag)) { return; } else { XmlUpdateRequestHandler.log.warn("unexpected XML tag /delete/" + currTag); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag /delete/" + currTag); } processor.processDelete(deleteCmd); deleteCmd.id = null; deleteCmd.query = null; break; // Add everything to the text case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: text.append(parser.getText()); break; } } }
From source file:org.apache.solr.handler.XMLLoader.java
/** * Given the input stream, read a document * * @since solr 1.3//from w w w . j a v a 2 s .c o m */ SolrInputDocument readDoc(XMLStreamReader parser) throws XMLStreamException { SolrInputDocument doc = new SolrInputDocument(); String attrName = ""; for (int i = 0; i < parser.getAttributeCount(); i++) { attrName = parser.getAttributeLocalName(i); if ("boost".equals(attrName)) { doc.setDocumentBoost(Float.parseFloat(parser.getAttributeValue(i))); } else { XmlUpdateRequestHandler.log.warn("Unknown attribute doc/@" + attrName); } } StringBuilder text = new StringBuilder(); String name = null; float boost = 1.0f; boolean isNull = false; while (true) { int event = parser.next(); switch (event) { // Add everything to the text case XMLStreamConstants.SPACE: case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: text.append(parser.getText()); break; case XMLStreamConstants.END_ELEMENT: if ("doc".equals(parser.getLocalName())) { return doc; } else if ("field".equals(parser.getLocalName())) { if (!isNull) { doc.addField(name, text.toString(), boost); boost = 1.0f; } } break; case XMLStreamConstants.START_ELEMENT: text.setLength(0); String localName = parser.getLocalName(); if (!"field".equals(localName)) { XmlUpdateRequestHandler.log.warn("unexpected XML tag doc/" + localName); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unexpected XML tag doc/" + localName); } boost = 1.0f; String attrVal = ""; for (int i = 0; i < parser.getAttributeCount(); i++) { attrName = parser.getAttributeLocalName(i); attrVal = parser.getAttributeValue(i); if ("name".equals(attrName)) { name = attrVal; } else if ("boost".equals(attrName)) { boost = Float.parseFloat(attrVal); } else if ("null".equals(attrName)) { isNull = StrUtils.parseBoolean(attrVal); } else { XmlUpdateRequestHandler.log.warn("Unknown attribute doc/field/@" + attrName); } } break; } } }
From source file:org.apache.solr.handler.XmlUpdateRequestHandlerTest.java
@Test public void testReadDoc() throws Exception { String xml = "<doc boost=\"5.5\">" + " <field name=\"id\" boost=\"2.2\">12345</field>" + " <field name=\"name\">kitten</field>" + " <field name=\"cat\" boost=\"3\">aaa</field>" + " <field name=\"cat\" boost=\"4\">bbb</field>" + " <field name=\"cat\" boost=\"5\">bbb</field>" + " <field name=\"ab\">a&b</field>" + "</doc>"; XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(xml)); parser.next(); // read the START document... //null for the processor is all right here XMLLoader loader = new XMLLoader(); SolrInputDocument doc = loader.readDoc(parser); // Read boosts assertEquals(5.5f, doc.getDocumentBoost(), 0.1); assertEquals(1.0f, doc.getField("name").getBoost(), 0.1); assertEquals(2.2f, doc.getField("id").getBoost(), 0.1); // Boost is the product of each value assertEquals((3 * 4 * 5.0f), doc.getField("cat").getBoost(), 0.1); // Read values assertEquals("12345", doc.getField("id").getValue()); assertEquals("kitten", doc.getField("name").getValue()); assertEquals("a&b", doc.getField("ab").getValue()); // read something with escaped characters Collection<Object> out = doc.getField("cat").getValues(); assertEquals(3, out.size());// www. j a v a2s. c om assertEquals("[aaa, bbb, bbb]", out.toString()); }
From source file:org.apache.solr.update.AddBlockUpdateTest.java
@Test public void testXML() throws IOException, XMLStreamException { UpdateRequest req = new UpdateRequest(); List<SolrInputDocument> docs = new ArrayList<>(); String xml_doc1 = "<doc >" + " <field name=\"id\">1</field>" + " <field name=\"parent_s\">X</field>" + "<doc> " + " <field name=\"id\" >2</field>" + " <field name=\"child_s\">y</field>" + "</doc>" + "<doc> " + " <field name=\"id\" >3</field>" + " <field name=\"child_s\">z</field>" + "</doc>" + "</doc>"; String xml_doc2 = "<doc >" + " <field name=\"id\">4</field>" + " <field name=\"parent_s\">A</field>" + "<doc> " + " <field name=\"id\" >5</field>" + " <field name=\"child_s\">b</field>" + "</doc>" + "<doc> " + " <field name=\"id\" >6</field>" + " <field name=\"child_s\">c</field>" + "</doc>" + "</doc>"; XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(xml_doc1)); parser.next(); // read the START document... //null for the processor is all right here XMLLoader loader = new XMLLoader(); SolrInputDocument document1 = loader.readDoc(parser); XMLStreamReader parser2 = inputFactory.createXMLStreamReader(new StringReader(xml_doc2)); parser2.next(); // read the START document... //null for the processor is all right here //XMLLoader loader = new XMLLoader(); SolrInputDocument document2 = loader.readDoc(parser2); docs.add(document1);//from w ww.j a v a 2s . co m docs.add(document2); Collections.shuffle(docs, random()); req.add(docs); RequestWriter requestWriter = new RequestWriter(); OutputStream os = new ByteArrayOutputStream(); requestWriter.write(req, os); assertBlockU(os.toString()); assertU(commit()); final SolrIndexSearcher searcher = getSearcher(); assertSingleParentOf(searcher, one("yz"), "X"); assertSingleParentOf(searcher, one("bc"), "A"); }
From source file:org.apache.synapse.commons.json.JsonDataSource.java
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException { XMLStreamReader reader = getReader(); xmlWriter.writeStartDocument();/* w w w.j a v a2 s . c om*/ while (reader.hasNext()) { int x = reader.next(); switch (x) { case XMLStreamConstants.START_ELEMENT: xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI()); int namespaceCount = reader.getNamespaceCount(); for (int i = namespaceCount - 1; i >= 0; i--) { xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i)); } int attributeCount = reader.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } break; case XMLStreamConstants.START_DOCUMENT: break; case XMLStreamConstants.CHARACTERS: xmlWriter.writeCharacters(reader.getText()); break; case XMLStreamConstants.CDATA: xmlWriter.writeCData(reader.getText()); break; case XMLStreamConstants.END_ELEMENT: xmlWriter.writeEndElement(); break; case XMLStreamConstants.END_DOCUMENT: xmlWriter.writeEndDocument(); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: xmlWriter.writeComment(reader.getText()); break; case XMLStreamConstants.DTD: xmlWriter.writeDTD(reader.getText()); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); break; case XMLStreamConstants.ENTITY_REFERENCE: xmlWriter.writeEntityRef(reader.getLocalName()); break; default: throw new OMException(); } } xmlWriter.writeEndDocument(); xmlWriter.flush(); xmlWriter.close(); }
From source file:org.apache.syncope.client.cli.commands.migrate.MigrateConf.java
private static void exec(final String src, final String dst) throws XMLStreamException, IOException { XMLStreamWriter writer = new PrettyPrintXMLStreamWriter( OUTPUT_FACTORY.createXMLStreamWriter(new FileWriter(dst)), 2); writer.writeStartDocument("UTF-8", "1.0"); writer.writeStartElement("dataset"); StringWriter reporterSW = new StringWriter(); XMLStreamWriter reporter = new PrettyPrintXMLStreamWriter(OUTPUT_FACTORY.createXMLStreamWriter(reporterSW), 2);//from w ww. j a v a 2s. co m reporter.writeStartDocument("UTF-8", "1.0"); reporter.writeStartElement("dataset"); InputStream inputStream = Files.newInputStream(Paths.get(src)); XMLStreamReader reader = INPUT_FACTORY.createXMLStreamReader(inputStream); reader.nextTag(); // root reader.nextTag(); // dataset writer.writeStartElement("AnyType"); writer.writeAttribute("id", "USER"); writer.writeAttribute("kind", "USER"); writer.writeEndElement(); writer.writeStartElement("AnyTypeClass"); writer.writeAttribute("id", "BaseUser"); writer.writeEndElement(); writer.writeStartElement("AnyType_AnyTypeClass"); writer.writeAttribute("anyType_id", "USER"); writer.writeAttribute("anyTypeClass_id", "BaseUser"); writer.writeEndElement(); writer.writeStartElement("AnyType"); writer.writeAttribute("id", "GROUP"); writer.writeAttribute("kind", "GROUP"); writer.writeEndElement(); writer.writeStartElement("AnyTypeClass"); writer.writeAttribute("id", "BaseGroup"); writer.writeEndElement(); writer.writeStartElement("AnyType_AnyTypeClass"); writer.writeAttribute("anyType_id", "GROUP"); writer.writeAttribute("anyTypeClass_id", "BaseGroup"); writer.writeEndElement(); writer.writeStartElement("AnyTypeClass"); writer.writeAttribute("id", "BaseUMembership"); writer.writeEndElement(); Set<String> connInstanceCapabilities = new HashSet<>(); String lastUUID; String syncopeConf = UUID.randomUUID().toString(); Map<String, String> cPlainAttrs = new HashMap<>(); Map<String, String> policies = new HashMap<>(); Map<String, String> connInstances = new HashMap<>(); Map<String, String> provisions = new HashMap<>(); Map<String, String> mappings = new HashMap<>(); Map<String, String> tasks = new HashMap<>(); Map<String, String> notifications = new HashMap<>(); Map<String, String> reports = new HashMap<>(); String globalAccountPolicy = null; String globalPasswordPolicy = null; while (reader.hasNext()) { if (reader.isStartElement()) { switch (reader.getLocalName().toLowerCase()) { case "syncopeconf": writer.writeStartElement("SyncopeConf"); writer.writeAttribute("id", syncopeConf); writer.writeEndElement(); break; case "cschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("PlainSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeEndElement(); break; case "cattr": writer.writeStartElement("CPlainAttr"); copyAttrs(reader, writer, "owner_id", "schema_name"); lastUUID = UUID.randomUUID().toString(); writer.writeAttribute("id", lastUUID); writer.writeAttribute("owner_id", syncopeConf); writer.writeAttribute("schema_id", getAttributeValue(reader, "schema_name")); writer.writeEndElement(); cPlainAttrs.put(getAttributeValue(reader, "id"), lastUUID); break; case "cattrvalue": writer.writeStartElement("CPlainAttrValue"); copyAttrs(reader, writer, "attribute_id"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("attribute_id", cPlainAttrs.get(getAttributeValue(reader, "attribute_id"))); writer.writeEndElement(); break; case "uschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("PlainSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseUser"); writer.writeEndElement(); break; case "uderschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("DerSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseUser"); writer.writeEndElement(); break; case "uvirschema": reporter.writeStartElement("VirSchema"); copyAttrs(reader, reporter); reporter.writeAttribute("key", getAttributeValue(reader, "name")); reporter.writeEndElement(); break; case "rschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("PlainSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseGroup"); writer.writeEndElement(); break; case "rderschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("DerSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseGroup"); writer.writeEndElement(); break; case "rvirschema": reporter.writeStartElement("VirSchema"); reporter.writeAttribute("key", getAttributeValue(reader, "name")); copyAttrs(reader, reporter); reporter.writeEndElement(); break; case "mschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("PlainSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseUMembership"); writer.writeEndElement(); break; case "mderschema": writer.writeStartElement("SyncopeSchema"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeStartElement("DerSchema"); copyAttrs(reader, writer); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("anyTypeClass_id", "BaseUMembership"); writer.writeEndElement(); break; case "mvirschema": reporter.writeStartElement("VirSchema"); copyAttrs(reader, reporter); reporter.writeAttribute("key", getAttributeValue(reader, "name")); reporter.writeEndElement(); break; case "policy": String policyId = getAttributeValue(reader, "id"); lastUUID = UUID.randomUUID().toString(); policies.put(policyId, lastUUID); ObjectNode specification = (ObjectNode) OBJECT_MAPPER .readTree(getAttributeValue(reader, "specification")); switch (getAttributeValue(reader, "DTYPE")) { case "SyncPolicy": writer.writeStartElement("PullPolicy"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("description", getAttributeValue(reader, "description")); writer.writeEndElement(); break; case "PasswordPolicy": writer.writeStartElement("PasswordPolicy"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("description", getAttributeValue(reader, "description")); if ("GLOBAL_PASSWORD".equalsIgnoreCase(getAttributeValue(reader, "type"))) { globalPasswordPolicy = lastUUID; } JsonNode allowNullPassword = specification.get("allowNullPassword"); if (allowNullPassword != null) { writer.writeAttribute("allowNullPassword", allowNullPassword.asBoolean() ? "1" : "0"); specification.remove("allowNullPassword"); } JsonNode historyLength = specification.get("historyLength"); if (historyLength != null) { writer.writeAttribute("historyLength", historyLength.asText()); specification.remove("historyLength"); } specification.put("@class", "org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf"); writer.writeEndElement(); writer.writeStartElement("PasswordRuleConfInstance"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("passwordPolicy_id", lastUUID); writer.writeAttribute("serializedInstance", specification.toString()); writer.writeEndElement(); break; case "AccountPolicy": writer.writeStartElement("AccountPolicy"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("description", getAttributeValue(reader, "description")); if ("GLOBAL_ACCOUNT".equalsIgnoreCase(getAttributeValue(reader, "type"))) { globalAccountPolicy = lastUUID; } JsonNode propagateSuspension = specification.get("propagateSuspension"); if (propagateSuspension != null) { writer.writeAttribute("propagateSuspension", propagateSuspension.asBoolean() ? "1" : "0"); specification.remove("propagateSuspension"); } JsonNode permittedLoginRetries = specification.get("permittedLoginRetries"); if (permittedLoginRetries != null) { writer.writeAttribute("maxAuthenticationAttempts", permittedLoginRetries.asText()); specification.remove("permittedLoginRetries"); } specification.put("@class", "org.apache.syncope.common.lib.policy.DefaultAccountRuleConf"); writer.writeEndElement(); writer.writeStartElement("AccountRuleConfInstance"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("accountPolicy_id", lastUUID); writer.writeAttribute("serializedInstance", specification.toString()); writer.writeEndElement(); break; default: } break; case "conninstance": lastUUID = UUID.randomUUID().toString(); connInstances.put(getAttributeValue(reader, "id"), lastUUID); writer.writeStartElement("ConnInstance"); copyAttrs(reader, writer); writer.writeAttribute("id", lastUUID); writer.writeEndElement(); break; case "conninstance_capabilities": String connInstanceId = getAttributeValue(reader, "connInstance_id"); String connInstanceKey = connInstances.get(connInstanceId); String capabilities = getAttributeValue(reader, "capabilities"); if (capabilities.startsWith("ONE_PHASE_")) { capabilities = capabilities.substring(10); } else if (capabilities.startsWith("TWO_PHASES_")) { capabilities = capabilities.substring(11); } if (!connInstanceCapabilities.contains(connInstanceId + capabilities)) { writer.writeStartElement("ConnInstance_capabilities"); writer.writeAttribute("connInstance_id", connInstanceKey); writer.writeAttribute("capability", capabilities); writer.writeEndElement(); connInstanceCapabilities.add(connInstanceId + capabilities); } break; case "externalresource": writer.writeStartElement("ExternalResource"); copyAttrs(reader, writer, "syncTraceLevel", "userializedSyncToken", "rserializedSyncToken", "propagationMode", "propagationPrimary", "connector_id", "syncPolicy_id", "passwordPolicy_id", "creator", "lastModifier", "creationDate", "lastChangeDate"); writer.writeAttribute("id", getAttributeValue(reader, "name")); writer.writeAttribute("connector_id", connInstances.get(getAttributeValue(reader, "connector_id"))); writer.writeAttribute("provisioningTraceLevel", getAttributeValue(reader, "syncTraceLevel")); String syncPolicyKey = policies.get(getAttributeValue(reader, "syncPolicy_id")); if (StringUtils.isNotBlank(syncPolicyKey)) { writer.writeAttribute("pullPolicy_id", syncPolicyKey); } String passwordPolicyKey = policies.get(getAttributeValue(reader, "passwordPolicy_id")); if (StringUtils.isNotBlank(passwordPolicyKey)) { writer.writeAttribute("passwordPolicy_id", passwordPolicyKey); } writer.writeEndElement(); break; case "externalresource_propactions": writer.writeStartElement("ExternalResource_PropActions"); writer.writeAttribute("resource_id", getAttributeValue(reader, "externalResource_name")); String propActionClassName = getAttributeValue(reader, "element"); switch (propActionClassName) { case "org.apache.syncope.core.propagation.impl.LDAPMembershipPropagationActions": propActionClassName = "org.apache.syncope.core.provisioning.java.propagation." + "LDAPMembershipPropagationActions"; break; case "org.apache.syncope.core.propagation.impl.LDAPPasswordPropagationActions": propActionClassName = "org.apache.syncope.core.provisioning.java.propagation." + "LDAPPasswordPropagationActions"; break; case "org.apache.syncope.core.propagation.impl.DBPasswordPropagationActions": propActionClassName = "org.apache.syncope.core.provisioning.java.propagation." + "DBPasswordPropagationActions"; break; default: } writer.writeAttribute("actionClassName", propActionClassName); writer.writeEndElement(); break; case "policy_externalresource": writer.writeStartElement("AccountPolicy_ExternalResource"); writer.writeAttribute("accountPolicy_id", policies.get(getAttributeValue(reader, "account_policy_id"))); writer.writeAttribute("resource_id", getAttributeValue(reader, "resource_name")); writer.writeEndElement(); break; case "umapping": String umappingId = getAttributeValue(reader, "id"); lastUUID = UUID.randomUUID().toString(); provisions.put(umappingId, lastUUID); writer.writeStartElement("Provision"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("resource_id", getAttributeValue(reader, "resource_name")); writer.writeAttribute("anyType_id", "USER"); writer.writeAttribute("objectClass", "__ACCOUNT__"); writer.writeEndElement(); lastUUID = UUID.randomUUID().toString(); mappings.put(umappingId, lastUUID); writer.writeStartElement("Mapping"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("provision_id", provisions.get(umappingId)); String uaccountLink = getAttributeValue(reader, "accountlink"); if (StringUtils.isNotBlank(uaccountLink)) { writer.writeAttribute("connObjectLink", uaccountLink); } writer.writeEndElement(); break; case "umappingitem": String uIntMappingType = getAttributeValue(reader, "intMappingType"); if (uIntMappingType.endsWith("VirtualSchema")) { reporter.writeStartElement("MappingItem"); copyAttrs(reader, reporter, "accountid", "intMappingType"); reporter.writeEndElement(); } else { writer.writeStartElement("MappingItem"); copyAttrs(reader, writer, "accountid", "intMappingType", "mapping_id", "intMappingType", "intAttrName"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("mapping_id", mappings.get(getAttributeValue(reader, "mapping_id"))); writer.writeAttribute("connObjectKey", getAttributeValue(reader, "accountid")); writeIntAttrName(uIntMappingType, "intAttrName", mappings.get(getAttributeValue(reader, "intAttrName")), writer); writer.writeEndElement(); } break; case "rmapping": String rmappingId = "10" + getAttributeValue(reader, "id"); lastUUID = UUID.randomUUID().toString(); provisions.put(rmappingId, lastUUID); writer.writeStartElement("Provision"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("resource_id", getAttributeValue(reader, "resource_name")); writer.writeAttribute("anyType_id", "GROUP"); writer.writeAttribute("objectClass", "__GROUP__"); writer.writeEndElement(); lastUUID = UUID.randomUUID().toString(); mappings.put(rmappingId, lastUUID); writer.writeStartElement("Mapping"); writer.writeAttribute("id", lastUUID); writer.writeAttribute("provision_id", provisions.get(rmappingId)); String raccountLink = getAttributeValue(reader, "accountlink"); if (StringUtils.isNotBlank(raccountLink)) { writer.writeAttribute("connObjectLink", raccountLink); } writer.writeEndElement(); break; case "rmappingitem": String rIntMappingType = getAttributeValue(reader, "intMappingType"); if (rIntMappingType.endsWith("VirtualSchema")) { reporter.writeStartElement("MappingItem"); copyAttrs(reader, reporter, "accountid", "intMappingType"); reporter.writeEndElement(); } else { writer.writeStartElement("MappingItem"); copyAttrs(reader, writer, "accountid", "intMappingType", "mapping_id", "intAttrName"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("mapping_id", mappings.get("10" + getAttributeValue(reader, "mapping_id"))); writer.writeAttribute("connObjectKey", getAttributeValue(reader, "accountid")); writeIntAttrName(rIntMappingType, "intAttrName", mappings.get(getAttributeValue(reader, "intAttrName")), writer); writer.writeEndElement(); } break; case "task": writer.writeStartElement("Task"); copyAttrs(reader, writer, "DTYPE", "propagationMode", "subjectType", "subjectId", "xmlAttributes", "jobClassName", "userTemplate", "roleTemplate", "userFilter", "roleFilter", "propagationOperation", "syncStatus", "fullReconciliation", "resource_name"); lastUUID = UUID.randomUUID().toString(); tasks.put(getAttributeValue(reader, "id"), lastUUID); writer.writeAttribute("id", lastUUID); String resourceName = getAttributeValue(reader, "resource_name"); if (StringUtils.isNotBlank(resourceName)) { writer.writeAttribute("resource_id", resourceName); } String name = getAttributeValue(reader, "name"); if (StringUtils.isNotBlank(name)) { writer.writeAttribute("name", name); } switch (getAttributeValue(reader, "DTYPE")) { case "PropagationTask": writer.writeAttribute("DTYPE", "PropagationTask"); writer.writeAttribute("anyTypeKind", getAttributeValue(reader, "subjectType")); writer.writeAttribute("anyKey", getAttributeValue(reader, "subjectId")); writer.writeAttribute("attributes", getAttributeValue(reader, "xmlAttributes")); writer.writeAttribute("operation", getAttributeValue(reader, "propagationOperation")); writer.writeEndElement(); break; case "SyncTask": writer.writeAttribute("DTYPE", "PullTask"); writer.writeAttribute("syncStatus", getAttributeValue(reader, "syncStatus")); String fullReconciliation = getAttributeValue(reader, "fullReconciliation"); if ("1".equals(fullReconciliation)) { writer.writeAttribute("pullMode", "FULL_RECONCILIATION"); } else if ("0".equals(fullReconciliation)) { writer.writeAttribute("pullMode", "INCREMENTAL"); } writer.writeEndElement(); String userTemplate = getAttributeValue(reader, "userTemplate"); if (StringUtils.isNotBlank(userTemplate)) { ObjectNode template = (ObjectNode) OBJECT_MAPPER.readTree(userTemplate); JsonNode plainAttrs = template.remove("attrs"); template.set("plainAttrs", plainAttrs); writer.writeStartElement("AnyTemplatePullTask"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("pullTask_id", lastUUID); writer.writeAttribute("anyType_id", "USER"); writer.writeAttribute("template", template.toString()); writer.writeEndElement(); } String roleTemplate = getAttributeValue(reader, "roleTemplate"); if (StringUtils.isNotBlank(roleTemplate)) { ObjectNode template = (ObjectNode) OBJECT_MAPPER.readTree(roleTemplate); JsonNode plainAttrs = template.remove("attrs"); template.set("plainAttrs", plainAttrs); writer.writeStartElement("AnyTemplatePullTask"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("pullTask_id", lastUUID); writer.writeAttribute("anyType_id", "GROUP"); writer.writeAttribute("template", template.toString()); writer.writeEndElement(); } break; case "SchedTask": writer.writeAttribute("DTYPE", "SchedTask"); writer.writeAttribute("jobDelegateClassName", getAttributeValue(reader, "jobClassName")); writer.writeEndElement(); break; case "NotificationTask": writer.writeAttribute("DTYPE", "NotificationTask"); writer.writeEndElement(); break; case "PushTask": writer.writeAttribute("DTYPE", "PushTask"); writer.writeEndElement(); String userFilter = getAttributeValue(reader, "userFilter"); if (StringUtils.isNotBlank(userFilter)) { writer.writeStartElement("PushTaskAnyFilter"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("pushTask_id", lastUUID); writer.writeAttribute("anyType_id", "USER"); writer.writeAttribute("fiql", userFilter); writer.writeEndElement(); } String roleFilter = getAttributeValue(reader, "roleFilter"); if (StringUtils.isNotBlank(roleFilter)) { writer.writeStartElement("PushTaskAnyFilter"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("pushTask_id", lastUUID); writer.writeAttribute("anyType_id", "GROUP"); writer.writeAttribute("fiql", roleFilter); writer.writeEndElement(); } break; default: } break; case "taskexec": writer.writeStartElement("TaskExec"); copyAttrs(reader, writer, "task_id"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("task_id", tasks.get(getAttributeValue(reader, "task_id"))); writer.writeEndElement(); break; case "synctask_actionsclassnames": writer.writeStartElement("PullTask_actionsClassNames"); writer.writeAttribute("pullTask_id", tasks.get(getAttributeValue(reader, "syncTask_id"))); String syncActionClassName = getAttributeValue(reader, "element"); switch (syncActionClassName) { case "org.apache.syncope.core.sync.impl.LDAPMembershipSyncActions": syncActionClassName = "org.apache.syncope.core.provisioning.java.pushpull.LDAPMembershipPullActions"; break; case "org.apache.syncope.core.sync.impl.LDAPPasswordSyncActions": syncActionClassName = "org.apache.syncope.core.provisioning.java.pushpull.LDAPPasswordPullActions"; break; case "org.apache.syncope.core.sync.impl.DBPasswordSyncActions": syncActionClassName = "org.apache.syncope.core.provisioning.java.pushpull.DBPasswordPullActions"; break; default: } writer.writeAttribute("actionClassName", syncActionClassName); writer.writeEndElement(); break; case "notification": writer.writeStartElement("Notification"); lastUUID = UUID.randomUUID().toString(); notifications.put(getAttributeValue(reader, "id"), lastUUID); writer.writeAttribute("id", lastUUID); copyAttrs(reader, writer, "recipientAttrType", "template", "userAbout", "roleAbout", "recipients", "recipientAttrName"); String recipientAttrType = getAttributeValue(reader, "recipientAttrType"); writeIntAttrName(recipientAttrType, "recipientAttrName", mappings.get(getAttributeValue(reader, "recipientAttrName")), writer); String recipients = getAttributeValue(reader, "recipients"); if (StringUtils.isNotBlank(recipients)) { writer.writeAttribute("recipientsFIQL", getAttributeValue(reader, "recipients")); } writer.writeAttribute("template_id", getAttributeValue(reader, "template")); writer.writeEndElement(); String userAbout = getAttributeValue(reader, "userAbout"); if (StringUtils.isNotBlank(userAbout)) { writer.writeStartElement("AnyAbout"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("notification_id", lastUUID); writer.writeAttribute("anyType_id", "USER"); writer.writeAttribute("filter", userAbout); writer.writeEndElement(); } String roleAbout = getAttributeValue(reader, "roleAbout"); if (StringUtils.isNotBlank(roleAbout)) { writer.writeStartElement("AnyAbout"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("notification_id", lastUUID); writer.writeAttribute("anyType_id", "GROUP"); writer.writeAttribute("filter", roleAbout); writer.writeEndElement(); } break; case "notification_events": writer.writeStartElement("Notification_events"); copyAttrs(reader, writer, "notification_id", "events"); writer.writeAttribute("notification_id", notifications.get(getAttributeValue(reader, "notification_id"))); writer.writeAttribute("event", getAttributeValue(reader, "events").replaceAll("Controller", "Logic")); writer.writeEndElement(); break; case "notificationtask_recipients": writer.writeStartElement("NotificationTask_recipients"); copyAttrs(reader, writer, "notificationTask_id"); writer.writeAttribute("notificationTask_id", tasks.get(getAttributeValue(reader, "notificationTask_id"))); writer.writeEndElement(); break; case "report": writer.writeStartElement("Report"); copyAttrs(reader, writer); lastUUID = UUID.randomUUID().toString(); reports.put(getAttributeValue(reader, "id"), lastUUID); writer.writeAttribute("id", lastUUID); writer.writeAttribute("name", getAttributeValue(reader, "name")); writer.writeEndElement(); break; case "reportletconfinstance": writer.writeStartElement("ReportletConfInstance"); copyAttrs(reader, writer, "report_id"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("report_id", reports.get(getAttributeValue(reader, "report_id"))); writer.writeEndElement(); break; case "reportexec": writer.writeStartElement("ReportExec"); copyAttrs(reader, writer, "report_id"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("report_id", reports.get(getAttributeValue(reader, "report_id"))); writer.writeEndElement(); break; case "securityquestion": writer.writeStartElement("SecurityQuestion"); copyAttrs(reader, writer); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeEndElement(); break; default: } } reader.next(); } writer.writeStartElement("Realm"); writer.writeAttribute("id", UUID.randomUUID().toString()); writer.writeAttribute("name", "/"); if (globalAccountPolicy != null) { writer.writeAttribute("accountPolicy_id", globalAccountPolicy); } if (globalPasswordPolicy != null) { writer.writeAttribute("passwordPolicy_id", globalPasswordPolicy); } writer.writeEndElement(); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); reporter.writeEndElement(); reporter.writeEndDocument(); reporter.close(); System.out.println("\nVirtual items, require manual intervention:\n" + reporterSW.toString()); }
From source file:org.apache.syncope.client.console.widgets.reconciliation.ReconciliationReportParser.java
public static ReconciliationReport parse(final Date run, final InputStream in) throws XMLStreamException { XMLStreamReader streamReader = INPUT_FACTORY.createXMLStreamReader(in); streamReader.nextTag(); // root streamReader.nextTag(); // report streamReader.nextTag(); // reportlet ReconciliationReport report = new ReconciliationReport(run); List<Missing> missing = new ArrayList<>(); List<Misaligned> misaligned = new ArrayList<>(); Set<String> onSyncope = null; Set<String> onResource = null; Any user = null;//ww w. j av a 2s .c om Any group = null; Any anyObject = null; String lastAnyType = null; while (streamReader.hasNext()) { if (streamReader.isStartElement()) { switch (streamReader.getLocalName()) { case "users": Anys users = new Anys(); users.setTotal(Integer.valueOf(streamReader.getAttributeValue("", "total"))); report.setUsers(users); break; case "user": user = new Any(); user.setType(AnyTypeKind.USER.name()); user.setKey(streamReader.getAttributeValue("", "key")); user.setName(streamReader.getAttributeValue("", "username")); report.getUsers().getAnys().add(user); break; case "groups": Anys groups = new Anys(); groups.setTotal(Integer.valueOf(streamReader.getAttributeValue("", "total"))); report.setGroups(groups); break; case "group": group = new Any(); group.setType(AnyTypeKind.GROUP.name()); group.setKey(streamReader.getAttributeValue("", "key")); group.setName(streamReader.getAttributeValue("", "groupName")); report.getGroups().getAnys().add(group); break; case "anyObjects": lastAnyType = streamReader.getAttributeValue("", "type"); Anys anyObjects = new Anys(); anyObjects.setAnyType(lastAnyType); anyObjects.setTotal(Integer.valueOf(streamReader.getAttributeValue("", "total"))); report.getAnyObjects().add(anyObjects); break; case "anyObject": anyObject = new Any(); anyObject.setType(lastAnyType); anyObject.setKey(streamReader.getAttributeValue("", "key")); final String anyType = lastAnyType; IterableUtils.find(report.getAnyObjects(), new Predicate<Anys>() { @Override public boolean evaluate(final Anys anys) { return anyType.equals(anys.getAnyType()); } }).getAnys().add(anyObject); break; case "missing": missing.add(new Missing(streamReader.getAttributeValue("", "resource"), streamReader.getAttributeValue("", "connObjectKeyValue"))); break; case "misaligned": misaligned.add(new Misaligned(streamReader.getAttributeValue("", "resource"), streamReader.getAttributeValue("", "connObjectKeyValue"), streamReader.getAttributeValue("", "name"))); break; case "onSyncope": onSyncope = new HashSet<>(); break; case "onResource": onResource = new HashSet<>(); break; case "value": Set<String> set = onSyncope == null ? onResource : onSyncope; set.add(streamReader.getElementText()); break; default: } } else if (streamReader.isEndElement()) { switch (streamReader.getLocalName()) { case "user": user.getMissing().addAll(missing); user.getMisaligned().addAll(misaligned); missing.clear(); misaligned.clear(); break; case "group": group.getMissing().addAll(missing); group.getMisaligned().addAll(misaligned); missing.clear(); misaligned.clear(); break; case "anyObject": anyObject.getMissing().addAll(missing); anyObject.getMisaligned().addAll(misaligned); missing.clear(); misaligned.clear(); break; case "onSyncope": misaligned.get(misaligned.size() - 1).getOnSyncope().addAll(onSyncope); onSyncope = null; break; case "onResource": misaligned.get(misaligned.size() - 1).getOnResource().addAll(onResource); onResource = null; break; default: } } streamReader.next(); } return report; }
From source file:org.apache.sysml.runtime.controlprogram.parfor.opt.PerfTestTool.java
private static void readProfile(String fname) throws XMLStreamException, IOException { //init profile map _profile = new HashMap<Integer, HashMap<Integer, CostFunction>>(); //read existing profile FileInputStream fis = new FileInputStream(fname); try {//from w w w .j a v a 2 s .co m //xml parsing XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(fis); int e = xsr.nextTag(); // profile start while (true) //read all instructions { e = xsr.nextTag(); // instruction start if (e == XMLStreamConstants.END_ELEMENT) break; //reached profile end tag //parse instruction int ID = Integer.parseInt(xsr.getAttributeValue(null, XML_ID)); //String name = xsr.getAttributeValue(null, XML_NAME).trim().replaceAll(" ", Lops.OPERAND_DELIMITOR); HashMap<Integer, CostFunction> tmp = new HashMap<Integer, CostFunction>(); _profile.put(ID, tmp); while (true) { e = xsr.nextTag(); // cost function start if (e == XMLStreamConstants.END_ELEMENT) break; //reached instruction end tag //parse cost function TestMeasure m = TestMeasure.valueOf(xsr.getAttributeValue(null, XML_MEASURE)); TestVariable lv = TestVariable.valueOf(xsr.getAttributeValue(null, XML_VARIABLE)); InternalTestVariable[] pv = parseTestVariables( xsr.getAttributeValue(null, XML_INTERNAL_VARIABLES)); DataFormat df = DataFormat.valueOf(xsr.getAttributeValue(null, XML_DATAFORMAT)); int tDefID = getTestDefID(m, lv, df, pv); xsr.next(); //read characters double[] params = parseParams(xsr.getText()); boolean multidim = _regTestDef.get(tDefID).getInternalVariables().length > 1; CostFunction cf = new CostFunction(params, multidim); tmp.put(tDefID, cf); xsr.nextTag(); // cost function end //System.out.println("added cost function"); } } xsr.close(); } finally { IOUtilFunctions.closeSilently(fis); } //mark profile as successfully read _flagReadData = true; }
From source file:org.apache.tajo.catalog.store.XMLCatalogSchemaManager.java
protected void loadFromXmlFile(XMLInputFactory xmlIf, URL filePath, List<StoreObject> storeObjects) throws IOException, XMLStreamException { XMLStreamReader xmlReader; xmlReader = xmlIf.createXMLStreamReader(filePath.openStream()); try {//from w ww . j av a 2s . c o m while (xmlReader.hasNext()) { if (xmlReader.next() == XMLStreamConstants.START_ELEMENT && "store".equals(xmlReader.getLocalName())) { StoreObject catalogStore = loadCatalogStore(xmlReader); if (catalogStore != null) { storeObjects.add(catalogStore); } } } } finally { try { xmlReader.close(); } catch (XMLStreamException ignored) { } } }
From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractRunMojo.java
protected StandardContext parseContextFile(File file) throws MojoExecutionException { try {/*w w w . j ava 2 s. c om*/ StandardContext standardContext = new StandardContext(); XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(new FileInputStream(file)); int tag = reader.next(); while (true) { if (tag == XMLStreamConstants.START_ELEMENT && StringUtils.equals("Context", reader.getLocalName())) { String path = reader.getAttributeValue(null, "path"); if (StringUtils.isNotBlank(path)) { standardContext.setPath(path); } String docBase = reader.getAttributeValue(null, "docBase"); if (StringUtils.isNotBlank(docBase)) { standardContext.setDocBase(docBase); } } if (!reader.hasNext()) { break; } tag = reader.next(); } return standardContext; } catch (XMLStreamException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (FileNotFoundException e) { throw new MojoExecutionException(e.getMessage(), e); } }