Java tutorial
/* Designed and developed by Ismail E. Kartoglu Copyright 2015 King's College London Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package cognition.pipeline.service.anonymisation; import org.apache.log4j.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Node; import org.jsoup.nodes.TextNode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import cognition.common.model.Individual; @Service public class AnonymisationService { private static Logger logger = Logger.getLogger(AnonymisationService.class); @Autowired private NamePseudonymiser namePseudonymiser; @Autowired private PhonePseudonymiser phonePseudonymiser; @Autowired private DateOfBirthPseudonymiser dateOfBirthPseudonymiser; @Autowired private NHSNumberPseudonymiser nhsNumberPseudonymiser; @Autowired private AddressPseudonymiser addressPseudonymiser; public String pseudonymisePersonHTML(Individual individual, String text) { Document document = Jsoup.parse(text); traverseAndAnonymise(document, individual); return document.toString(); } public String pseudonymisePersonPlainText(Individual individual, String text) { return pseudonymiseAll(individual, text); } private String pseudonymiseAll(Individual individual, String text) { text = namePseudonymiser.pseudonymise(individual, text); text = phonePseudonymiser.pseudonymise(individual, text); text = nhsNumberPseudonymiser.pseudonymise(individual, text); text = dateOfBirthPseudonymiser.pseudonymise(individual, text); text = addressPseudonymiser.pseudonymise(individual, text); return text; } private void traverseAndAnonymise(Node node, Individual individual) { if (node == null) { return; } if (node instanceof TextNode) { TextNode textNode = (TextNode) node; textNode.text(pseudonymiseAll(individual, textNode.text())); } if (CollectionUtils.isEmpty(node.childNodes())) { return; } for (Node child : node.childNodes()) { traverseAndAnonymise(child, individual); } } public void setNamePseudonymiser(NamePseudonymiser namePseudonymiser) { this.namePseudonymiser = namePseudonymiser; } public void setPhonePseudonymiser(PhonePseudonymiser phonePseudonymiser) { this.phonePseudonymiser = phonePseudonymiser; } public void setDateOfBirthPseudonymiser(DateOfBirthPseudonymiser dateOfBirthPseudonymiser) { this.dateOfBirthPseudonymiser = dateOfBirthPseudonymiser; } public void setNhsNumberPseudonymiser(NHSNumberPseudonymiser nhsNumberPseudonymiser) { this.nhsNumberPseudonymiser = nhsNumberPseudonymiser; } public void setAddressPseudonymiser(AddressPseudonymiser addressPseudonymiser) { this.addressPseudonymiser = addressPseudonymiser; } }