| Conditions | 14 |
| Total Lines | 94 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like br.ufrj.ppgi.greco.kettle.AnnotatorStep.processRow(StepMetaInterface,StepDataInterface) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package br.ufrj.ppgi.greco.kettle; |
||
| 63 | public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
||
| 64 | AnnotatorStepMeta meta = (AnnotatorStepMeta) smi; |
||
| 65 | AnnotatorStepData data = (AnnotatorStepData) sdi; |
||
| 66 | |||
| 67 | // Obtem linha do fluxo de entrada e termina caso nao haja mais entrada |
||
| 68 | |||
| 69 | Object[] row = getRow(); |
||
| 70 | |||
| 71 | if (row == null) { // Nao ha mais linhas de dados |
||
| 72 | setOutputDone(); |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Executa apenas uma vez. Variavel first definida na superclasse com |
||
| 77 | // valor true |
||
| 78 | if (first) { |
||
| 79 | first = false; |
||
| 80 | |||
| 81 | // Obtem todas as colunas ateh o step anterior. |
||
| 82 | // Chamar apenas apos chamar getRow() |
||
| 83 | RowMetaInterface rowMeta = getInputRowMeta(); |
||
| 84 | data.outputRowMeta = meta.getInnerKeepInputFields() ? rowMeta.clone() : new RowMeta(); |
||
| 85 | |||
| 86 | // Adiciona os metadados do step atual |
||
| 87 | meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
||
| 88 | } |
||
| 89 | |||
| 90 | String outputNTriple; |
||
| 91 | |||
| 92 | // Logica do step |
||
| 93 | // Leitura de campos Input |
||
| 94 | String inputSubject = getInputRowMeta().getString(row, meta.getInputSubject(), ""); |
||
| 95 | String inputPredicate = getInputRowMeta().getString(row, meta.getInputPredicate(), ""); |
||
| 96 | String inputObject = getInputRowMeta().getString(row, meta.getInputObject(), ""); |
||
| 97 | String outputSubject = inputSubject; |
||
| 98 | String outputPredicate = inputPredicate; |
||
| 99 | String outputObject = inputObject; |
||
| 100 | |||
| 101 | try { |
||
| 102 | // abre arquivo xml |
||
| 103 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
||
| 104 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
||
| 105 | Document doc = docBuilder.parse(new File(meta.getBrowseFilename())); |
||
| 106 | NodeList listOfMaps = doc.getElementsByTagName("map"); |
||
| 107 | int totalMaps = listOfMaps.getLength(); |
||
| 108 | // procura em cada node map as regras de anota |
||
| 109 | for (int i = 0; i < totalMaps; i++) { |
||
| 110 | Node fromMapNode = listOfMaps.item(i); |
||
| 111 | if (fromMapNode.getNodeType() == Node.ELEMENT_NODE) { |
||
| 112 | Element fromMapElement = (Element) fromMapNode; |
||
| 113 | NodeList fromList = fromMapElement.getElementsByTagName("from"); |
||
| 114 | Element fromElement = (Element) fromList.item(0); |
||
| 115 | NodeList textFList = fromElement.getChildNodes(); |
||
| 116 | NodeList toList = fromMapElement.getElementsByTagName("to"); |
||
| 117 | Element toElement = (Element) toList.item(0); |
||
| 118 | NodeList textTList = toElement.getChildNodes(); |
||
| 119 | if (((Node) textFList.item(0)).getNodeValue().trim().contains(inputSubject)) { |
||
| 120 | outputSubject = ((Node) textTList.item(0)).getNodeValue().trim(); |
||
| 121 | } |
||
| 122 | if (((Node) textFList.item(0)).getNodeValue().trim().contains(inputPredicate)) { |
||
| 123 | outputPredicate = ((Node) textTList.item(0)).getNodeValue().trim(); |
||
| 124 | } |
||
| 125 | if (((Node) textFList.item(0)).getNodeValue().trim().contains(inputObject)) { |
||
| 126 | outputObject = ((Node) textTList.item(0)).getNodeValue().trim(); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | } catch (ParserConfigurationException e) { |
||
| 132 | // TODO Auto-generated catch block |
||
| 133 | e.printStackTrace(); |
||
| 134 | } catch (SAXException e) { |
||
| 135 | // TODO Auto-generated catch block |
||
| 136 | e.printStackTrace(); |
||
| 137 | } catch (IOException e) { |
||
| 138 | // TODO Auto-generated catch block |
||
| 139 | e.printStackTrace(); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (inputPredicate.equals(RDF_TYPE_URI)) { |
||
| 143 | outputNTriple = String.format(URI_OBJECT_TRIPLE_FORMAT, outputSubject, outputPredicate, outputObject); |
||
| 144 | } else { |
||
| 145 | |||
| 146 | outputNTriple = String.format(LITERAL_OBJECT_TRIPLE_FORMAT, outputSubject, outputPredicate, outputObject); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Set output row |
||
| 150 | Object[] outputRow = meta.getInnerKeepInputFields() ? row : new Object[0]; |
||
| 151 | |||
| 152 | outputRow = RowDataUtil.addValueData(outputRow, outputRow.length, outputNTriple); |
||
| 153 | |||
| 154 | putRow(data.outputRowMeta, outputRow); |
||
| 155 | |||
| 156 | return true; |
||
| 157 | } |
||
| 159 |