Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue705: create CsmElement hierarchy
- Loading branch information
1 parent
55e5dd9
commit e007d46
Showing
13 changed files
with
538 additions
and
364 deletions.
There are no files selected for viewing
403 changes: 42 additions & 361 deletions
403
javaparser-core/src/main/java/com/github/javaparser/printer/ConcreteSyntaxModel.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...er-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmAttribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.observer.ObservableProperty; | ||
import com.github.javaparser.printer.ConcreteSyntaxModel; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
public class CsmAttribute implements CsmElement { | ||
private ObservableProperty property; | ||
|
||
public CsmAttribute(ObservableProperty property) { | ||
this.property = property; | ||
} | ||
|
||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
Object value = property.singleValueFor(node); | ||
if (value instanceof Enum) { | ||
printer.print(((Enum) value).name().toLowerCase()); | ||
} else { | ||
if (value != null) { | ||
printer.print(value.toString()); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...parser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmChild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.observer.ObservableProperty; | ||
import com.github.javaparser.printer.ConcreteSyntaxModel; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
|
||
class CsmChild implements CsmElement { | ||
private ObservableProperty property; | ||
|
||
public CsmChild(ObservableProperty property) { | ||
this.property = property; | ||
} | ||
|
||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
Node child = property.singlePropertyFor(node); | ||
if (child != null) { | ||
ConcreteSyntaxModel.genericPrettyPrint(child, printer); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...rser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmComment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.printer.ConcreteSyntaxModel; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
public class CsmComment implements CsmElement{ | ||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
if (node.hasComment()) { | ||
ConcreteSyntaxModel.genericPrettyPrint(node.getComment(), printer); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmConditional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.observer.ObservableProperty; | ||
import com.github.javaparser.printer.ConcreteSyntaxModel; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public class CsmConditional implements CsmElement { | ||
Predicate<Node> predicateCondition; | ||
private ObservableProperty condition; | ||
private CsmElement thenElement; | ||
private CsmElement elseElement; | ||
|
||
public CsmConditional(Predicate<Node> condition, CsmElement thenElement, CsmElement elseElement) { | ||
this.predicateCondition = condition; | ||
this.thenElement = thenElement; | ||
this.elseElement = elseElement; | ||
} | ||
|
||
public CsmConditional(ObservableProperty condition, CsmElement thenElement, CsmElement elseElement) { | ||
this.condition = condition; | ||
this.thenElement = thenElement; | ||
this.elseElement = elseElement; | ||
} | ||
|
||
public CsmConditional(ObservableProperty condition, CsmElement thenElement) { | ||
this.condition = condition; | ||
this.thenElement = thenElement; | ||
this.elseElement = null; | ||
} | ||
|
||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
boolean test; | ||
if (condition != null) { | ||
if (condition.isSingle()) { | ||
test = condition.singlePropertyFor(node) != null; | ||
} else { | ||
test = condition.listValueFor(node) != null && !condition.listValueFor(node).isEmpty(); | ||
} | ||
} else { | ||
test = predicateCondition.test(node); | ||
} | ||
if (test) { | ||
thenElement.prettyPrint(node, printer); | ||
} else { | ||
if (elseElement != null) { | ||
elseElement.prettyPrint(node, printer); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
|
||
public interface CsmElement { | ||
|
||
void prettyPrint(Node node, SourcePrinter printer); | ||
} |
33 changes: 33 additions & 0 deletions
33
...arser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmIndent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
public class CsmIndent implements CsmElement { | ||
|
||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
printer.indent(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
javaparser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2016 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.printer.concretesyntaxmodel; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.NodeList; | ||
import com.github.javaparser.ast.observer.ObservableProperty; | ||
import com.github.javaparser.printer.ConcreteSyntaxModel; | ||
import com.github.javaparser.printer.SourcePrinter; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
public class CsmList implements CsmElement { | ||
private ObservableProperty property; | ||
private CsmElement separator; | ||
private CsmElement preceeding; | ||
private CsmElement following; | ||
|
||
public CsmList(ObservableProperty property, CsmElement separator) { | ||
this.property = property; | ||
this.separator = separator; | ||
} | ||
|
||
public CsmList(ObservableProperty property) { | ||
this.property = property; | ||
this.separator = null; | ||
} | ||
|
||
public CsmList(ObservableProperty property, CsmElement separator, CsmElement preceeding, CsmElement following) { | ||
this.property = property; | ||
this.separator = separator; | ||
this.preceeding = preceeding; | ||
this.following = following; | ||
} | ||
|
||
@Override | ||
public void prettyPrint(Node node, SourcePrinter printer) { | ||
if (property.isAboutNodes()) { | ||
NodeList nodeList = property.listValueFor(node); | ||
if (nodeList == null) { | ||
return; | ||
} | ||
if (!nodeList.isEmpty() && preceeding != null) { | ||
preceeding.prettyPrint(node, printer); | ||
} | ||
for (int i = 0; i < nodeList.size(); i++) { | ||
ConcreteSyntaxModel.genericPrettyPrint(nodeList.get(i), printer); | ||
if (separator != null && i != (nodeList.size() - 1)) { | ||
separator.prettyPrint(node, printer); | ||
} | ||
} | ||
if (!nodeList.isEmpty() && following != null) { | ||
following.prettyPrint(node, printer); | ||
} | ||
} else { | ||
Collection<?> values = property.listPropertyFor(node); | ||
if (values == null) { | ||
return; | ||
} | ||
if (!values.isEmpty() && preceeding != null) { | ||
preceeding.prettyPrint(node, printer); | ||
} | ||
for (Iterator it = values.iterator(); it.hasNext(); ) { | ||
printer.print(it.next().toString()); | ||
if (separator != null && it.hasNext()) { | ||
separator.prettyPrint(node, printer); | ||
} | ||
} | ||
if (!values.isEmpty() && following != null) { | ||
following.prettyPrint(node, printer); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.