private static void addElement( List<String> lines, Patch patch, Map<String, RevFeatureType> featureTypes) throws IOException { String[] headerTokens = lines.get(0).split("\t"); if (headerTokens.length == 4 || headerTokens.length == 3) { // feature or feature type // modified // modification if (lines.size() == 1) { // feature type FeatureTypeDiff diff = new FeatureTypeDiff( headerTokens[0], ObjectId.valueOf(headerTokens[1]), ObjectId.valueOf(headerTokens[2])); patch.addAlteredTree(diff); } else { // feature String element = Joiner.on("\n").join(lines.subList(1, lines.size())); ByteArrayInputStream stream; stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8)); String operation = headerTokens[0].trim(); if (operation.equals("M")) { String fullPath = headerTokens[1].trim(); String oldMetadataId = headerTokens[2].trim(); String newMetadataId = headerTokens[3].trim(); RevFeatureType newRevFeatureType = featureTypes.get(newMetadataId); RevFeatureType oldRevFeatureType = featureTypes.get(oldMetadataId); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); for (int i = 1; i < lines.size(); i++) { addDifference(lines.get(i), map, oldRevFeatureType, newRevFeatureType); } FeatureDiff featureDiff = new FeatureDiff(fullPath, map, oldRevFeatureType, newRevFeatureType); patch.addModifiedFeature(featureDiff); } else if (operation.equals("A") || operation.equals("R")) { String fullPath = headerTokens[1].trim(); String featureTypeId = headerTokens[2].trim(); RevFeatureType revFeatureType; revFeatureType = featureTypes.get(featureTypeId); FeatureBuilder featureBuilder = new FeatureBuilder(revFeatureType); RevFeature revFeature = (RevFeature) serializer.read(null, stream); Feature feature = featureBuilder.build(NodeRef.nodeFromPath(fullPath), revFeature); if (operation.equals("R")) { patch.addRemovedFeature(fullPath, feature, revFeatureType); } else { patch.addAddedFeature(fullPath, feature, revFeatureType); } } else { throw new IllegalArgumentException("Wrong patch content: " + lines.get(0)); } } } else if (headerTokens.length == 1) { // feature type definition String element = Joiner.on("\n").join(lines); ByteArrayInputStream stream = new ByteArrayInputStream(element.getBytes(Charsets.UTF_8)); String[] tokens = lines.get(1).split("\t"); RevFeatureType featureType = (RevFeatureType) serializer.read(null, stream); featureTypes.put(featureType.getId().toString(), featureType); } else { throw new IllegalArgumentException("Wrong patch content: " + lines.get(0)); } }
public static void write(Writer w, Patch patch) throws IOException { StringBuilder sb = new StringBuilder(); List<RevFeatureType> featureTypes = patch.getFeatureTypes(); for (RevFeatureType featureType : featureTypes) { ByteArrayOutputStream output = new ByteArrayOutputStream(); serializer.write(featureType, output); sb.append(output.toString()); sb.append('\n'); } for (FeatureInfo feature : patch.getAddedFeatures()) { String path = feature.getPath(); sb.append("A\t" + path + "\t" + feature.getFeatureType().getId() + "\n"); ByteArrayOutputStream output = new ByteArrayOutputStream(); RevFeature revFeature = RevFeatureBuilder.build(feature.getFeature()); try { serializer.write(revFeature, output); } catch (IOException e) { } sb.append(output.toString()); sb.append('\n'); } for (FeatureInfo feature : patch.getRemovedFeatures()) { String path = feature.getPath(); sb.append("R\t" + path + "\t" + feature.getFeatureType().getId() + "\n"); ByteArrayOutputStream output = new ByteArrayOutputStream(); RevFeature revFeature = RevFeatureBuilder.build(feature.getFeature()); try { serializer.write(revFeature, output); } catch (IOException e) { } sb.append(output.toString()); sb.append('\n'); } for (FeatureDiff diff : patch.getModifiedFeatures()) { sb.append( "M\t" + diff.getPath() + "\t" + diff.getOldFeatureType().getId().toString() + "\t" + diff.getNewFeatureType().getId().toString() + "\n"); sb.append(diff.asText() + "\n"); } for (FeatureTypeDiff diff : patch.getAlteredTrees()) { sb.append(diff.toString() + "\n"); } w.write(sb.toString()); w.flush(); }